Reap a killed nvidia-smi on a thread of its own, tell why it gave no output, restore the test budget on failure, clearer cap docs

This commit is contained in:
vangel76 committed 2026-11-18 17:38:13 +01:00
1 parent 84b8f58fd0
commit 34a1c88930
4 files changed
+118 -43

No files matched your search

+3 -2
View File
@@ -1679,8 +1679,9 @@ impl GpuContext {
}
/// Bytes of uploaded layer textures the context keeps (by default 3/8 of the card's memory
/// when known, 1536 MiB otherwise; a positive `EFFECTCRAFT_GPU_MEMCAP_MB` lowers that default to
/// half the cap, not below 1536 MiB; a positive `EFFECTCRAFT_GPU_UPLOAD_MB` sets it and is not
/// when known, 1536 MiB otherwise; a positive `EFFECTCRAFT_GPU_MEMCAP_MB` can only lower that
/// default: to at most the larger of half the cap and 1536 MiB, so a smaller default, like the
/// 768 MiB of a 2 GiB card, stays; a positive `EFFECTCRAFT_GPU_UPLOAD_MB` sets it and is not
/// lowered by the cap; see `memcap`). An application
/// can raise it (static layers of a big frame upload once) or lower it (a small GPU); the
/// least recently used textures go first.
+92 -36
View File
@@ -76,8 +76,9 @@ impl MemCap {
fn from_envs(cap_var: Option<&str>, upload_var: Option<&str>, device_type: wgpu::DeviceType, vram: Option<u64>) -> MemCap {
let mut upload = upload_budget(upload_var, vram);
// An explicit cap is the user's: it does not grow with the cache, so a default budget
// is lowered to half of it (never below the former fixed 1.5 GiB); an explicit one stays.
// An explicit cap is the user's: it does not grow with the cache, so a default budget is
// kept at most at the larger of half the cap and the former fixed 1.5 GiB (it is never
// raised, and a smaller default stays as it is); an explicit budget stays.
// A value that is not a number is treated as unset (with a warning), not as a cap.
let cap = match explicit_cap(cap_var) {
Some(cap) => {
@@ -364,24 +365,37 @@ fn nvidia_smi_output() -> Option<&'static str> {
OUTPUT
.get_or_init(|| {
let out = run_with_timeout(std::ffi::OsStr::new("nvidia-smi"), &NVIDIA_SMI_ARGS, NVIDIA_SMI_TIMEOUT);
if out.is_none() {
log::debug!(target: "effectcraft_gpu", "nvidia-smi is missing, failed or took longer than {NVIDIA_SMI_TIMEOUT:?}: video memory unknown");
if let Err(why) = &out {
log::debug!(target: "effectcraft_gpu", "nvidia-smi gave no answer ({why:?}, timeout {NVIDIA_SMI_TIMEOUT:?}): video memory unknown");
}
out
out.ok()
})
.as_deref()
}
/// How long a killed program may take to exit before it is left behind unreaped.
/// How long the caller waits for a killed program to exit before a thread of its own reaps it.
#[cfg(not(target_arch = "wasm32"))]
const REAP_TIMEOUT: Duration = Duration::from_millis(200);
/// Why [`run_with_timeout`] has no output.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, PartialEq, Eq)]
enum NoOutput {
/// The program could not start, exited with an error, or its output could not be read.
Failed,
/// The program was still running at the timeout (it is killed).
TimedOut,
/// The program exited successfully, but its output was still open at the timeout (a process
/// it left behind holds it).
OutputOpen,
}
/// Runs `program` with `args` and returns its standard output when it exits successfully and its
/// output ends within `timeout`, without a console window on Windows. The output is read on a
/// thread of its own, so a process the program left behind holding the pipe open cannot hold the
/// caller past the timeout. A program still running at the timeout is killed (see [`stop`]).
#[cfg(not(target_arch = "wasm32"))]
fn run_with_timeout(program: &std::ffi::OsStr, args: &[&str], timeout: Duration) -> Option<String> {
fn run_with_timeout(program: &std::ffi::OsStr, args: &[&str], timeout: Duration) -> Result<String, NoOutput> {
use std::io::Read;
use std::process::{Command, Stdio};
let mut cmd = Command::new(program);
@@ -393,7 +407,7 @@ fn run_with_timeout(program: &std::ffi::OsStr, args: &[&str], timeout: Duration)
cmd.creation_flags(CREATE_NO_WINDOW);
}
let start = std::time::Instant::now();
let mut child = cmd.spawn().ok()?;
let mut child = cmd.spawn().map_err(|_| NoOutput::Failed)?;
let (tx, rx) = std::sync::mpsc::channel();
let reader = child.stdout.take().and_then(|mut stdout| {
std::thread::Builder::new()
@@ -405,39 +419,59 @@ fn run_with_timeout(program: &std::ffi::OsStr, args: &[&str], timeout: Duration)
.ok()
});
if reader.is_none() {
stop(&mut child);
return None;
let _ = stop(child);
return Err(NoOutput::Failed);
}
loop {
match child.try_wait() {
Ok(Some(status)) if status.success() => break,
Ok(None) if start.elapsed() < timeout => std::thread::sleep(Duration::from_millis(10)),
Ok(Some(_)) => return None,
_ => {
stop(&mut child);
return None;
Ok(Some(_)) => return Err(NoOutput::Failed),
Ok(None) => {
let _ = stop(child);
return Err(NoOutput::TimedOut);
}
Err(_) => {
let _ = stop(child);
return Err(NoOutput::Failed);
}
}
}
rx.recv_timeout(timeout.saturating_sub(start.elapsed())).ok().flatten()
match rx.recv_timeout(timeout.saturating_sub(start.elapsed())) {
Ok(Some(out)) => Ok(out),
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Err(NoOutput::OutputOpen),
Ok(None) | Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => Err(NoOutput::Failed),
}
}
/// Kills `child` and collects its exit status, waiting at most [`REAP_TIMEOUT`] for it to exit
/// (a process stuck in the driver may not die at once, and the caller must not wait for that).
/// The exit status, or `None` when the program has not exited by then.
/// Kills `child` and collects its exit status, so it is not left behind as a zombie. The caller
/// waits at most [`REAP_TIMEOUT`] for that (a process stuck in the driver may not die at once);
/// after that a thread of its own waits for the program (see [`reap_later`]). The exit status
/// when the program exited within [`REAP_TIMEOUT`], otherwise the reaping thread.
#[cfg(not(target_arch = "wasm32"))]
fn stop(child: &mut std::process::Child) -> Option<std::process::ExitStatus> {
fn stop(mut child: std::process::Child) -> Result<std::process::ExitStatus, Option<Reaper>> {
let _ = child.kill();
let start = std::time::Instant::now();
loop {
match child.try_wait() {
Ok(Some(status)) => return Some(status),
Ok(Some(status)) => return Ok(status),
Ok(None) if start.elapsed() < REAP_TIMEOUT => std::thread::sleep(Duration::from_millis(5)),
_ => return None,
_ => return Err(reap_later(child)),
}
}
}
/// A thread that waits for a program to exit; it gives the exit status.
#[cfg(not(target_arch = "wasm32"))]
type Reaper = std::thread::JoinHandle<Option<std::process::ExitStatus>>;
/// Waits for `child` to exit on a thread of its own, which collects its exit status. `None` when
/// the thread cannot start: the child is then dropped (left unreaped, as it cannot be helped).
#[cfg(not(target_arch = "wasm32"))]
fn reap_later(mut child: std::process::Child) -> Option<Reaper> {
std::thread::Builder::new().name("nvidia-smi reaper".into()).spawn(move || child.wait().ok()).ok()
}
/// The memory in bytes of the NVIDIA GPU in `nvidia-smi` output (lines of `bus id, device id,
/// memory in MiB`, like `00000000:01:00.0, 0x250310DE, 12288`) that is the adapter: the line at
/// PCI address `bus` (wgpu's `device_pci_bus_id`, `0000:01:00.0`), otherwise the smallest among
@@ -619,6 +653,9 @@ mod tests {
assert_eq!((m.cap(), m.upload_budget()), (4 * GIB, 2 * GIB as usize));
let m = MemCap::from_envs(Some("8"), None, d, Some(32 * GIB));
assert_eq!((m.cap(), m.upload_budget()), (8 * MIB, 3 * GIB as usize / 2));
// The cap only lowers: a 2 GiB card's 768 MiB default stays under a 2 GiB cap.
let m = MemCap::from_envs(Some("2048"), None, d, Some(2 * GIB));
assert_eq!((m.cap(), m.upload_budget()), (2 * GIB, 768 * MIB as usize));
let m = MemCap::from_envs(Some("0"), None, d, Some(32 * GIB));
assert_eq!((m.cap(), m.upload_budget()), (0, 12 * GIB as usize));
let m = MemCap::from_envs(None, Some("9000"), d, Some(32 * GIB));
@@ -721,16 +758,16 @@ mod tests {
#[test]
fn a_program_that_fails_or_hangs_gives_no_output() {
let missing = std::ffi::OsStr::new("effectcraft-no-such-program-3731");
assert_eq!(run_with_timeout(missing, &NVIDIA_SMI_ARGS, Duration::from_secs(5)), None);
assert_eq!(run_with_timeout(missing, &NVIDIA_SMI_ARGS, Duration::from_secs(5)), Err(NoOutput::Failed));
let me = std::env::current_exe().unwrap();
// This test binary stands in for the program: listing tests succeeds and prints them,
// an unknown flag exits with an error, and the sleeping test outlasts the timeout.
let listed = run_with_timeout(me.as_os_str(), &["--list", "--exact", "memcap::tests::stand_in_for_a_hanging_program"], Duration::from_secs(20));
assert!(listed.is_some_and(|out| out.contains("stand_in_for_a_hanging_program")));
assert_eq!(run_with_timeout(me.as_os_str(), &["--no-such-flag-3731"], Duration::from_secs(20)), None);
assert!(listed.is_ok_and(|out| out.contains("stand_in_for_a_hanging_program")));
assert_eq!(run_with_timeout(me.as_os_str(), &["--no-such-flag-3731"], Duration::from_secs(20)), Err(NoOutput::Failed));
let start = std::time::Instant::now();
let hung = run_with_timeout(me.as_os_str(), &["--exact", "--ignored", "memcap::tests::stand_in_for_a_hanging_program"], Duration::from_millis(500));
assert_eq!(hung, None);
assert_eq!(hung, Err(NoOutput::TimedOut));
// Well under the stand-in's 5 s: the caller did not wait for it to finish.
assert!(start.elapsed() < Duration::from_secs(3), "waited {:?}", start.elapsed());
}
@@ -757,26 +794,45 @@ mod tests {
// The timeout leaves the stand-in time to start and exit; the left process sleeps 5 s.
let out =
run_with_timeout(me.as_os_str(), &["--exact", "--ignored", "memcap::tests::stand_in_that_leaves_its_output_open"], Duration::from_millis(2500));
assert_eq!(out, None);
// Not a stand-in that failed to start its left process (that exits with an error).
assert_eq!(out, Err(NoOutput::OutputOpen));
assert!(start.elapsed() < Duration::from_millis(4500), "waited {:?}", start.elapsed());
}
/// A program killed at the timeout is reaped (its exit status collected), within a bounded wait.
/// The hanging stand-in, its output discarded.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_killed_program_is_reaped() {
fn hanging_stand_in() -> std::process::Child {
let me = std::env::current_exe().unwrap();
let mut child = std::process::Command::new(me)
std::process::Command::new(me)
.args(["--exact", "--ignored", "memcap::tests::stand_in_for_a_hanging_program"])
.stdout(std::process::Stdio::null())
.spawn()
.unwrap();
.unwrap()
}
/// A program killed at the timeout is reaped (its exit status collected): by the caller within
/// `REAP_TIMEOUT`, or else by a thread of its own, never left behind.
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_killed_program_is_reaped() {
let start = std::time::Instant::now();
// A loaded machine may take longer than one `REAP_TIMEOUT` to end the process.
let mut status = stop(&mut child);
while status.is_none() && start.elapsed() < Duration::from_secs(3) {
status = stop(&mut child);
}
let status = match stop(hanging_stand_in()) {
Ok(status) => Some(status),
Err(reaper) => reaper.expect("the reaper thread starts").join().unwrap(),
};
assert!(status.is_some_and(|s| !s.success()), "{status:?}");
assert!(start.elapsed() < Duration::from_secs(4), "waited {:?}", start.elapsed());
}
/// A killed program that has not exited when the caller stops waiting is reaped by a thread of
/// its own, which collects its exit status (#3737 review).
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_program_that_exits_late_is_reaped_by_a_thread() {
let mut child = hanging_stand_in();
let _ = child.kill();
let start = std::time::Instant::now();
let status = reap_later(child).expect("the reaper thread starts").join().unwrap();
assert!(status.is_some_and(|s| !s.success()), "{status:?}");
assert!(start.elapsed() < Duration::from_secs(3), "waited {:?}", start.elapsed());
}
+20 -3
View File
@@ -1533,8 +1533,16 @@ fn upload_budget_bounds_the_layer_textures_kept() {
let img = g.render(&r, s.cid, Tick::from_seconds_f64(k as f64 / 30.0)).expect("the GPU renders the frame");
(img.data, g.ctx.transfer_stats().upload_bytes - before)
};
// The shared context's budget comes back when the test ends, also when a frame fails (#3737
// review): the GPU lock is held until this thread ends, so no other test sees the change.
struct Restore<'a>(&'a crate::GpuContext, usize);
impl Drop for Restore<'_> {
fn drop(&mut self) {
self.0.set_upload_budget(self.1);
}
}
let _restore = Restore(&g.ctx, g.ctx.upload_budget());
// A budget that holds both 640 KB footprints, whatever EFFECTCRAFT_GPU_UPLOAD_MB says.
let inherited = g.ctx.upload_budget();
g.ctx.set_upload_budget(64 << 20);
let fits = effectcraft_render::cache::LayerCache::new(64 << 20);
let (a, _) = frame(&fits, 0);
@@ -1544,7 +1552,6 @@ fn upload_budget_bounds_the_layer_textures_kept() {
let tight = effectcraft_render::cache::LayerCache::new(64 << 20);
let _ = frame(&tight, 0);
let (c, up) = frame(&tight, 1);
g.ctx.set_upload_budget(inherited);
assert_eq!(up_fits, 0, "static layers re-uploaded within a budget that fits them");
assert!(up > 0, "over budget, the footprints must upload again");
assert!(a == b && b == c, "the upload budget changed the frame");
@@ -1558,10 +1565,13 @@ fn upload_budget_bounds_the_layer_textures_kept() {
#[test]
fn upload_budget_comes_from_the_memory_rule() {
const ENV: &str = "EFFECTCRAFT_GPU_UPLOAD_TEST_CHILD";
// What the child prints once its checks passed: a child without a GPU exits successfully too.
const CHECKED: &str = "upload budget child: checked";
let tight = std::env::var_os(ENV).is_some();
if !tight && std::env::var_os("EFFECTCRAFT_GPU_UPLOAD_MB").is_some() {
return; // the caller set a budget of their own
}
let mut child_checked = None;
if !tight {
// The child uses the GPU: tests of this process wait until it is done (#3737 review).
hold_gpu_lock();
@@ -1572,9 +1582,15 @@ fn upload_budget_comes_from_the_memory_rule() {
.args(["--exact", "tests::upload_budget_comes_from_the_memory_rule", "--nocapture"])
.output()
.expect("spawn child test process");
assert!(out.status.success(), "child test failed:\n{}\n{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
let (stdout, stderr) = (String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr));
assert!(out.status.success(), "child test failed:\n{stdout}\n{stderr}");
child_checked = Some((stdout.contains(CHECKED), format!("{stdout}\n{stderr}")));
}
let Some(g) = gpu() else { return };
// This process has a GPU, so the child must have run its checks, not skipped them.
if let Some((checked, output)) = child_checked {
assert!(checked, "the child did not run its checks:\n{output}");
}
let mut s = Scene::new(BitDepth::Bpc8);
for i in 0..3 {
let mut l = s.footage(600, 400); // 3.8 MB as RGBA f32 each
@@ -1595,6 +1611,7 @@ fn upload_budget_comes_from_the_memory_rule() {
if tight {
assert_eq!(g.ctx.upload_budget(), 1 << 20, "EFFECTCRAFT_GPU_UPLOAD_MB=1");
assert!(up > 0, "layers over the budget must upload again");
println!("{CHECKED}");
} else {
assert_eq!(g.ctx.upload_budget(), g.ctx.upload_budget_from_rule());
assert_eq!(up, 0, "layers within the budget re-uploaded");
+3 -2
View File
@@ -101,8 +101,9 @@ Commands (CLI, MCP, control channel):
textures of uploaded layers: 1.5 GiB, or three eighths of the card's memory where it is
known: on Linux with amdgpu, on Windows, and on NVIDIA cards through `nvidia-smi`; the
default cap grows with it, never above
three quarters of the card, and a cap above 0 set with `EFFECTCRAFT_GPU_MEMCAP_MB` lowers the
default budget to half that cap, not below 1.5 GiB; `EFFECTCRAFT_GPU_UPLOAD_MB` sets the budget
three quarters of the card, and a cap above 0 set with `EFFECTCRAFT_GPU_MEMCAP_MB` can only
lower the default budget: to at most the larger of half that cap and 1.5 GiB, so a smaller
default, like the 768 MiB of a 2 GiB card, stays; `EFFECTCRAFT_GPU_UPLOAD_MB` sets the budget
in whole MiB, 0 = default, and a budget set there is kept). It
renders one frame at a time where the memory can't be measured (Metal, WebGPU, GL, or the
cap turned off) and once frames have had to wait for memory. The render log names the count.