calmrun 0.1.0

A machine-wide queue for heavy commands, so parallel coding agents and terminals don't freeze the computer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! The scheduling rules as a pure function: state snapshot in, waiters allowed to start out.
//!
//! Every waiting calmrun process runs the same function on the same snapshot and starts only
//! if its own id is in the result, so the queue behaves as if one scheduler decided. The start
//! conditions themselves are [`crate::gates`]; this module owns order and passing.

use crate::state::Job;
use std::collections::BTreeMap;

/// Queue-wide limits from the config.
#[derive(Debug, Clone, Copy)]
pub struct Limits {
    pub budget: u32,
    /// Free RAM needed to start a job while something already runs (0 = no check).
    pub min_free_bytes: u64,
    /// A waiter older than this may no longer be passed (0 = strict order).
    pub pass_limit_ms: u64,
    /// RAM assumed taken per point by a job that has just started, before it shows up in
    /// the free-RAM reading (0 = no reservation).
    pub ram_per_point: u64,
    /// How long that assumption fades out over.
    pub reserve_window_ms: u64,
}

/// RAM assumed taken by jobs started in the last `reserve_window_ms`, fading linearly.
pub fn recent_reservation(jobs: &[Job], now_ms: u64, limits: &Limits) -> u64 {
    if limits.reserve_window_ms == 0 {
        return 0;
    }
    jobs.iter()
        .filter_map(|j| j.started_ms.map(|s| (j, now_ms.saturating_sub(s))))
        .filter(|(_, age)| *age < limits.reserve_window_ms)
        .map(|(j, age)| {
            let full = limits.ram_per_point * effective_cost(j.cost, limits.budget) as u64;
            (full as u128 * (limits.reserve_window_ms - age) as u128 / limits.reserve_window_ms as u128)
                as u64
        })
        .sum()
}

/// Everything besides the jobs that a scheduling pass looks at.
#[derive(Debug, Clone, Copy)]
pub struct Inputs<'a> {
    pub now_ms: u64,
    pub free_ram: u64,
    pub limits: Limits,
    /// Slots per named resource.
    pub caps: &'a BTreeMap<String, u32>,
}

/// Cost as the scheduler sees it: at least 1, at most the budget.
pub fn effective_cost(cost: u32, budget: u32) -> u32 {
    cost.clamp(1, budget.max(1))
}

/// Waiting jobs in queue order: prio high first, then `order` (first come). Parked entries
/// (owner gone, place kept for a ticket) are not waiting.
pub fn waiting_in_order(jobs: &[Job]) -> Vec<&Job> {
    let mut w: Vec<&Job> = jobs.iter().filter(|j| !j.running() && !j.parked()).collect();
    w.sort_by_key(|j| (!j.high, j.order, j.id));
    w
}

/// Points taken by running jobs.
pub fn used_points(jobs: &[Job], budget: u32) -> u32 {
    jobs.iter()
        .filter(|j| j.running())
        .map(|j| effective_cost(j.cost, budget))
        .sum()
}

/// Slots of a named resource (a resource that is not configured has 1 slot).
pub fn capacity(caps: &BTreeMap<String, u32>, name: &str) -> u32 {
    caps.get(name).copied().unwrap_or(1).max(1)
}

/// Why a waiter cannot start (for the waiting message).
#[derive(Debug, Clone, PartialEq)]
pub enum Block {
    Points {
        used: u32,
        needs: u32,
    },
    Ram {
        free: u64,
        needs: u64,
    },
    Disk,
    Key,
    /// All slots of this named resource are taken.
    Resource(String),
    /// Kept in line behind a waiter that may no longer be passed.
    Order,
}

/// The outcome of one scheduling pass.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Plan {
    /// Waiters that may start now, in queue order.
    pub admitted: Vec<u64>,
    /// Every other waiter with its reason.
    pub blocked: Vec<(u64, Block)>,
}

impl Plan {
    /// Whether this waiter may start.
    pub fn admits(&self, id: u64) -> bool {
        self.admitted.contains(&id)
    }

    /// Why this waiter may not start (`Order` if it was not looked at).
    pub fn reason(&self, id: u64) -> Block {
        self.blocked
            .iter()
            .find(|(b, _)| *b == id)
            .map_or(Block::Order, |(_, r)| r.clone())
    }
}

/// Walk the waiters in order; admit each that passes every gate, taking its share of each.
/// A later waiter may pass a blocked one only while no blocked waiter ahead of it has waited
/// longer than the pass limit.
pub fn schedule(jobs: &[Job], inputs: &Inputs) -> Plan {
    schedule_with(jobs, inputs, crate::gates::standard(jobs, inputs))
}

/// [`schedule`] with an explicit list of gates.
pub fn schedule_with<'a>(
    jobs: &'a [Job],
    inputs: &Inputs,
    mut gates: Vec<Box<dyn crate::gates::Gate<'a> + 'a>>,
) -> Plan {
    let mut active = jobs.iter().any(Job::running);
    let mut plan = Plan::default();
    let mut barrier = false;
    for w in waiting_in_order(jobs) {
        let reason = if barrier {
            Some(Block::Order)
        } else {
            gates.iter().find_map(|g| g.blocks(w, active))
        };
        match reason {
            None => {
                gates.iter_mut().for_each(|g| g.admit(w));
                active = true;
                plan.admitted.push(w.id);
            }
            Some(r) => {
                let waited = inputs.now_ms.saturating_sub(w.queued_ms);
                let limit = inputs.limits.pass_limit_ms;
                barrier |= limit == 0 || waited > limit;
                plan.blocked.push((w.id, r));
            }
        }
    }
    plan
}

/// Test shorthand: no named resources.
#[cfg(test)]
pub fn plan(jobs: &[Job], now_ms: u64, free_ram: u64, lim: Limits) -> (Vec<u64>, Vec<(u64, Block)>) {
    plan_with(jobs, now_ms, free_ram, lim, &BTreeMap::new())
}

/// Test shorthand with named resources.
#[cfg(test)]
pub fn plan_with(
    jobs: &[Job],
    now_ms: u64,
    free_ram: u64,
    limits: Limits,
    caps: &BTreeMap<String, u32>,
) -> (Vec<u64>, Vec<(u64, Block)>) {
    let p = schedule(
        jobs,
        &Inputs {
            now_ms,
            free_ram,
            limits,
            caps,
        },
    );
    (p.admitted, p.blocked)
}

/// Test shorthand: only the admitted ids.
#[cfg(test)]
pub fn admissible(jobs: &[Job], now_ms: u64, free_ram: u64, lim: Limits) -> Vec<u64> {
    plan(jobs, now_ms, free_ram, lim).0
}

#[cfg(test)]
mod tests {
    use super::*;

    const GB: u64 = 1 << 30;
    const LIM: Limits = Limits {
        budget: 4,
        min_free_bytes: 3 * GB,
        pass_limit_ms: 600_000,
        ram_per_point: 0,
        reserve_window_ms: 0,
    };

    fn job(id: u64, cost: u32, running: bool) -> Job {
        Job {
            id,
            pid: id as u32,
            pid_start: 0,
            label: format!("j{id}"),
            kind: "test".into(),
            cost,
            keys: vec![],
            cwd: ".".into(),
            high: false,
            order: id as i64,
            queued_ms: 1_000,
            started_ms: running.then_some(1_000),
            child_pid: None,
            parked_until: None,
            mem_bytes: None,
            disk_short: false,
            needs: Vec::new(),
        }
    }
    fn keyed(mut j: Job, k: &str) -> Job {
        j.keys = vec![k.into()];
        j
    }

    #[test]
    fn fills_budget_in_order() {
        let jobs = vec![job(1, 2, false), job(2, 2, false), job(3, 2, false)];
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![1, 2]);
    }

    #[test]
    fn respects_running_points() {
        let jobs = vec![job(1, 3, true), job(2, 2, false), job(3, 1, false)];
        // 2 does not fit; 3 fits and may pass because 2 is young.
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![3]);
    }

    #[test]
    fn strict_order_when_pass_limit_zero() {
        let jobs = vec![job(1, 3, true), job(2, 2, false), job(3, 1, false)];
        let lim = Limits {
            pass_limit_ms: 0,
            ..LIM
        };
        assert!(admissible(&jobs, 2_000, 16 * GB, lim).is_empty());
    }

    #[test]
    fn old_waiter_blocks_passing() {
        let jobs = vec![job(1, 3, true), job(2, 2, false), job(3, 1, false)];
        // job 2 queued at 1s; at 1s + 11 min it is past the 10 min pass limit.
        let now = 1_000 + 11 * 60_000;
        assert!(admissible(&jobs, now, 16 * GB, LIM).is_empty());
        // Exactly at the limit, passing is still allowed.
        assert_eq!(admissible(&jobs, 1_000 + 600_000, 16 * GB, LIM), vec![3]);
    }

    #[test]
    fn ram_gate_skipped_when_idle() {
        let jobs = vec![job(1, 1, false), job(2, 1, false)];
        // Only 1 GB free: the first may start because nothing runs, the second may not.
        assert_eq!(admissible(&jobs, 2_000, GB, LIM), vec![1]);
        let (_, blocked) = plan(&jobs, 2_000, GB, LIM);
        assert_eq!(
            blocked,
            vec![(
                2,
                Block::Ram {
                    free: GB,
                    needs: 3 * GB
                }
            )]
        );
    }

    #[test]
    fn ram_gate_when_busy_and_disabled() {
        let jobs = vec![job(1, 1, true), job(2, 1, false)];
        assert!(admissible(&jobs, 2_000, GB, LIM).is_empty());
        let lim = Limits {
            min_free_bytes: 0,
            ..LIM
        };
        assert_eq!(admissible(&jobs, 2_000, GB, lim), vec![2]);
    }

    #[test]
    fn prio_high_goes_first() {
        let mut hi = job(3, 2, false);
        hi.high = true;
        let jobs = vec![job(1, 1, true), job(2, 2, false), hi];
        // budget 4, used 1: only one cost-2 job fits; the high one wins.
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![3]);
    }

    #[test]
    fn exclusive_keys_never_overlap() {
        let jobs = vec![
            keyed(job(1, 1, true), "nm:a"),
            keyed(job(2, 1, false), "nm:a"),
            keyed(job(3, 1, false), "nm:b"),
        ];
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![3]);
        // Two waiters on the same key: only the first is admitted.
        let jobs = vec![keyed(job(1, 1, false), "nm:a"), keyed(job(2, 1, false), "nm:a")];
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![1]);
    }

    #[test]
    fn cost_clamped_to_budget() {
        let jobs = vec![job(1, 9, false)];
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![1]);
        assert_eq!(effective_cost(9, 4), 4);
        assert_eq!(effective_cost(0, 4), 1);
        let jobs = vec![job(1, 9, true), job(2, 1, false)];
        assert_eq!(used_points(&jobs, 4), 4);
        assert!(admissible(&jobs, 2_000, 16 * GB, LIM).is_empty());
    }

    #[test]
    fn bumped_order_goes_first() {
        let mut b = job(3, 2, false);
        b.order = -1;
        let jobs = vec![job(1, 2, true), job(2, 2, false), b];
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![3]);
    }

    #[test]
    fn named_resources_have_slots() {
        let need = |mut j: Job, n: &str| {
            j.needs = vec![n.to_string()];
            j
        };
        let mut caps = BTreeMap::new();
        caps.insert("db".to_string(), 1);
        caps.insert("gpu".to_string(), 2);
        // One db slot taken by a running job: the db waiter is blocked, the other passes.
        let jobs = vec![
            need(job(1, 1, true), "db"),
            need(job(2, 1, false), "db"),
            job(3, 1, false),
        ];
        let (adm, blocked) = plan_with(&jobs, 2_000, 16 * GB, LIM, &caps);
        assert_eq!(adm, vec![3]);
        assert_eq!(blocked, vec![(2, Block::Resource("db".into()))]);
        // Two gpu slots: two of three waiters start.
        let jobs = vec![
            need(job(1, 1, false), "gpu"),
            need(job(2, 1, false), "gpu"),
            need(job(3, 1, false), "gpu"),
        ];
        assert_eq!(plan_with(&jobs, 2_000, 16 * GB, LIM, &caps).0, vec![1, 2]);
        // Unconfigured resources have one slot.
        let jobs = vec![need(job(1, 1, false), "x"), need(job(2, 1, false), "x")];
        assert_eq!(plan_with(&jobs, 2_000, 16 * GB, LIM, &caps).0, vec![1]);
    }

    #[test]
    fn extra_gates_plug_in_without_touching_the_loop() {
        /// Lets only even job ids start.
        struct EvenOnly;
        impl<'a> crate::gates::Gate<'a> for EvenOnly {
            fn blocks(&self, w: &'a Job, _: bool) -> Option<Block> {
                (w.id % 2 == 1).then_some(Block::Key)
            }
            fn admit(&mut self, _: &'a Job) {}
        }
        let jobs = vec![job(1, 1, false), job(2, 1, false), job(4, 1, false)];
        let caps = BTreeMap::new();
        let inputs = Inputs {
            now_ms: 2_000,
            free_ram: 16 * GB,
            limits: LIM,
            caps: &caps,
        };
        let mut gates = crate::gates::standard(&jobs, &inputs);
        gates.push(Box::new(EvenOnly));
        let plan = schedule_with(&jobs, &inputs, gates);
        assert_eq!(plan.admitted, vec![2, 4]);
        assert_eq!(plan.reason(1), Block::Key);
    }

    #[test]
    fn just_started_jobs_reserve_ram_for_the_whole_pass() {
        let lim = Limits {
            ram_per_point: GB,
            reserve_window_ms: 20_000,
            ..LIM
        };
        // 4.5 GB free, gate needs 3 GB. One job started 0 s ago with cost 1 reserves 1 GB:
        // 3.5 GB left, so one more cost-1 job may start (taking 1 GB), the next may not.
        let mut started = job(1, 1, true);
        started.started_ms = Some(2_000);
        let jobs = vec![started, job(2, 1, false), job(3, 1, false)];
        let (adm, blocked) = plan(&jobs, 2_000, 4 * GB + GB / 2, lim);
        assert_eq!(adm, vec![2]);
        assert!(matches!(blocked[0], (3, Block::Ram { .. })), "{blocked:?}");
        // The reservation fades: half way through the window only half is reserved.
        assert_eq!(recent_reservation(&jobs, 12_000, &lim), GB / 2);
        assert_eq!(recent_reservation(&jobs, 30_000, &lim), 0);
    }

    #[test]
    fn short_disk_waits_only_while_busy() {
        let mut short = job(2, 1, false);
        short.disk_short = true;
        let jobs = vec![job(1, 1, true), short.clone(), job(3, 1, false)];
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![3]);
        let (_, blocked) = plan(&jobs, 2_000, 16 * GB, LIM);
        assert_eq!(blocked, vec![(2, Block::Disk)]);
        // Nothing running: it starts (the owner warns).
        assert_eq!(admissible(&[short], 2_000, 16 * GB, LIM), vec![2]);
    }

    #[test]
    fn parked_entries_are_skipped() {
        let mut parked = job(1, 2, false);
        parked.parked_until = Some(99_999_999);
        let jobs = vec![parked, job(2, 2, false)];
        assert_eq!(admissible(&jobs, 2_000, 16 * GB, LIM), vec![2]);
        assert_eq!(waiting_in_order(&jobs).len(), 1);
    }

    #[test]
    fn running_jobs_never_listed() {
        let jobs = vec![job(1, 2, true), job(2, 2, true)];
        assert!(admissible(&jobs, 2_000, 16 * GB, LIM).is_empty());
    }
}