6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 1 | use std::{ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 2 | collections::BTreeMap, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 3 | fs, io, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 4 | os::unix::net::{UnixListener, UnixStream}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 5 | path::PathBuf, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 6 | process, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 7 | sync::{Arc, Mutex, MutexGuard}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 8 | thread, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 9 | time::{Duration, Instant}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 10 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 11 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 12 | use crate::{ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 13 | client, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 14 | config::Config, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 15 | paths, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 16 | proto::{self, Acquire, Event, LeaseId, LeaseInfo, Reader, Request, Status}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 17 | sched::{self, Capacity, Disk, FsId, Need, Queued, Reason, Running, Verdict}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 18 | sys, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 19 | units::Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 20 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 21 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 22 | struct Run { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 23 | started: Instant, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 24 | rss: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 25 | reminded: Option<Instant>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 26 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 27 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 28 | struct Lease { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 29 | req: Acquire, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 30 | heavy: bool, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 31 | fs: Vec<FsId>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 32 | queued_at: Instant, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 33 | run: Option<Run>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 34 | out: UnixStream, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 35 | waiting: Option<Reason>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 36 | last_pos: usize, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 37 | watchers: Vec<UnixStream>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 38 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 39 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 40 | impl Lease { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 41 | fn elapsed(&self) -> Duration { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 42 | self.run |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 43 | .as_ref() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 44 | .map_or(self.queued_at, |r| r.started) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 45 | .elapsed() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 46 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 47 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 48 | fn overdue(&self, grace: f64) -> bool { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 49 | self.run.is_some() && self.elapsed() > self.req.need.time.mul_f64(grace) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 50 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 51 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 52 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 53 | struct State { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 54 | cfg: Config, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 55 | next: LeaseId, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 56 | leases: BTreeMap<LeaseId, Lease>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 57 | cap: Capacity, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 58 | baselines: BTreeMap<FsId, Bytes>, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 59 | conns: usize, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 60 | idle_since: Instant, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 61 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 62 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 63 | type Shared = Arc<Mutex<State>>; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 64 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 65 | fn lock(shared: &Shared) -> MutexGuard<'_, State> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 66 | shared |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 67 | .lock() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 68 | .unwrap_or_else(std::sync::PoisonError::into_inner) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 69 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 70 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 71 | impl State { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 72 | fn new(cfg: Config) -> Self { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 73 | let mut st = Self { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 74 | cfg, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 75 | next: 1, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 76 | leases: BTreeMap::new(), |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 77 | cap: Capacity { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 78 | ram_budget: Bytes::ZERO, |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 79 | ram_live: Bytes::ZERO, |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 80 | ram_reserve: Bytes::ZERO, |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 81 | cores: 1, |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 82 | disks: BTreeMap::new(), |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 83 | pressure: false, |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 84 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 85 | baselines: BTreeMap::new(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 86 | conns: 0, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 87 | idle_since: Instant::now(), |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 88 | }; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 89 | st.apply_budget(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 90 | st |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 91 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 92 | |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 93 | fn apply_budget(&mut self) { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 94 | let (total, live) = sys::memory(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 95 | self.cap.ram_budget = self.cfg.budget.ram.resolve(total); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 96 | self.cap.ram_live = live; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 97 | self.cap.ram_reserve = self.cfg.budget.ram_reserve; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 98 | self.cap.cores = self.cfg.budget.cores.resolve(sys::cores()); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 99 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 100 | |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 101 | /// running leases keep the heaviness they started with, so a reload can't |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 102 | /// retroactively put two heavies side by side |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 103 | fn reload(&mut self, cfg: Config) { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 104 | self.cfg = cfg; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 105 | self.apply_budget(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 106 | let queued: Vec<(LeaseId, Need)> = self |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 107 | .leases |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 108 | .iter() |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 109 | .filter(|(_, l)| l.run.is_none()) |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 110 | .map(|(id, l)| (*id, l.req.need)) |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 111 | .collect(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 112 | for (id, need) in queued { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 113 | let heavy = self.heavy(&need); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 114 | if let Some(l) = self.leases.get_mut(&id) { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 115 | l.heavy = heavy; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 116 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 117 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 118 | self.refresh(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 119 | self.reschedule(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 120 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 121 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 122 | fn heavy(&self, need: &Need) -> bool { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 123 | let h = &self.cfg.heavy; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 124 | need.ram >= h.ram || need.disk >= h.disk || need.cores >= h.cores.resolve(sys::cores()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 125 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 126 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 127 | /// capped at a tenth of the fs, or a 16G tmpfs /tmp with a 20G reserve |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 128 | /// admits nothing ever |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 129 | fn reserve(&self, size: Bytes) -> Bytes { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 130 | self.cfg.budget.disk_reserve.min(Bytes(size.0 / 10)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 131 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 132 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 133 | fn fs_for(&mut self, cwd: &PathBuf) -> Vec<FsId> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 134 | let mut ids = Vec::new(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 135 | for path in std::iter::once(cwd).chain(&self.cfg.budget.disk_paths) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 136 | if let Some(fs) = sys::fs(path) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 137 | if !ids.contains(&fs.id) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 138 | ids.push(fs.id); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 139 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 140 | let reserve = self.reserve(fs.size); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 141 | self.cap.disks.entry(fs.id).or_insert(Disk { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 142 | free: fs.free, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 143 | reserve, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 144 | consumed: Bytes::ZERO, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 145 | }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 146 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 147 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 148 | ids |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 149 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 150 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 151 | fn refresh(&mut self) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 152 | let (_, live) = sys::memory(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 153 | self.cap.ram_live = live; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 154 | self.cap.pressure = self |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 155 | .cfg |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 156 | .budget |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 157 | .psi_max |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 158 | .is_some_and(|max| sys::pressure().is_some_and(|p| p > max)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 159 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 160 | let mut paths: Vec<PathBuf> = self.leases.values().map(|l| l.req.cwd.clone()).collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 161 | paths.extend(self.cfg.budget.disk_paths.iter().cloned()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 162 | let mut disks = BTreeMap::new(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 163 | for path in paths { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 164 | if let Some(fs) = sys::fs(&path) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 165 | disks.insert(fs.id, (fs.free, self.reserve(fs.size))); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 166 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 167 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 168 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 169 | let busy: Vec<FsId> = self |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 170 | .leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 171 | .values() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 172 | .filter(|l| l.run.is_some()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 173 | .flat_map(|l| l.fs.iter().copied()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 174 | .collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 175 | self.baselines.retain(|id, _| busy.contains(id)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 176 | self.cap.disks = disks |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 177 | .into_iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 178 | .map(|(id, (free, reserve))| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 179 | let consumed = if busy.contains(&id) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 180 | self.baselines |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 181 | .entry(id) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 182 | .or_insert(free) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 183 | .saturating_sub(free) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 184 | } else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 185 | Bytes::ZERO |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 186 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 187 | ( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 188 | id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 189 | Disk { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 190 | free, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 191 | reserve, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 192 | consumed, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 193 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 194 | ) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 195 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 196 | .collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 197 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 198 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 199 | fn running(&self) -> Vec<Running> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 200 | self.leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 201 | .values() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 202 | .filter_map(|l| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 203 | let run = l.run.as_ref()?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 204 | Some(Running { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 205 | need: l.req.need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 206 | heavy: l.heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 207 | fs: l.fs.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 208 | rss: run.rss, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 209 | remaining: l.req.need.time.saturating_sub(run.started.elapsed()), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 210 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 211 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 212 | .collect() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 213 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 214 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 215 | fn reschedule(&mut self) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 216 | let queue: Vec<Queued> = self |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 217 | .leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 218 | .iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 219 | .filter(|(_, l)| l.run.is_none()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 220 | .map(|(id, l)| Queued { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 221 | id: *id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 222 | need: l.req.need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 223 | heavy: l.heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 224 | fs: l.fs.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 225 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 226 | .collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 227 | if queue.is_empty() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 228 | return; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 229 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 230 | let verdicts = sched::plan( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 231 | &queue, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 232 | &self.running(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 233 | &self.cap, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 234 | self.cfg.queue.backfill_max_time.0, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 235 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 236 | let cores = self.cap.cores; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 237 | let mut position = 0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 238 | for (id, verdict) in verdicts { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 239 | let Some(lease) = self.leases.get_mut(&id) else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 240 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 241 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 242 | match verdict { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 243 | Verdict::Admit => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 244 | lease.run = Some(Run { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 245 | started: Instant::now(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 246 | rss: Bytes::ZERO, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 247 | reminded: None, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 248 | }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 249 | let _ = proto::send( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 250 | &mut lease.out, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 251 | &Event::Granted { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 252 | lease: id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 253 | cores: lease.req.need.cores.clamp(1, cores.max(1)), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 254 | heavy: lease.heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 255 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 256 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 257 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 258 | Verdict::Wait(reason) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 259 | position += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 260 | lease.waiting = Some(reason); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 261 | if lease.last_pos != position { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 262 | lease.last_pos = position; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 263 | let _ = proto::send( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 264 | &mut lease.out, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 265 | &Event::Queued { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 266 | lease: id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 267 | position, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 268 | reason: Some(reason), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 269 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 270 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 271 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 272 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 273 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 274 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 275 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 276 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 277 | fn remove(&mut self, id: LeaseId) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 278 | if let Some(lease) = self.leases.remove(&id) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 279 | for mut w in lease.watchers { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 280 | let _ = proto::send(&mut w, &Event::Fired { overdue: false }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 281 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 282 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 283 | if self.leases.is_empty() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 284 | self.idle_since = Instant::now(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 285 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 286 | self.refresh(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 287 | self.reschedule(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 288 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 289 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 290 | fn fire_overdue(&mut self) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 291 | let grace = self.cfg.overrun.time_grace; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 292 | for lease in self.leases.values_mut() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 293 | if !lease.watchers.is_empty() && lease.overdue(grace) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 294 | for mut w in lease.watchers.drain(..) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 295 | let _ = proto::send(&mut w, &Event::Fired { overdue: true }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 296 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 297 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 298 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 299 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 300 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 301 | fn info(&self) -> Vec<LeaseInfo> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 302 | let grace = self.cfg.overrun.time_grace; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 303 | let mut position = 0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 304 | self.leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 305 | .iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 306 | .map(|(id, l)| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 307 | if l.run.is_none() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 308 | position += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 309 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 310 | LeaseInfo { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 311 | id: *id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 312 | cmd: l.req.cmd.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 313 | cwd: l.req.cwd.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 314 | session: l.req.session.clone(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 315 | need: l.req.need, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 316 | heavy: l.heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 317 | running: l.run.is_some(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 318 | elapsed: l.elapsed().as_secs(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 319 | rss: l.run.as_ref().map_or(Bytes::ZERO, |r| r.rss), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 320 | overdue: l.overdue(grace), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 321 | waiting: l.waiting.filter(|_| l.run.is_none()), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 322 | position: if l.run.is_none() { position } else { 0 }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 323 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 324 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 325 | .collect() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 326 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 327 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 328 | fn status(&self) -> Status { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 329 | let (ram_free, cores_free) = sched::headroom(&self.running(), &self.cap); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 330 | Status { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 331 | leases: self.info(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 332 | ram_free, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 333 | ram_budget: self.cap.ram_budget, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 334 | cores_free, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 335 | cores: self.cap.cores, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 336 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 337 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 338 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 339 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 340 | fn serve(shared: &Shared, mut stream: UnixStream) -> io::Result<()> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 341 | let mut reader = Reader::new(&stream)?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 342 | let Some(req) = reader.recv::<Request>()? else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 343 | return Ok(()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 344 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 345 | match req { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 346 | Request::Acquire(req) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 347 | let id = { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 348 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 349 | let id = st.next; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 350 | st.next += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 351 | let fs = st.fs_for(&req.cwd); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 352 | let heavy = st.heavy(&req.need); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 353 | st.leases.insert( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 354 | id, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 355 | Lease { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 356 | req, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 357 | heavy, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 358 | fs, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 359 | queued_at: Instant::now(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 360 | run: None, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 361 | out: stream.try_clone()?, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 362 | waiting: None, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 363 | last_pos: 0, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 364 | watchers: Vec::new(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 365 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 366 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 367 | st.reschedule(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 368 | id |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 369 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 370 | hold(shared, id, &mut reader); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 371 | lock(shared).remove(id); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 372 | Ok(()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 373 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 374 | Request::Status => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 375 | let status = lock(shared).status(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 376 | proto::send(&mut stream, &Event::Status(status)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 377 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 378 | Request::Kill { lease } => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 379 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 380 | let reply = match st.leases.get_mut(&lease) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 381 | Some(l) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 382 | let _ = proto::send(&mut l.out, &Event::Kill); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 383 | Event::Ok |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 384 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 385 | None => Event::Err { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 386 | msg: format!("no lease {lease}"), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 387 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 388 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 389 | drop(st); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 390 | proto::send(&mut stream, &reply) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 391 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 392 | Request::Extend { lease, by } => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 393 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 394 | let reply = match st.leases.get_mut(&lease) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 395 | Some(l) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 396 | l.req.need.time += by.0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 397 | if let Some(run) = &mut l.run { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 398 | run.reminded = None; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 399 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 400 | Event::Ok |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 401 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 402 | None => Event::Err { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 403 | msg: format!("no lease {lease}"), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 404 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 405 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 406 | drop(st); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 407 | proto::send(&mut stream, &reply) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 408 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 409 | Request::Watch { lease } => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 410 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 411 | let grace = st.cfg.overrun.time_grace; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 412 | match st.leases.get_mut(&lease) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 413 | Some(l) if l.overdue(grace) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 414 | proto::send(&mut stream, &Event::Fired { overdue: true }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 415 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 416 | Some(l) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 417 | l.watchers.push(stream); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 418 | Ok(()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 419 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 420 | None => proto::send(&mut stream, &Event::Fired { overdue: false }), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 421 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 422 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 423 | Request::Overdue { session } => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 424 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 425 | let grace = st.cfg.overrun.time_grace; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 426 | let every = st.cfg.overrun.remind_every.0; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 427 | let mut due = Vec::new(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 428 | for (id, l) in &mut st.leases { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 429 | if l.req.session.as_deref() != Some(&session) || !l.overdue(grace) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 430 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 431 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 432 | let Some(run) = &mut l.run else { continue }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 433 | if run.reminded.is_some_and(|t| t.elapsed() < every) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 434 | continue; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 435 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 436 | run.reminded = Some(Instant::now()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 437 | due.push(*id); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 438 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 439 | let leases = st |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 440 | .info() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 441 | .into_iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 442 | .filter(|i| due.contains(&i.id)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 443 | .collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 444 | drop(st); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 445 | proto::send(&mut stream, &Event::Overdue { leases }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 446 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 447 | Request::Started { .. } | Request::Usage { .. } | Request::Done => proto::send( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 448 | &mut stream, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 449 | &Event::Err { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 450 | msg: "no lease on this connection".into(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 451 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 452 | ), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 453 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 454 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 455 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 456 | /// a client that vanishes without saying done got sigkilled or similar, and |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 457 | /// pdeathsig only reaches its direct child. take the rest of the group down |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 458 | /// so a headless build doesn't keep eating ram off the books |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 459 | fn hold(shared: &Shared, id: LeaseId, reader: &mut Reader) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 460 | let mut pgid = None; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 461 | let clean = loop { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 462 | match reader.recv::<Request>() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 463 | Ok(Some(Request::Usage { rss })) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 464 | if let Some(run) = lock(shared) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 465 | .leases |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 466 | .get_mut(&id) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 467 | .and_then(|l| l.run.as_mut()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 468 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 469 | run.rss = rss; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 470 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 471 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 472 | Ok(Some(Request::Started { pid })) => pgid = Some(pid), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 473 | Ok(Some(Request::Done)) => break true, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 474 | Ok(Some(_)) => {} |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 475 | Ok(None) | Err(_) => break false, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 476 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 477 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 478 | if !clean && let Some(pgid) = pgid.filter(|p| *p > 1) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 479 | unsafe { libc::kill(-pgid, libc::SIGTERM) }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 480 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 481 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 482 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 483 | fn tick(shared: &Shared) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 484 | loop { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 485 | thread::sleep(Duration::from_secs(1)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 486 | let mut st = lock(shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 487 | st.refresh(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 488 | st.reschedule(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 489 | st.fire_overdue(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 490 | if st.conns == 0 && st.leases.is_empty() && st.idle_since.elapsed() >= st.cfg.queue.linger.0 |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 491 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 492 | // a spawner holds this while it clears a stale socket, so taking it |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 493 | // here stops us unlinking our successor's socket on the way out |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 494 | if let Some(_guard) = try_spawn_lock() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 495 | let _ = fs::remove_file(paths::socket()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 496 | process::exit(0); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 497 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 498 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 499 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 500 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 501 | |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 502 | /// watches the directory, not the file. editors save by renaming over it, |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 503 | /// which would orphan a watch on the old inode after the first edit |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 504 | #[cfg(target_os = "linux")] |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 505 | fn watch_config(shared: &Shared) { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 506 | use std::os::unix::ffi::OsStrExt; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 507 | |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 508 | let path = paths::config_file(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 509 | let (Some(dir), Some(name)) = (path.parent(), path.file_name()) else { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 510 | return; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 511 | }; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 512 | let _ = fs::create_dir_all(dir); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 513 | let Ok(cdir) = std::ffi::CString::new(dir.as_os_str().as_bytes()) else { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 514 | return; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 515 | }; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 516 | let fd = unsafe { libc::inotify_init1(libc::IN_CLOEXEC) }; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 517 | if fd < 0 { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 518 | return; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 519 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 520 | let mask = libc::IN_CLOSE_WRITE |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 521 | | libc::IN_MOVED_TO |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 522 | | libc::IN_MOVED_FROM |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 523 | | libc::IN_CREATE |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 524 | | libc::IN_DELETE; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 525 | if unsafe { libc::inotify_add_watch(fd, cdir.as_ptr(), mask) } < 0 { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 526 | return; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 527 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 528 | |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 529 | let header = std::mem::size_of::<libc::inotify_event>(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 530 | let mut buf = [0u8; 4096]; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 531 | loop { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 532 | let n = unsafe { libc::read(fd, buf.as_mut_ptr().cast(), buf.len()) }; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 533 | let Ok(n) = usize::try_from(n) else { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 534 | return; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 535 | }; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 536 | let mut hit = false; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 537 | let mut off = 0; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 538 | while off + header <= n { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 539 | let ev: libc::inotify_event = |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 540 | unsafe { std::ptr::read_unaligned(buf.as_ptr().add(off).cast()) }; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 541 | let len = ev.len as usize; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 542 | let raw = &buf[off + header..(off + header + len).min(n)]; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 543 | let ev_name = raw.split(|b| *b == 0).next().unwrap_or_default(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 544 | hit |= ev_name == name.as_bytes(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 545 | off += header + len; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 546 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 547 | if !hit { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 548 | continue; |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 549 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 550 | thread::sleep(Duration::from_millis(100)); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 551 | match Config::load() { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 552 | Ok(cfg) => { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 553 | lock(shared).reload(cfg); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 554 | eprintln!("roundabout: config reloaded"); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 555 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 556 | Err(e) => eprintln!("roundabout: kept the old config, {e}"), |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 557 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 558 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 559 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 560 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 561 | fn try_spawn_lock() -> Option<client::Flock> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 562 | let file = fs::OpenOptions::new() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 563 | .write(true) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 564 | .open(paths::spawn_lock()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 565 | .ok()?; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 566 | client::Flock::try_exclusive(file) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 567 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 568 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 569 | pub fn main(cfg: Config) -> ! { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 570 | // fork once more so the client that spawned us can reap its child now |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 571 | // instead of carrying a zombie for the length of its build |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 572 | match unsafe { libc::fork() } { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 573 | -1 => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 574 | eprintln!("roundabout: fork failed: {}", io::Error::last_os_error()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 575 | process::exit(1); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 576 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 577 | 0 => {} |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 578 | _ => process::exit(0), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 579 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 580 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 581 | let listener = match UnixListener::bind(paths::socket()) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 582 | Ok(l) => l, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 583 | Err(e) if e.kind() == io::ErrorKind::AddrInUse && client::connect().is_some() => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 584 | process::exit(0) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 585 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 586 | Err(e) => { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 587 | eprintln!("roundabout: bind: {e}"); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 588 | process::exit(1); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 589 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 590 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 591 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 592 | let shared: Shared = Arc::new(Mutex::new(State::new(cfg))); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 593 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 594 | let shared = shared.clone(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 595 | thread::spawn(move || tick(&shared)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 596 | } |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 597 | #[cfg(target_os = "linux")] |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 598 | { |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 599 | let shared = shared.clone(); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 600 | thread::spawn(move || watch_config(&shared)); |
56a853ebf254 | atagen | 2026-09-27 03:59 UTC | 601 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 602 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 603 | for stream in listener.incoming() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 604 | let Ok(stream) = stream else { continue }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 605 | lock(&shared).conns += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 606 | let shared = shared.clone(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 607 | thread::spawn(move || { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 608 | if let Err(e) = serve(&shared, stream) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 609 | eprintln!("roundabout: {e}"); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 610 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 611 | let mut st = lock(&shared); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 612 | st.conns -= 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 613 | if st.leases.is_empty() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 614 | st.idle_since = Instant::now(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 615 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 616 | }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 617 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 618 | process::exit(0) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 619 | } |