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 | path::Path, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 4 | time::{Duration, SystemTime, UNIX_EPOCH}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 5 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 6 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 7 | use serde::{Deserialize, Serialize}; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 8 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 9 | use crate::{sched::Need, store, units::Bytes}; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 10 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 11 | const KEEP: usize = 1000; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 12 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 13 | #[derive(Clone, Copy, Debug, Serialize, Deserialize)] |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 14 | pub struct Record { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 15 | pub runs: u32, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 16 | pub peak_rss: Bytes, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 17 | /// ewma, so one freak run doesn't define the command forever |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 18 | pub wall: u64, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 19 | pub cpu: u64, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 20 | pub cores: u32, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 21 | pub last: u64, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 22 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 23 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 24 | type History = BTreeMap<String, Record>; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 25 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 26 | pub fn key(root: &Path, cmd: &[String]) -> String { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 27 | let mut k = format!("{}\t{}", root.display(), cmd.join(" ")); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 28 | k.truncate(k.floor_char_boundary(400)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 29 | k |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 30 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 31 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 32 | fn now() -> u64 { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 33 | SystemTime::now() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 34 | .duration_since(UNIX_EPOCH) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 35 | .map_or(0, |d| d.as_secs()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 36 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 37 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 38 | pub fn record(key: String, peak_rss: Bytes, wall: Duration, cpu: Duration, cores: u32) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 39 | let _ = store::update("history", |h: &mut History| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 40 | let wall = wall.as_secs(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 41 | h.entry(key) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 42 | .and_modify(|r| { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 43 | r.runs += 1; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 44 | r.peak_rss = r.peak_rss.max(peak_rss); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 45 | r.wall = (r.wall * 7 + wall * 3) / 10; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 46 | r.cpu = cpu.as_secs(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 47 | r.cores = cores; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 48 | r.last = now(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 49 | }) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 50 | .or_insert_with(|| Record { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 51 | runs: 1, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 52 | peak_rss, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 53 | wall, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 54 | cpu: cpu.as_secs(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 55 | cores, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 56 | last: now(), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 57 | }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 58 | if h.len() > KEEP { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 59 | let mut ages: Vec<_> = h.iter().map(|(k, r)| (r.last, k.clone())).collect(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 60 | ages.sort(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 61 | for (_, k) in ages.into_iter().take(h.len() - KEEP) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 62 | h.remove(&k); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 63 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 64 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 65 | }); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 66 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 67 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 68 | pub fn lookup(key: &str) -> Option<Record> { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 69 | store::load::<History>("history").get(key).copied() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 70 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 71 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 72 | /// padded, since the agent that under-asks is the one that ooms the video. |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 73 | /// nix builds happen in nix-daemon, so their rss never shows up in ours and |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 74 | /// the base estimate stands |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 75 | pub fn suggest(rec: &Record, base: Need, cmd: &[String]) -> Need { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 76 | let via_daemon = cmd |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 77 | .iter() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 78 | .any(|t| t.contains("nix") || t.contains("home-manager")); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 79 | Need { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 80 | ram: if via_daemon { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 81 | base.ram |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 82 | } else { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 83 | Bytes(rec.peak_rss.0 + rec.peak_rss.0 / 4).max(Bytes::mib(256)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 84 | }, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 85 | cores: rec.cores.max(1), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 86 | time: Duration::from_secs((rec.wall * 3 / 2).max(30)), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 87 | ..base |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 88 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 89 | } |