168 lines
8.6 KiB
Rust
168 lines
8.6 KiB
Rust
//! Resource presets: how much of the host `leleka up` lets the engine take (`docs/deploy.md`, "Resource presets").
|
|
//!
|
|
//! * `dedicated` (default): the host belongs to the runtime. The profile's VRAM budget as it is, engine defaults for the
|
|
//! memory governor, CPU workers up to the useful maximum.
|
|
//! * `workstation`: Docker, agents and the IDE share the machine. At least 4 cores (8 on hosts with more than 16) stay
|
|
//! free of engine workers, the memory governor keeps 8-16 GiB of host RAM free (it sheds the prefix cache first), and
|
|
//! about 4 GiB of VRAM is left to the desktop; the expert tier is sized from what remains.
|
|
//!
|
|
//! `compute` is a pure function of the host facts and the profile; `apply` writes the result into the profile and the
|
|
//! engine environment. Explicit profile fields and command-line overrides always win over a preset.
|
|
use std::collections::BTreeMap;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{
|
|
facts::{Facts, Gpu},
|
|
profile::{ARCHIVE_BYTES, ExpertSource, KarSource, Profile},
|
|
};
|
|
|
|
const GIB: u64 = 1 << 30;
|
|
const MIB: u64 = 1 << 20;
|
|
/// The native expert pool refuses more than 15 workers, and fewer cost decode: on the 5090 replay (job 9p0) 8 workers decode 2.4 % slower than 15
|
|
/// (136.4 against 139.8 tok/s), so the useful maximum is the pool's limit.
|
|
/// VRAM the desktop/browser keeps in the workstation preset.
|
|
const MAX_USEFUL_WORKERS: u32 = 15;
|
|
/// VRAM the desktop/browser keeps in the workstation preset. Every GiB taken from the expert tier costs decode: 3 GiB less (27 against 30 GiB) was
|
|
/// 12 % slower on the 5090 replay (job 9p0: 119.6 against 139.8 tok/s), so the margin is the 2 GiB a desktop session needs.
|
|
const WORKSTATION_VRAM_MARGIN_MIB: u64 = 2048;
|
|
/// Resident host memory of a memfd-backed engine besides the archive (tokenizer, pinned buffers, CUDA host state).
|
|
const ENGINE_EXTRA_BYTES: u64 = 3 * GIB;
|
|
/// Host memory of an engine with the resident expert source: the page-locked RAM copy of the routed experts plus rings, staging pools and pins
|
|
/// (53.54 GiB, the fixed footprint the engine's own budget reported in the live restart check plat-reconf-live; while it runs the 121 GiB server has only
|
|
/// 12-13 GiB available besides the 51.5 GiB sealed archive). It is what makes a higher free-RAM target unreachable on that host.
|
|
const ENGINE_FIXED_RESIDENT_BYTES: u64 = 54 * GIB;
|
|
/// RAM a Windows desktop session keeps for itself (browser, IDE, Docker Desktop) when the engine shares the machine.
|
|
const WINDOWS_DESKTOP_RESERVE_BYTES: u64 = 12 * GIB;
|
|
/// Same in the workstation preset: the coding agent, the IDE, a browser and their tooling work next to the engine (estimate, to be replaced by a
|
|
/// measured desktop of the 48 GB laptop).
|
|
const WINDOWS_WORKSTATION_DESKTOP_RESERVE_BYTES: u64 = 18 * GIB;
|
|
/// Hot part of the file-mapped expert bank the workstation preset plans for on Windows, instead of `min(MemTotal / 2, bank)`: the pages beyond it are
|
|
/// read from disk when the desktop takes the RAM (estimate; decode speed against this value is not measured yet).
|
|
const WINDOWS_WORKSTATION_WORKING_SET_GIB: u32 = 8;
|
|
/// RAM the Windows workstation preset keeps available (memory-governor soft floor; the hard floor is half of it).
|
|
const WINDOWS_WORKSTATION_RAM_TARGET_BYTES: u64 = 8 * GIB;
|
|
const MAPPED_WORKING_SET_ENV: &str = "LELEKA_MAPPED_WORKING_SET_GIB";
|
|
/// Host memory of the file-backed engine besides its page-locked expert complement (pinned K/V, rings, pools, mapped working set; 11 GiB measured natively).
|
|
const MAPPED_ENGINE_FIXED_BYTES: u64 = 11 * GIB;
|
|
/// The memory governor keeps this much beyond its soft floor (`memory.reserve_mib`, 2 GiB in the server profile).
|
|
const DEFAULT_RESERVE_BYTES: u64 = 2 * GIB;
|
|
/// Page-locked share of the routed experts in the mapped form never needs to exceed the bank itself.
|
|
const RESIDENT_BANK_GIB: u64 = 46;
|
|
/// Host memory the mapped form keeps for the engine itself, the tokenizer and the working set of the page cache.
|
|
const MAPPED_OVERHEAD_BYTES: u64 = 8 * GIB;
|
|
/// What the mapped form still needs available to start.
|
|
const MAPPED_MIN_AVAILABLE_BYTES: u64 = 10 * GIB;
|
|
pub const CPU_WORKERS_ENV: &str = "LELEKA_ORIGINAL_SERVE_CPU_WORKERS";
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum Preset {
|
|
#[default]
|
|
Dedicated,
|
|
Workstation,
|
|
}
|
|
|
|
impl Preset {
|
|
pub fn as_str(self) -> &'static str {
|
|
match self {
|
|
Preset::Dedicated => "dedicated",
|
|
Preset::Workstation => "workstation",
|
|
}
|
|
}
|
|
|
|
pub fn parse(text: &str) -> Result<Preset, String> {
|
|
match text {
|
|
"dedicated" => Ok(Preset::Dedicated),
|
|
"workstation" => Ok(Preset::Workstation),
|
|
other => Err(format!(
|
|
"unknown preset '{other}' (use dedicated or workstation)"
|
|
)),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
|
|
pub struct ResourcePlan {
|
|
pub preset: Preset,
|
|
pub cores: u32,
|
|
/// `None` when the core count is unknown: the engine keeps its own default.
|
|
pub cpu_workers: Option<u32>,
|
|
pub gpu_budget_bytes: u64,
|
|
/// The profile's `min_vram_free_mib`, lowered when the preset lowered the budget.
|
|
pub min_vram_free_mib: Option<u64>,
|
|
pub vram_free_after_mib: Option<u64>,
|
|
/// Host RAM the memory governor keeps available: (soft, hard) floors in MiB, `None` = engine defaults.
|
|
pub ram_floors_mib: Option<(u64, u64)>,
|
|
pub ram_expected_free_after_bytes: Option<u64>,
|
|
/// Windows file-backed form: GiB of experts the engine may page-lock, sized from the free RAM so that the desktop keeps its own working set
|
|
/// (without it the engine takes nearly all free RAM; live native run 2026-10-25: 31 GiB resident, 14 GiB of swap, a refused start).
|
|
pub resident_budget_gib: Option<u32>,
|
|
/// Windows file-backed form: lower memory-governor floors in MiB (soft, hard). Windows has no OOM killer and trims its standby list under
|
|
/// pressure, so the 4 GiB soft floor made for Linux only costs headroom: the engine's start check needs `fixed footprint + soft floor + reserve`
|
|
/// (29.4 + 4 + 1 GiB against 34 GiB available on the 48 GB laptop, live run 2026-10-25).
|
|
pub windows_floors_mib: Option<(u64, u64)>,
|
|
/// Windows workstation preset: GiB of the file-mapped expert bank the engine's start check plans as hot (`LELEKA_MAPPED_WORKING_SET_GIB`).
|
|
pub mapped_working_set_gib: Option<u32>,
|
|
/// Host RAM cannot hold the sealed archive and the experts next to the free target: serve from SSD/HDD instead
|
|
/// (archive as a read-only file, experts memory-mapped, this many GiB of them page-locked).
|
|
pub mapped_resident_gib: Option<u32>,
|
|
/// Shortfalls that make the preset unachievable (the preflight fails on them).
|
|
pub problems: Vec<String>,
|
|
pub warnings: Vec<String>,
|
|
}
|
|
|
|
fn gib_text(bytes: u64) -> String {
|
|
format!("{:.1} GiB", bytes as f64 / GIB as f64)
|
|
}
|
|
|
|
impl ResourcePlan {
|
|
/// The table `leleka up` prints: what the engine takes and what stays free.
|
|
pub fn render(&self, facts: &Facts) -> String {
|
|
let mut out = format!("resources ({} preset)\n", self.preset.as_str());
|
|
if let Some(workers) = self.cpu_workers {
|
|
out.push_str(&format!(
|
|
" cpu {workers} expert workers of {} cores; ~{} cores stay free\n",
|
|
self.cores,
|
|
self.cores.saturating_sub(workers + 1)
|
|
));
|
|
}
|
|
out.push_str(&format!(
|
|
" vram budget {}{}\n",
|
|
gib_text(self.gpu_budget_bytes),
|
|
self.vram_free_after_mib
|
|
.map_or(String::new(), |free| format!(", ~{} MiB stay free", free))
|
|
));
|
|
match (self.ram_floors_mib, self.ram_expected_free_after_bytes) {
|
|
(Some((soft, hard)), after) => out.push_str(&format!(
|
|
" host ram caches shed below {} MiB available, requests refused below {} MiB{}\n",
|
|
soft,
|
|
hard,
|
|
after.map_or(String::new(), |b| format!(
|
|
"; ~{} free after the start",
|
|
gib_text(b)
|
|
))
|
|
)),
|
|
(None, Some(after)) => out.push_str(&format!(
|
|
" host ram engine defaults; ~{} of {} free after the start\n",
|
|
gib_text(after),
|
|
gib_text(facts.memory.total)
|
|
)),
|
|
(None, None) => {}
|
|
}
|
|
for warning in &self.warnings {
|
|
out.push_str(&format!(" warning {warning}\n"));
|
|
}
|
|
out
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
mod sizing;
|
|
pub use sizing::{
|
|
apply, compute, prefill_capacity_for_budget, with_resident_archive, with_resident_bytes,
|
|
workstation_ram_target,
|
|
};
|