fix(app): only a reading made with no game running is kept as other programs' memory

- a normal placement keeps its reading only when no game runs: the perf loop passes whether a
  game runs to every switch, so the way back from a failed game stage while the game runs keeps
  the reading from before the game
- game detection also looks at every running program once as the services start: a program in
  a launcher's game folder or the person's games list counts, also behind other windows
- checked on the win-ci VM: a minimised program in a Steam game folder is found at the start
  while nothing is in front

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 10:52:48 +09:00
parent 4a81b88d41
commit 5316f4e9b0
6 changed files with 175 additions and 41 deletions

View File

@@ -256,8 +256,9 @@ pub struct Llm {
/// game stage counts other programs from it (the card then also holds the game, which its
/// reserve is for). None when the services started with a game running.
baseline: Mutex<Option<i64>>,
/// A game ran when the services started (set before `start`).
game_at_start: AtomicBool,
/// A game runs (or may run) as the LLM is placed: set before `start` and at every `switch`.
/// Only a normal placement made while none runs keeps its reading.
game_running: AtomicBool,
/// The STT worker's model is on the card (true at switches while the worker runs), so it is not
/// taken for another program's memory.
stt_on_card: AtomicBool,
@@ -286,14 +287,14 @@ impl Llm {
pub fn with_exe(layout: Layout, exe: std::path::PathBuf) -> Self {
Self { layout, exe, status: Mutex::new(LlmStatus { state: LlmState::Off, managed: false, percent: None, note: None }),
active: Mutex::new(None), cancel: Arc::new(AtomicBool::new(false)), port: Mutex::new(0),
stop_timeout: Duration::from_secs(120), gpu: None, baseline: Mutex::new(None), game_at_start: AtomicBool::new(false), stt_on_card: AtomicBool::new(false), room: Mutex::new(None), threads: Mutex::new(None),
stop_timeout: Duration::from_secs(120), gpu: None, baseline: Mutex::new(None), game_running: AtomicBool::new(false), stt_on_card: AtomicBool::new(false), room: Mutex::new(None), threads: Mutex::new(None),
stage: Mutex::new((Stage::Normal, None)) }
}
/// Whether a game runs as the services start: the start's reading of the card then includes it
/// and is not kept as other programs' memory.
/// Whether a game runs (also in the background) as the services start: the start's reading of
/// the card then includes it and is not kept as other programs' memory.
pub fn game_at_start(&self, on: bool) {
self.game_at_start.store(on, Ordering::SeqCst);
self.game_running.store(on, Ordering::SeqCst);
}
/// `{stage, gameRoomMib}` for the dashboard.
@@ -351,9 +352,9 @@ impl Llm {
// from the latest normal placement, as the card then also holds the game.
let stt_model = if self.stt_on_card.load(Ordering::SeqCst) { crate::placement::STT_MODEL_MIB } else { 0 };
let now_others = (card.used_mib - stt_model).max(0);
// A normal placement keeps its reading, unless a game ran at the start (its memory would be
// taken for other programs'; a later normal placement comes only after the game ended).
if stage != Stage::Gaming && !self.game_at_start.swap(false, Ordering::SeqCst) {
// Only a normal placement made while no game runs keeps its reading (a game's memory would be
// taken for other programs'; also on the way back from a failed game stage).
if stage != Stage::Gaming && !self.game_running.load(Ordering::SeqCst) {
*self.baseline.lock().unwrap() = Some(now_others);
}
let baseline = *self.baseline.lock().unwrap();
@@ -456,12 +457,13 @@ impl Llm {
/// Move the running LLM to `stage` (`perf.rs`): a restart with that stage's placement, or a stop
/// when it pauses (also a game stage with no room for the game). Returns the stage it ended in.
/// With `llm.url` nothing moves.
/// `stt_on_card`: the STT worker's model is loaded on the card.
pub async fn switch(&self, settings: &Settings, stage: Stage, stt_on_card: bool) -> Result<Stage, String> {
/// `stt_on_card`: the STT worker's model is loaded on the card; `game_running`: a game runs now.
pub async fn switch(&self, settings: &Settings, stage: Stage, stt_on_card: bool, game_running: bool) -> Result<Stage, String> {
if settings.llm.url.is_some() {
return Ok(Stage::Normal);
}
self.stt_on_card.store(stt_on_card, Ordering::SeqCst);
self.game_running.store(game_running, Ordering::SeqCst);
if stage != Stage::Paused && self.start_stage(settings, stage).await?.is_some() {
return Ok(stage);
}
@@ -987,7 +989,7 @@ pub(crate) mod tests {
llm.start(&s).await.unwrap();
assert_eq!(llm.stage_view(), json!({"stage": "normal", "gameRoomMib": null}));
std::fs::remove_file(l.data.join("calls")).unwrap();
assert_eq!(tokio::time::timeout(Duration::from_secs(30), llm.switch(&s, Stage::Gaming, false)).await.unwrap().unwrap(), Stage::Gaming);
assert_eq!(tokio::time::timeout(Duration::from_secs(30), llm.switch(&s, Stage::Gaming, false, true)).await.unwrap().unwrap(), Stage::Gaming);
let g = launches(&l);
// Measured once at the game's context and one request, then the 2 GiB minimum (8 GB does not
// fit on 16 GB) with half the engine's 16 CPU threads.
@@ -999,13 +1001,13 @@ pub(crate) mod tests {
assert_eq!(llm.status().state, LlmState::Ready);
// Back to normal: the run's first reading of other programs, the normal placement again.
std::fs::remove_file(l.data.join("calls")).unwrap();
assert_eq!(llm.switch(&s, Stage::Normal, false).await.unwrap(), Stage::Normal);
assert_eq!(llm.switch(&s, Stage::Normal, false, false).await.unwrap(), Stage::Normal);
let n = launches(&l);
assert_eq!(n.len(), 1, "{n:?}");
assert!(n[0].contains("--context 32768") && n[0].ends_with("--set expert-vram=1.5 --set prefill=2048"), "{}", n[0]);
assert_eq!(llm.stage_view(), json!({"stage": "normal", "gameRoomMib": null}));
// Pause: the LLM stops.
assert_eq!(llm.switch(&s, Stage::Paused, false).await.unwrap(), Stage::Paused);
assert_eq!(llm.switch(&s, Stage::Paused, false, true).await.unwrap(), Stage::Paused);
assert_eq!(llm.status().state, LlmState::Off);
assert_eq!(llm.stage_view()["stage"], json!("paused"));
}
@@ -1036,11 +1038,11 @@ pub(crate) mod tests {
let llm = llm.with_gpu(live_card(&l, others.clone(), true));
// A game starts and holds 2500 MiB: the game stage counts other programs as before the game.
others.store(1460 + 2500, Ordering::SeqCst);
llm.switch(&s, Stage::Gaming, true).await.unwrap();
llm.switch(&s, Stage::Gaming, true, true).await.unwrap();
assert!(launches(&l).last().unwrap().contains("--set expert-vram=2 "), "{:?}", launches(&l));
// The game ends but a video player now holds 1000 MiB more: the normal stage reads that.
others.store(1460 + 1000, Ordering::SeqCst);
llm.switch(&s, Stage::Normal, true).await.unwrap();
llm.switch(&s, Stage::Normal, true, false).await.unwrap();
let expected = crate::placement::normal(16303, 1460 + 1000, &crate::placement::Probe { context: 32768, parallel: 1,
gpu: "GPU-test".into(), model_mib: 9874, staging_gib: 1.1 }, 1024);
assert!(expected < 1.5);
@@ -1069,7 +1071,7 @@ pub(crate) mod tests {
llm.game_at_start(true);
let s = parse_settings(&json!({"llm": {"port": port}})).unwrap();
llm.start(&s).await.unwrap();
assert_eq!(llm.switch(&s, Stage::Gaming, false).await.unwrap(), Stage::Gaming, "not paused");
assert_eq!(llm.switch(&s, Stage::Gaming, false, true).await.unwrap(), Stage::Gaming, "not paused");
let probe16 = crate::placement::Probe { context: 16384, parallel: 1, gpu: "GPU-test".into(), model_mib: 9874, staging_gib: 1.1 };
let expected = crate::placement::normal(16303, 1460 + 2500, &probe16, 1024);
assert!(launches(&l).last().unwrap().contains(&format!("--set expert-vram={expected} ")), "{:?}", launches(&l));
@@ -1087,7 +1089,7 @@ pub(crate) mod tests {
let s = parse_settings(&json!({"llm": {"port": port}})).unwrap();
llm.start(&s).await.unwrap();
others.store(1460 + 4000 + 4500, Ordering::SeqCst);
llm.switch(&s, Stage::Gaming, false).await.unwrap();
llm.switch(&s, Stage::Gaming, false, true).await.unwrap();
let probe16 = crate::placement::Probe { context: 16384, parallel: 1, gpu: "GPU-test".into(), model_mib: 9874, staging_gib: 1.1 };
let expected = crate::placement::game(24576, 1460, &probe16, 8500).expert_gib;
assert_ne!(expected, crate::placement::game(24576, 1460, &probe16, 8192).expert_gib, "the test tells them apart");
@@ -1109,7 +1111,7 @@ pub(crate) mod tests {
let llm = llm.with_gpu(Arc::new(move || {
(r2.fetch_add(1, Ordering::SeqCst) == 0).then(|| Gpu { id: "GPU-test".into(), used_mib: 1460, total_mib: 16303 })
}));
assert_eq!(llm.switch(&s, Stage::Gaming, false).await.unwrap(), Stage::Gaming);
assert_eq!(llm.switch(&s, Stage::Gaming, false, true).await.unwrap(), Stage::Gaming);
assert!(launches(&l).last().unwrap().contains("--set expert-vram=0 "), "{:?}", launches(&l));
assert_eq!(llm.stage_view(), json!({"stage": "gaming", "gameRoomMib": null}));
}
@@ -1128,7 +1130,7 @@ pub(crate) mod tests {
}));
let s = parse_settings(&json!({"llm": {"port": port}})).unwrap();
llm.start(&s).await.unwrap();
assert_eq!(llm.switch(&s, Stage::Gaming, false).await.unwrap(), Stage::Paused);
assert_eq!(llm.switch(&s, Stage::Gaming, false, true).await.unwrap(), Stage::Paused);
assert_eq!(llm.status().state, LlmState::Off);
assert_eq!(llm.stage_view(), json!({"stage": "paused", "gameRoomMib": null}));
let c = std::fs::read_to_string(l.data.join("calls")).unwrap();