| 1 | //! `PressureConfig::state` reified sans-IO: load shedding is a pure classifier |
| 2 | //! of a resource sample plus prior shedding state (the hysteresis latch). No |
| 3 | //! I/O, no clock — the env read that builds the config stays at the edge |
| 4 | //! (`main.rs`). This is the degenerate case of the pattern: a pure decision |
| 5 | //! with no ports and no effects, included to prove the seam is uniform. It is |
| 6 | //! the extraction target for `main.rs`'s private `PressureConfig`. |
| 7 | |
| 8 | /// Watermarks and ceilings. Built once from the environment by the caller; |
| 9 | /// the core never reads the environment itself. |
| 10 | #[derive(Clone, Copy)] |
| 11 | pub struct PressureConfig { |
| 12 | pub resident_high: Option<usize>, |
| 13 | pub resident_low: usize, |
| 14 | pub rss_high_bytes: Option<u64>, |
| 15 | pub cpu_high_x100: Option<u64>, |
| 16 | } |
| 17 | |
| 18 | /// A resource sample — the only input the classifier reads. |
| 19 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 20 | pub struct Load { |
| 21 | pub resident_cells: usize, |
| 22 | pub rss_bytes: u64, |
| 23 | pub cpu_percent_x100: u64, |
| 24 | } |
| 25 | |
| 26 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 27 | pub struct PressureState { |
| 28 | pub admission_blocked: bool, |
| 29 | pub shedding: bool, |
| 30 | pub trigger: Option<&'static str>, |
| 31 | } |
| 32 | |
| 33 | impl PressureConfig { |
| 34 | /// The first ceiling the sample crosses, or `None`. |
| 35 | pub fn trigger(self, s: Load) -> Option<&'static str> { |
| 36 | if self.resident_high.is_some_and(|h| s.resident_cells >= h) { |
| 37 | return Some("resident_cells"); |
| 38 | } |
| 39 | if self.rss_high_bytes.is_some_and(|h| s.rss_bytes >= h) { |
| 40 | return Some("rss"); |
| 41 | } |
| 42 | if self.cpu_high_x100.is_some_and(|h| s.cpu_percent_x100 >= h) { |
| 43 | return Some("cpu"); |
| 44 | } |
| 45 | None |
| 46 | } |
| 47 | |
| 48 | /// Back under every low watermark (80% of each high) — the latch release. |
| 49 | pub fn relieved(self, s: Load) -> bool { |
| 50 | let resident_ok = |
| 51 | self.resident_high.is_none_or(|_| s.resident_cells <= self.resident_low); |
| 52 | let rss_ok = self |
| 53 | .rss_high_bytes |
| 54 | .is_none_or(|h| s.rss_bytes <= h.saturating_mul(4) / 5); |
| 55 | let cpu_ok = self |
| 56 | .cpu_high_x100 |
| 57 | .is_none_or(|h| s.cpu_percent_x100 <= h.saturating_mul(4) / 5); |
| 58 | resident_ok && rss_ok && cpu_ok |
| 59 | } |
| 60 | |
| 61 | /// How far to shed down to for a given trigger. |
| 62 | pub fn release_target(self, resident_cells: usize, reason: &str) -> usize { |
| 63 | if reason == "resident_cells" { |
| 64 | self.resident_low |
| 65 | } else { |
| 66 | resident_cells.saturating_sub((resident_cells / 10).max(1)) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// The resource whose low watermark still holds the shedding latch. An |
| 71 | /// instantaneous trigger wins; inside a hysteresis band, preserve the |
| 72 | /// resource-specific reason until every configured low watermark clears. |
| 73 | pub fn shedding_trigger( |
| 74 | self, |
| 75 | s: Load, |
| 76 | was_shedding: bool, |
| 77 | ) -> Option<&'static str> { |
| 78 | if let Some(trigger) = self.trigger(s) { |
| 79 | return Some(trigger); |
| 80 | } |
| 81 | if !was_shedding || self.relieved(s) { |
| 82 | return None; |
| 83 | } |
| 84 | if self.resident_high.is_some() && s.resident_cells > self.resident_low { |
| 85 | return Some("resident_cells"); |
| 86 | } |
| 87 | if self |
| 88 | .rss_high_bytes |
| 89 | .is_some_and(|h| s.rss_bytes > h.saturating_mul(4) / 5) |
| 90 | { |
| 91 | return Some("rss"); |
| 92 | } |
| 93 | if self |
| 94 | .cpu_high_x100 |
| 95 | .is_some_and(|h| s.cpu_percent_x100 > h.saturating_mul(4) / 5) |
| 96 | { |
| 97 | return Some("cpu"); |
| 98 | } |
| 99 | None |
| 100 | } |
| 101 | |
| 102 | /// The transition: `was_shedding` is the only carried state — the latch |
| 103 | /// that keeps shedding between the high crossing and the low release. |
| 104 | pub fn state(self, s: Load, was_shedding: bool) -> PressureState { |
| 105 | let trigger = self.trigger(s); |
| 106 | let admission_blocked = trigger.is_some(); |
| 107 | let shedding = self.shedding_trigger(s, was_shedding).is_some(); |
| 108 | PressureState { admission_blocked, shedding, trigger } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /// Admission order for cells waiting on resident capacity. |
| 114 | /// |
| 115 | /// A hibernated cell whose node is at its resident ceiling must wait for a |
| 116 | /// slot. celld's executor waits on a `Notify`: every waiter wakes on each |
| 117 | /// release and races to reserve. That is unfair by construction, and the |
| 118 | /// 2026-08-02 working-set pilot measured the consequence — 13 requests |
| 119 | /// timed out after 20 s while roughly 2,336 slots were freed around them, |
| 120 | /// at 117 evictions/s. They never ran out of capacity; they lost every race. |
| 121 | /// |
| 122 | /// Admission is therefore a QUEUE decision, and a pure one: given the |
| 123 | /// waiters in arrival order and the slots available, who is admitted? |
| 124 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 125 | pub struct Waiter { |
| 126 | /// Arrival tick — smaller is older. |
| 127 | pub arrived_at: u64, |
| 128 | pub id: u64, |
| 129 | } |
| 130 | |
| 131 | /// Admit up to `slots` waiters, oldest first. Returns their ids. |
| 132 | /// |
| 133 | /// FIFO is the whole policy. It costs nothing over a race and converts |
| 134 | /// "eventually, probably" into a bound: a waiter with `k` waiters ahead of |
| 135 | /// it is admitted within `k` releases, so no arrival pattern can starve it. |
| 136 | pub fn admit(waiters: &[Waiter], slots: usize) -> Vec<u64> { |
| 137 | let mut order: Vec<&Waiter> = waiters.iter().collect(); |
| 138 | order.sort_by_key(|w| (w.arrived_at, w.id)); |
| 139 | order.into_iter().take(slots).map(|w| w.id).collect() |
| 140 | } |
| 141 | |
| 142 | /// May one more cell be admitted to residency right now? |
| 143 | /// |
| 144 | /// Two independent facts gate admission, and conflating them is what turned |
| 145 | /// the 2,000-ring lab workload into a cliff rather than a plateau. |
| 146 | /// |
| 147 | /// `sampled_pressure` is the hysteresis latch from [`PressureConfig::state`]: |
| 148 | /// a periodic view of RSS, CPU, and residency, and the only thing entitled to |
| 149 | /// say "this node is overloaded". The headroom test below is the other, and it |
| 150 | /// is instantaneous. |
| 151 | /// |
| 152 | /// A request that merely lost a race for the last slot has learned the second |
| 153 | /// fact, not the first. If such a waiter is allowed to assert node-wide |
| 154 | /// pressure, every other cell on the node is refused until the next sample — |
| 155 | /// including cells that would fit — and the waiters keep the latch hot for one |
| 156 | /// another. Capacity that exists then goes unused for a full sampling period. |
| 157 | pub fn may_admit( |
| 158 | owned: usize, |
| 159 | pending: usize, |
| 160 | resident_high: Option<usize>, |
| 161 | sampled_pressure: bool, |
| 162 | ) -> bool { |
| 163 | if sampled_pressure { |
| 164 | return false; |
| 165 | } |
| 166 | resident_high.is_none_or(|high| owned.saturating_add(pending) < high) |
| 167 | } |