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