A bar that has been filled is not taken back when the GPU dies
Browse files at this commit197d1e2756fbe434b7c52a9ea848cdaaec3c703eParent: 13b8d93- fix
- a bar that has been filled is not taken back when the GPU dies
5 changed files +351 -26
- crates/app/src/importing.rs +14 -2
- crates/app/src/tests/cards.rs +34 -0
- crates/engine/src/export.rs +56 -9
- crates/engine/src/hwproc.rs +71 -12
- crates/engine/tests/export.rs +176 -3
crates/app/src/importing.rsmodified+14 -2
| @@ -407,9 +407,21 @@ pub(crate) fn eta_secs(marks: &[(f32, f32)], elapsed: f32, progress: f32) -> Opt | |||
| 407 | // alone reads well: raw window rate spikes eightfold on either edge of a | 407 | // alone reads well: raw window rate spikes eightfold on either edge of a |
| 408 | // stall, and the run average alone never notices a segment change. | 408 | // stall, and the run average alone never notices a segment change. |
| 409 | let overall = progress / elapsed; | 409 | let overall = progress / elapsed; |
| 410 | + // Never below zero, whatever the marks say. A bar that went *backwards* | ||
| 411 | + // between two marks -- an export written again from the start after its | ||
| 412 | + // hardware encoder died -- made this negative, which cancelled the run's | ||
| 413 | + // own rate and left an answer that read "~0:00 left" over six minutes of | ||
| 414 | + // software encoding. The bar is monotone at the source now | ||
| 415 | + // (`engine::export`'s `fetch_max`), and a rate the other way is still not | ||
| 416 | + // an estimate: a window that has gone nowhere leans on the whole run's | ||
| 417 | + // rate, which is exactly what a stall does. | ||
| 410 | let recent = marks | 418 | let recent = marks |
| 411 | .first() | 419 | .first() |
| 412 | .filter(|&&(t, _)| elapsed - t >= ETA_SPAN) | 420 | .filter(|&&(t, _)| elapsed - t >= ETA_SPAN) |
| 413 | - .map_or(overall, |&(t, p)| (progress - p) / (elapsed - t)); | 421 | + .map_or(overall, |&(t, p)| ((progress - p) / (elapsed - t)).max(0.)); |
| 414 | - Some(2. * (1. - progress) / (recent + overall)) | 422 | + // Both rates at once can only be zero where nothing has happened at all, |
| 423 | + // which the guards above already answer with "estimating…" -- said in | ||
| 424 | + // arithmetic as well, because the alternative is an infinite division | ||
| 425 | + // printed as a clock. | ||
| 426 | + Some(2. * (1. - progress) / (recent + overall)).filter(|left| left.is_finite()) | ||
| 415 | } | 427 | } |
crates/app/src/tests/cards.rsmodified+34 -0
| @@ -1387,3 +1387,37 @@ fn the_export_estimate_rides_out_a_stall_and_converges() { | |||
| 1387 | assert_eq!(clock(114.), "1:54"); | 1387 | assert_eq!(clock(114.), "1:54"); |
| 1388 | assert_eq!(clock(-1.), "0:00"); | 1388 | assert_eq!(clock(-1.), "0:00"); |
| 1389 | } | 1389 | } |
| 1390 | + | ||
| 1391 | +/// The two shapes that made the countdown lie rather than guess: a bar that | ||
| 1392 | +/// went backwards, and a division that could reach infinity. The engine no | ||
| 1393 | +/// longer pulls its bar back (`export`'s `fetch_max`), which is why this asks | ||
| 1394 | +/// the estimator directly -- it is handed the marks a re-run used to leave. | ||
| 1395 | +#[test] | ||
| 1396 | +fn the_export_estimate_answers_a_bar_that_went_backwards_with_a_guess() { | ||
| 1397 | + // 93% at 400 s, then the hardware encoder dies and the mark stands while | ||
| 1398 | + // the software one writes the film again from the first frame. | ||
| 1399 | + let marks = vec![(392., 0.93), (396., 0.93), (400., 0.93)]; | ||
| 1400 | + let stalled = eta_secs(&marks, 400., 0.93).expect("a stall is still estimated"); | ||
| 1401 | + assert!(stalled > 0., "a running export has time left: {stalled}"); | ||
| 1402 | + // The old shape: the same marks against a bar that had been put back to | ||
| 1403 | + // zero. The rate cannot be negative, so what is left cannot be either -- | ||
| 1404 | + // and it is never the "~0:00 left" that read as an export about to finish. | ||
| 1405 | + let backwards = eta_secs(&marks, 400., 0.05).expect("a backward bar still estimates"); | ||
| 1406 | + assert!( | ||
| 1407 | + backwards >= stalled, | ||
| 1408 | + "a bar at 5% claimed less left than one at 93%: {backwards} vs {stalled}" | ||
| 1409 | + ); | ||
| 1410 | + assert!(backwards > 1., "the countdown read as good as finished"); | ||
| 1411 | + // Nothing it can be handed prints as an infinite clock. | ||
| 1412 | + for (marks, elapsed, progress) in [ | ||
| 1413 | + (vec![(0., 0.)], f32::MAX, f32::MIN_POSITIVE), | ||
| 1414 | + (vec![(0., f32::MAX)], 3., 0.5), | ||
| 1415 | + (Vec::new(), f32::MAX, 0.5), | ||
| 1416 | + ] { | ||
| 1417 | + let left = eta_secs(&marks, elapsed, progress); | ||
| 1418 | + assert!( | ||
| 1419 | + left.is_none_or(f32::is_finite), | ||
| 1420 | + "{left:?} would be printed as a clock" | ||
| 1421 | + ); | ||
| 1422 | + } | ||
| 1423 | +} | ||
crates/engine/src/export.rsmodified+56 -9
| @@ -380,7 +380,10 @@ struct Shared { | |||
| 380 | /// backwards -- 5,458 backward steps over one film export before this rule | 380 | /// backwards -- 5,458 backward steps over one film export before this rule |
| 381 | /// was written down. A `store` is only correct where nothing else can be | 381 | /// was written down. A `store` is only correct where nothing else can be |
| 382 | /// publishing at all, which now means the terminal [`PROGRESS_SCALE`] and | 382 | /// publishing at all, which now means the terminal [`PROGRESS_SCALE`] and |
| 383 | - /// the audio-only export ([`run_audio`]) that has no second thread. | 383 | + /// the audio-only export ([`run_audio`]) that has no second thread. The |
| 384 | + /// rule outlives one attempt at the file: an export rerun in software | ||
| 385 | + /// because the hardware seat died writes this same atomic, and the bar | ||
| 386 | + /// holds where the dead seat left it rather than starting again. | ||
| 384 | progress: AtomicU32, | 387 | progress: AtomicU32, |
| 385 | cancel: AtomicBool, | 388 | cancel: AtomicBool, |
| 386 | finished: AtomicBool, | 389 | finished: AtomicBool, |
| @@ -513,7 +516,13 @@ pub fn start( | |||
| 513 | { | 516 | { |
| 514 | eprintln!("export video: {e}; writing it again in software"); | 517 | eprintln!("export video: {e}; writing it again in software"); |
| 515 | let _ = std::fs::remove_file(&part); | 518 | let _ = std::fs::remove_file(&part); |
| 516 | - worker.progress.store(0, Ordering::Relaxed); | 519 | + // The bar is *not* put back to zero: the rerun writes |
| 520 | + // it with `fetch_max` like everything else | ||
| 521 | + // ([`Shared::progress`]), so it holds where the dead | ||
| 522 | + // seat left it and moves again once the software | ||
| 523 | + // encoder passes that mark. A person whose GPU has just | ||
| 524 | + // died is the last one who should also watch the bar | ||
| 525 | + // run backwards. | ||
| 517 | let mut software = settings.clone(); | 526 | let mut software = settings.clone(); |
| 518 | software.seat = EncoderSeat::Software; | 527 | software.seat = EncoderSeat::Software; |
| 519 | run(&project, &meta, &part, &worker, &software) | 528 | run(&project, &meta, &part, &worker, &software) |
| @@ -1619,7 +1628,7 @@ impl CopyPlan { | |||
| 1619 | done += 1; | 1628 | done += 1; |
| 1620 | shared | 1629 | shared |
| 1621 | .progress | 1630 | .progress |
| 1622 | - .store(picture_progress(done, total), Ordering::Relaxed); | 1631 | + .fetch_max(picture_progress(done, total), Ordering::Relaxed); |
| 1623 | } | 1632 | } |
| 1624 | } | 1633 | } |
| 1625 | // Everything up to here is still cancellable, exactly as the encoded | 1634 | // Everything up to here is still cancellable, exactly as the encoded |
| @@ -2045,6 +2054,45 @@ fn window_fidelity( | |||
| 2045 | /// encodes and joins where the file it is writing needs the track. | 2054 | /// encodes and joins where the file it is writing needs the track. |
| 2046 | type AudioJob = thread::JoinHandle<crate::Result<Option<ExportAudio>>>; | 2055 | type AudioJob = thread::JoinHandle<crate::Result<Option<ExportAudio>>>; |
| 2047 | 2056 | ||
| 2057 | +/// The pass while [`run`] holds it, joined however that function leaves -- | ||
| 2058 | +/// including through a `?`. | ||
| 2059 | +/// | ||
| 2060 | +/// A [`JoinHandle`](thread::JoinHandle) that is merely dropped *detaches* its | ||
| 2061 | +/// thread, and the one error that matters here is the hardware encoder's | ||
| 2062 | +/// process dying ([`crate::hwproc`]): the whole export is then written again by | ||
| 2063 | +/// [`start`], which starts an audio pass of its own. The abandoned one would go | ||
| 2064 | +/// on mixing and encoding the same sound beside it -- two passes of a feature | ||
| 2065 | +/// film's audio at once, on the machine that has just lost its GPU. Joined | ||
| 2066 | +/// instead, so an export has one audio pass at a time and none outlives it. | ||
| 2067 | +struct AudioPass(Option<AudioJob>); | ||
| 2068 | + | ||
| 2069 | +impl AudioPass { | ||
| 2070 | + /// Whether the pass has landed, so the picture loop may collect it without | ||
| 2071 | + /// waiting. | ||
| 2072 | + fn is_finished(&self) -> bool { | ||
| 2073 | + self.0.as_ref().is_some_and(thread::JoinHandle::is_finished) | ||
| 2074 | + } | ||
| 2075 | + | ||
| 2076 | + /// Whether a pass is still to be collected -- which is what "the sound is | ||
| 2077 | + /// still being worked out" means on the encoder line. | ||
| 2078 | + fn running(&self) -> bool { | ||
| 2079 | + self.0.is_some() | ||
| 2080 | + } | ||
| 2081 | + | ||
| 2082 | + /// The handle, for the one caller that is about to join it itself. | ||
| 2083 | + fn take(&mut self) -> Option<AudioJob> { | ||
| 2084 | + self.0.take() | ||
| 2085 | + } | ||
| 2086 | +} | ||
| 2087 | + | ||
| 2088 | +impl Drop for AudioPass { | ||
| 2089 | + fn drop(&mut self) { | ||
| 2090 | + if let Some(job) = self.0.take() { | ||
| 2091 | + let _ = job.join(); | ||
| 2092 | + } | ||
| 2093 | + } | ||
| 2094 | +} | ||
| 2095 | + | ||
| 2048 | /// The pass's answer, or the failure that is the export's failure. A panicked | 2096 | /// The pass's answer, or the failure that is the export's failure. A panicked |
| 2049 | /// audio thread reads as an error rather than as silence: an export that wrote | 2097 | /// audio thread reads as an error rather than as silence: an export that wrote |
| 2050 | /// its picture and quietly no sound is the one outcome this must not have. | 2098 | /// its picture and quietly no sound is the one outcome this must not have. |
| @@ -2162,7 +2210,9 @@ fn run( | |||
| 2162 | }; | 2210 | }; |
| 2163 | // A Matroska file (and a copied one, whose muxer is the same) collects it | 2211 | // A Matroska file (and a copied one, whose muxer is the same) collects it |
| 2164 | // here; an mp4 leaves it running and collects it under the picture below. | 2212 | // here; an mp4 leaves it running and collects it under the picture below. |
| 2165 | - let mut audio_job = Some(audio_job); | 2213 | + // Held in a guard from here on, so no way out of this function leaves the |
| 2214 | + // pass running behind it ([`AudioPass`]). | ||
| 2215 | + let mut audio_job = AudioPass(Some(audio_job)); | ||
| 2166 | // The condition is the *format*, and the copied-picture path below relies | 2216 | // The condition is the *format*, and the copied-picture path below relies |
| 2167 | // on that covering it too: `plan` hands its packets straight to a Matroska | 2217 | // on that covering it too: `plan` hands its packets straight to a Matroska |
| 2168 | // muxer, so it may only exist where this has already collected them -- | 2218 | // muxer, so it may only exist where this has already collected them -- |
| @@ -2280,7 +2330,7 @@ fn run( | |||
| 2280 | // it replaced -- and the sound says whether it was copied or encoded, which | 2330 | // it replaced -- and the sound says whether it was copied or encoded, which |
| 2281 | // only `copy_audio` knows and, on the overlapped path, does not know yet. | 2331 | // only `copy_audio` knows and, on the overlapped path, does not know yet. |
| 2282 | let seat = encoder.label(); | 2332 | let seat = encoder.label(); |
| 2283 | - publish_seats(shared, seat, project, settings, sound, opus, audio_job.is_some()); | 2333 | + publish_seats(shared, seat, project, settings, sound, opus, audio_job.running()); |
| 2284 | // When the picture started, so the stage line below can say where under it | 2334 | // When the picture started, so the stage line below can say where under it |
| 2285 | // the sound landed -- the one number that says whether the two passes | 2335 | // the sound landed -- the one number that says whether the two passes |
| 2286 | // really overlapped, in the same voice as `encode_audio`'s own timers. | 2336 | // really overlapped, in the same voice as `encode_audio`'s own timers. |
| @@ -2391,10 +2441,7 @@ fn run( | |||
| 2391 | // encoded, and an overlapped pass that reported only at the end | 2441 | // encoded, and an overlapped pass that reported only at the end |
| 2392 | // would spend the whole encode first. Also where its measured | 2442 | // would spend the whole encode first. Also where its measured |
| 2393 | // codec reaches the line on screen. | 2443 | // codec reaches the line on screen. |
| 2394 | - if audio_job | 2444 | + if audio_job.is_finished() { |
| 2395 | - .as_ref() | ||
| 2396 | - .is_some_and(thread::JoinHandle::is_finished) | ||
| 2397 | - { | ||
| 2398 | late = join_audio(audio_job.take().expect("just seen to be there"))?; | 2445 | late = join_audio(audio_job.take().expect("just seen to be there"))?; |
| 2399 | sound = late.as_ref().map(|track| track.copied); | 2446 | sound = late.as_ref().map(|track| track.copied); |
| 2400 | opus = late | 2447 | opus = late |
crates/engine/src/hwproc.rsmodified+71 -12
| @@ -13,8 +13,8 @@ | |||
| 13 | //! handle on a child running `hw-encode-child`, which is the one process that | 13 | //! handle on a child running `hw-encode-child`, which is the one process that |
| 14 | //! `dlopen`s the plugin and therefore the only one a driver may kill. When it | 14 | //! `dlopen`s the plugin and therefore the only one a driver may kill. When it |
| 15 | //! dies -- by `abort()`, by any signal, or by going quiet past | 15 | //! dies -- by `abort()`, by any signal, or by going quiet past |
| 16 | -//! [`Remote::timeout`] -- the parent sees a closed socket or a timed-out read, | 16 | +//! [`Remote::deadline`] -- the parent sees a closed socket or a timed-out read, |
| 17 | -//! reaps the child, and reports an error carrying [`HW_LOST`]. The export then | 17 | +//! reaps the child, and reports a [`Lost`]. The export then |
| 18 | //! runs again on the software encoder ([`crate::export::start`]), or refuses in | 18 | //! runs again on the software encoder ([`crate::export::start`]), or refuses in |
| 19 | //! words where a person picked the GPU by name. | 19 | //! words where a person picked the GPU by name. |
| 20 | //! | 20 | //! |
| @@ -36,17 +36,32 @@ use std::time::Duration; | |||
| 36 | use crate::colorspace::{ColorDescription, Matrix, Transfer}; | 36 | use crate::colorspace::{ColorDescription, Matrix, Transfer}; |
| 37 | use crate::hw::VhDma; | 37 | use crate::hw::VhDma; |
| 38 | 38 | ||
| 39 | -/// The words every "the child is gone" error carries, and the only thing | 39 | +/// The words every "the child is gone" error carries, for the person reading |
| 40 | -/// [`crate::export`] matches on to decide that the software encoder should have | 40 | +/// the log; what *decides* anything is [`Lost`], never this text. |
| 41 | -/// another go at the file. A message, not an error kind, because | ||
| 42 | -/// [`crate::Error`] is a boxed `dyn Error` and a caller two crates away reads | ||
| 43 | -/// its text. | ||
| 44 | pub const HW_LOST: &str = "the hardware encoder process"; | 41 | pub const HW_LOST: &str = "the hardware encoder process"; |
| 45 | 42 | ||
| 43 | +/// The error a dead child carries. | ||
| 44 | +/// | ||
| 45 | +/// A type and not a phrase. [`is_lost`] is what commits a whole export to being | ||
| 46 | +/// written again from the first frame ([`crate::export::start`]), and matching | ||
| 47 | +/// on words handed that decision to any future error whose message happened to | ||
| 48 | +/// mention the child -- a silent re-encode of a feature film, caused by a | ||
| 49 | +/// wording. Nothing outside this module can make one. | ||
| 50 | +#[derive(Debug)] | ||
| 51 | +pub struct Lost(String); | ||
| 52 | + | ||
| 53 | +impl std::fmt::Display for Lost { | ||
| 54 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 55 | + f.write_str(&self.0) | ||
| 56 | + } | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +impl std::error::Error for Lost {} | ||
| 60 | + | ||
| 46 | /// Whether an error is one: a driver that took its process down, rather than a | 61 | /// Whether an error is one: a driver that took its process down, rather than a |
| 47 | /// picture this engine got wrong. | 62 | /// picture this engine got wrong. |
| 48 | pub fn is_lost(error: &crate::Error) -> bool { | 63 | pub fn is_lost(error: &crate::Error) -> bool { |
| 49 | - error.to_string().contains(HW_LOST) | 64 | + error.downcast_ref::<Lost>().is_some() |
| 50 | } | 65 | } |
| 51 | 66 | ||
| 52 | /// The helper's file name; it is built into the same directory as the editor. | 67 | /// The helper's file name; it is built into the same directory as the editor. |
| @@ -407,8 +422,32 @@ pub struct Remote { | |||
| 407 | /// Set the moment the child is known gone, so every later call refuses in | 422 | /// Set the moment the child is known gone, so every later call refuses in |
| 408 | /// the same words instead of blocking on a socket nobody holds. | 423 | /// the same words instead of blocking on a socket nobody holds. |
| 409 | lost: Option<String>, | 424 | lost: Option<String>, |
| 425 | + /// The slowest call this child has answered, and how many it has answered | ||
| 426 | + /// at all: what [`Remote::deadline`] measures its patience against. | ||
| 427 | + slowest: Duration, | ||
| 428 | + answered: u32, | ||
| 410 | } | 429 | } |
| 411 | 430 | ||
| 431 | +/// How many answered calls it takes before the wait stops being the configured | ||
| 432 | +/// ceiling and becomes this encoder's own measure, how much slower than its | ||
| 433 | +/// slowest answer a call may be before it is a wedge, and the least it is ever | ||
| 434 | +/// given. | ||
| 435 | +/// | ||
| 436 | +/// The ceiling has to be generous -- an encoder opening on a 4K frame is not a | ||
| 437 | +/// dead one -- and the export spends every second of it on a child that will | ||
| 438 | +/// never answer again: the file is owed the software encoder and the wait is | ||
| 439 | +/// pure delay before it starts. The window is what shrinks, not the ceiling. | ||
| 440 | +/// Once a child has answered [`CALIBRATE`] calls, the machine has said how long | ||
| 441 | +/// its own frames take, and eight times the slowest of them (never under | ||
| 442 | +/// [`FLOOR`]) contains a wedge in two or three seconds rather than fifteen, | ||
| 443 | +/// while a slow open still gets the whole ceiling and is never killed for | ||
| 444 | +/// being slow. The editor is live throughout either way -- the wait is on the | ||
| 445 | +/// export's own thread and the card goes on repainting -- so what this buys is | ||
| 446 | +/// the delay, not the window. | ||
| 447 | +const CALIBRATE: u32 = 8; | ||
| 448 | +const SLACK: u32 = 8; | ||
| 449 | +const FLOOR: Duration = Duration::from_secs(2); | ||
| 450 | + | ||
| 412 | impl Remote { | 451 | impl Remote { |
| 413 | /// How long the child may take over one call before it is treated as hung. | 452 | /// How long the child may take over one call before it is treated as hung. |
| 414 | /// A wedged ring does not always `abort()` -- it can simply stop -- and a | 453 | /// A wedged ring does not always `abort()` -- it can simply stop -- and a |
| @@ -421,6 +460,17 @@ impl Remote { | |||
| 421 | Duration::from_millis(ms) | 460 | Duration::from_millis(ms) |
| 422 | } | 461 | } |
| 423 | 462 | ||
| 463 | + /// How long the *next* call may take: the ceiling until this child has | ||
| 464 | + /// shown what its frames cost, then its own slowest answer with room to | ||
| 465 | + /// spare ([`CALIBRATE`]). | ||
| 466 | + fn deadline(&self) -> Duration { | ||
| 467 | + let ceiling = Self::timeout(); | ||
| 468 | + match self.answered >= CALIBRATE { | ||
| 469 | + true => ceiling.min((self.slowest * SLACK).max(FLOOR)), | ||
| 470 | + false => ceiling, | ||
| 471 | + } | ||
| 472 | + } | ||
| 473 | + | ||
| 424 | /// Starts a child on this seat. `None` is the plugin's own "no" carried | 474 | /// Starts a child on this seat. `None` is the plugin's own "no" carried |
| 425 | /// across the process boundary -- no driver, no entrypoint, a size refused | 475 | /// across the process boundary -- no driver, no entrypoint, a size refused |
| 426 | /// -- and the caller takes the software encoder exactly as it always did. | 476 | /// -- and the caller takes the software encoder exactly as it always did. |
| @@ -445,6 +495,8 @@ impl Remote { | |||
| 445 | open, | 495 | open, |
| 446 | au: Vec::new(), | 496 | au: Vec::new(), |
| 447 | lost: None, | 497 | lost: None, |
| 498 | + slowest: Duration::ZERO, | ||
| 499 | + answered: 0, | ||
| 448 | }; | 500 | }; |
| 449 | // The child answers once its encoder is open: a byte, and with it the | 501 | // The child answers once its encoder is open: a byte, and with it the |
| 450 | // descriptor of the staging buffer it made for this session's size. | 502 | // descriptor of the staging buffer it made for this session's size. |
| @@ -550,10 +602,18 @@ impl Remote { | |||
| 550 | /// One request and its reply, which is every call above: the child is fed, | 602 | /// One request and its reply, which is every call above: the child is fed, |
| 551 | /// then read, and anything that is not an answer is the child being gone. | 603 | /// then read, and anything that is not an answer is the child being gone. |
| 552 | fn round_trip(&mut self, msg: &[u8], fds: &[RawFd]) -> crate::Result<Option<&[u8]>> { | 604 | fn round_trip(&mut self, msg: &[u8], fds: &[RawFd]) -> crate::Result<Option<&[u8]>> { |
| 605 | + // Set per call, because what it may be is measured from the calls | ||
| 606 | + // before it ([`deadline`]). | ||
| 607 | + let _ = self.sock.set_read_timeout(Some(self.deadline())); | ||
| 608 | + let started = std::time::Instant::now(); | ||
| 553 | self.request(msg, fds)?; | 609 | self.request(msg, fds)?; |
| 554 | let mut au = std::mem::take(&mut self.au); | 610 | let mut au = std::mem::take(&mut self.au); |
| 555 | let read = read_reply(&mut self.sock, &mut au); | 611 | let read = read_reply(&mut self.sock, &mut au); |
| 556 | self.au = au; | 612 | self.au = au; |
| 613 | + if read.is_ok() { | ||
| 614 | + self.slowest = self.slowest.max(started.elapsed()); | ||
| 615 | + self.answered += 1; | ||
| 616 | + } | ||
| 557 | // Every "no access unit" leaves through a `return`, so the borrow of the | 617 | // Every "no access unit" leaves through a `return`, so the borrow of the |
| 558 | // buffer below is the only one alive when it is taken. | 618 | // buffer below is the only one alive when it is taken. |
| 559 | match read { | 619 | match read { |
| @@ -569,7 +629,7 @@ impl Remote { | |||
| 569 | 629 | ||
| 570 | fn request(&mut self, msg: &[u8], fds: &[RawFd]) -> crate::Result<()> { | 630 | fn request(&mut self, msg: &[u8], fds: &[RawFd]) -> crate::Result<()> { |
| 571 | if let Some(gone) = &self.lost { | 631 | if let Some(gone) = &self.lost { |
| 572 | - return Err(gone.clone().into()); | 632 | + return Err(Lost(gone.clone()).into()); |
| 573 | } | 633 | } |
| 574 | match send_msg(&self.sock, msg, fds) { | 634 | match send_msg(&self.sock, msg, fds) { |
| 575 | Ok(()) => Ok(()), | 635 | Ok(()) => Ok(()), |
| @@ -591,10 +651,9 @@ impl Remote { | |||
| 591 | Some(libc::SIGKILL) | None => format!("stopped answering ({why}) and was killed"), | 651 | Some(libc::SIGKILL) | None => format!("stopped answering ({why}) and was killed"), |
| 592 | Some(signal) => format!("was killed by signal {signal}"), | 652 | Some(signal) => format!("was killed by signal {signal}"), |
| 593 | }; | 653 | }; |
| 594 | - let message = | 654 | + let message = format!("{HW_LOST} {how}; the driver took it down and not the editor"); |
| 595 | - format!("{HW_LOST} {how}; the driver took it down and not the editor"); | ||
| 596 | self.lost = Some(message.clone()); | 655 | self.lost = Some(message.clone()); |
| 597 | - message.into() | 656 | + Lost(message).into() |
| 598 | } | 657 | } |
| 599 | } | 658 | } |
| 600 | 659 | ||
crates/engine/tests/export.rsmodified+176 -3
| @@ -1211,6 +1211,20 @@ fn a_proxy_of_real_4k_film_is_made_faster_than_it_plays() { | |||
| 1211 | /// stays pinned to software: it is not what is under test and the fixture must | 1211 | /// stays pinned to software: it is not what is under test and the fixture must |
| 1212 | /// decode the same way it does everywhere else in this file. | 1212 | /// decode the same way it does everywhere else in this file. |
| 1213 | fn injected<T>(abort: Option<&str>, hang: Option<&str>, body: impl FnOnce() -> T) -> T { | 1213 | fn injected<T>(abort: Option<&str>, hang: Option<&str>, body: impl FnOnce() -> T) -> T { |
| 1214 | + // Short, because one of these is waiting out a child that has stopped | ||
| 1215 | + // answering and the wait is the thing under test. | ||
| 1216 | + injected_with(abort, hang, Some("1500"), body) | ||
| 1217 | +} | ||
| 1218 | + | ||
| 1219 | +/// The same, with the wait left to the caller: `None` is what a person's own | ||
| 1220 | +/// editor runs with, which is the one setting none of the tests above ever | ||
| 1221 | +/// exercised. | ||
| 1222 | +fn injected_with<T>( | ||
| 1223 | + abort: Option<&str>, | ||
| 1224 | + hang: Option<&str>, | ||
| 1225 | + timeout_ms: Option<&str>, | ||
| 1226 | + body: impl FnOnce() -> T, | ||
| 1227 | +) -> T { | ||
| 1214 | let _exclusive = SEAT.write().unwrap_or_else(|poisoned| poisoned.into_inner()); | 1228 | let _exclusive = SEAT.write().unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 1215 | let restore = std::env::var_os("VE_SW_ENC"); | 1229 | let restore = std::env::var_os("VE_SW_ENC"); |
| 1216 | unsafe { | 1230 | unsafe { |
| @@ -1218,9 +1232,10 @@ fn injected<T>(abort: Option<&str>, hang: Option<&str>, body: impl FnOnce() -> T | |||
| 1218 | std::env::remove_var("VE_SW_ENC"); | 1232 | std::env::remove_var("VE_SW_ENC"); |
| 1219 | std::env::set_var("VE_HW_TEST_FAKE", "1"); | 1233 | std::env::set_var("VE_HW_TEST_FAKE", "1"); |
| 1220 | std::env::set_var("VE_HW_CHILD_BIN", env!("CARGO_BIN_EXE_hw-encode-child")); | 1234 | std::env::set_var("VE_HW_CHILD_BIN", env!("CARGO_BIN_EXE_hw-encode-child")); |
| 1221 | - // Short, because one of these is waiting out a child that has stopped | 1235 | + match timeout_ms { |
| 1222 | - // answering and the wait is the thing under test. | 1236 | + Some(ms) => std::env::set_var("VE_HW_TIMEOUT_MS", ms), |
| 1223 | - std::env::set_var("VE_HW_TIMEOUT_MS", "1500"); | 1237 | + None => std::env::remove_var("VE_HW_TIMEOUT_MS"), |
| 1238 | + } | ||
| 1224 | match abort { | 1239 | match abort { |
| 1225 | Some(at) => std::env::set_var("VE_HW_TEST_ABORT", at), | 1240 | Some(at) => std::env::set_var("VE_HW_TEST_ABORT", at), |
| 1226 | None => std::env::remove_var("VE_HW_TEST_ABORT"), | 1241 | None => std::env::remove_var("VE_HW_TEST_ABORT"), |
| @@ -1298,6 +1313,164 @@ fn a_hardware_encoder_killed_mid_export_costs_the_file_nothing() { | |||
| 1298 | std::fs::remove_file(&out).unwrap(); | 1313 | std::fs::remove_file(&out).unwrap(); |
| 1299 | } | 1314 | } |
| 1300 | 1315 | ||
| 1316 | +/// ...and the bar over that same death only ever goes forwards. The rerun is a | ||
| 1317 | +/// whole second pass over the timeline, so the one thing a person must not see | ||
| 1318 | +/// is the progress they watched fill drop back to zero: it holds at the mark the | ||
| 1319 | +/// dead seat reached and moves on from there, ending exactly full. | ||
| 1320 | +#[test] | ||
| 1321 | +fn a_hardware_encoder_killed_mid_export_never_pulls_the_bar_backwards() { | ||
| 1322 | + let source = asset("test_baseline.mp4"); | ||
| 1323 | + let (project, meta) = short_edit(&source, 20); | ||
| 1324 | + let out = out_path("hw_child_abort_progress"); | ||
| 1325 | + | ||
| 1326 | + let seen = injected(Some("3"), None, || { | ||
| 1327 | + let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); | ||
| 1328 | + // Sampled far faster than the export writes, so the retry cannot slip | ||
| 1329 | + // through between two reads. | ||
| 1330 | + let started = Instant::now(); | ||
| 1331 | + let mut seen = vec![handle.progress()]; | ||
| 1332 | + while !handle.is_finished() { | ||
| 1333 | + assert!( | ||
| 1334 | + started.elapsed() < Duration::from_secs(180), | ||
| 1335 | + "export did not finish" | ||
| 1336 | + ); | ||
| 1337 | + let now = handle.progress(); | ||
| 1338 | + if now != *seen.last().unwrap() { | ||
| 1339 | + seen.push(now); | ||
| 1340 | + } | ||
| 1341 | + std::thread::sleep(Duration::from_millis(1)); | ||
| 1342 | + } | ||
| 1343 | + handle | ||
| 1344 | + .result() | ||
| 1345 | + .expect("a finished export has an outcome") | ||
| 1346 | + .expect("the export survived its encoder"); | ||
| 1347 | + seen.push(handle.progress()); | ||
| 1348 | + seen | ||
| 1349 | + }); | ||
| 1350 | + | ||
| 1351 | + assert!( | ||
| 1352 | + seen.windows(2).all(|pair| pair[1] >= pair[0]), | ||
| 1353 | + "the bar went backwards across the fallback: {seen:?}" | ||
| 1354 | + ); | ||
| 1355 | + assert_eq!( | ||
| 1356 | + seen.last().copied(), | ||
| 1357 | + Some(1.0), | ||
| 1358 | + "a finished export leaves a full bar: {seen:?}" | ||
| 1359 | + ); | ||
| 1360 | + std::fs::remove_file(&out).unwrap(); | ||
| 1361 | +} | ||
| 1362 | + | ||
| 1363 | +/// Threads of this very process that belong to an audio pass. Every worker the | ||
| 1364 | +/// pass starts inherits its name from the thread that spawned it (Linux `comm` | ||
| 1365 | +/// is inherited), so this is the pass *and its own workers* -- which is what | ||
| 1366 | +/// makes it a measure of how many passes are running: two at once is twice the | ||
| 1367 | +/// crowd of one. | ||
| 1368 | +fn audio_pass_threads() -> usize { | ||
| 1369 | + std::fs::read_dir("/proc/self/task") | ||
| 1370 | + .into_iter() | ||
| 1371 | + .flatten() | ||
| 1372 | + .flatten() | ||
| 1373 | + .filter(|thread| { | ||
| 1374 | + std::fs::read_to_string(thread.path().join("comm")) | ||
| 1375 | + .is_ok_and(|name| name.trim() == "export-audio") | ||
| 1376 | + }) | ||
| 1377 | + .count() | ||
| 1378 | +} | ||
| 1379 | + | ||
| 1380 | +/// The busiest that crowd ever gets while `export` writes this project. | ||
| 1381 | +fn busiest_audio_pass(project: Project, meta: engine::VideoMeta, out: &Path) -> usize { | ||
| 1382 | + let handle = engine::export::start(project, meta, out, &ExportSettings::default()); | ||
| 1383 | + let started = Instant::now(); | ||
| 1384 | + let mut most = 0; | ||
| 1385 | + while !handle.is_finished() { | ||
| 1386 | + assert!( | ||
| 1387 | + started.elapsed() < Duration::from_secs(180), | ||
| 1388 | + "export did not finish" | ||
| 1389 | + ); | ||
| 1390 | + most = most.max(audio_pass_threads()); | ||
| 1391 | + std::thread::sleep(Duration::from_millis(1)); | ||
| 1392 | + } | ||
| 1393 | + handle | ||
| 1394 | + .result() | ||
| 1395 | + .expect("a finished export has an outcome") | ||
| 1396 | + .expect("the export was written"); | ||
| 1397 | + most | ||
| 1398 | +} | ||
| 1399 | + | ||
| 1400 | +/// One audio pass at a time, across that same death. An mp4 runs the sound | ||
| 1401 | +/// beside the picture, and the picture's failure used to leave that thread | ||
| 1402 | +/// *detached*: the re-run started a second pass, and a machine that had just | ||
| 1403 | +/// lost its GPU mixed and encoded the same film's sound twice at once. Measured | ||
| 1404 | +/// against the same export with nothing dying under it -- the fallback may not | ||
| 1405 | +/// be a busier place than an ordinary export is. | ||
| 1406 | +#[test] | ||
| 1407 | +fn a_hardware_encoder_killed_mid_export_leaves_no_audio_pass_behind() { | ||
| 1408 | + // AC-3: an mp4 cannot carry it, so the pass is a real decode and encode | ||
| 1409 | + // rather than the packet copy an AAC source is over in a millisecond. | ||
| 1410 | + let source = asset("test_ac3.mp4"); | ||
| 1411 | + let out = out_path("hw_child_abort_audio"); | ||
| 1412 | + | ||
| 1413 | + let (project, meta) = short_edit(&source, 40); | ||
| 1414 | + let alone = { | ||
| 1415 | + let _seat = pin_software(); | ||
| 1416 | + busiest_audio_pass(project, meta, &out) | ||
| 1417 | + }; | ||
| 1418 | + std::fs::remove_file(&out).unwrap(); | ||
| 1419 | + | ||
| 1420 | + let (project, meta) = short_edit(&source, 40); | ||
| 1421 | + let across = injected(Some("3"), None, || busiest_audio_pass(project, meta, &out)); | ||
| 1422 | + | ||
| 1423 | + println!("audio pass threads: {alone} alone, {across} across the fallback"); | ||
| 1424 | + assert!( | ||
| 1425 | + across <= alone, | ||
| 1426 | + "the fallback ran {across} audio-pass threads where one pass runs {alone}" | ||
| 1427 | + ); | ||
| 1428 | + let (written, _) = engine::demux::Demuxer::open(&out).expect("the export is a file"); | ||
| 1429 | + assert_eq!(written.frame_count, 40, "every timeline frame was written"); | ||
| 1430 | + std::fs::remove_file(&out).unwrap(); | ||
| 1431 | +} | ||
| 1432 | + | ||
| 1433 | +/// What a *shipping* editor waits when its child stops answering. Every test | ||
| 1434 | +/// here pins `VE_HW_TIMEOUT_MS` at 1.5 s, so none of them ever ran the 15 s | ||
| 1435 | +/// default -- fifteen seconds an export spends on a child that will never | ||
| 1436 | +/// answer again, before the software encoder it is owed can even begin. | ||
| 1437 | +/// | ||
| 1438 | +/// The ceiling stays where it is, because an encoder opening on a 4K frame is | ||
| 1439 | +/// not a dead one. What changes is that a child which has answered its first | ||
| 1440 | +/// frames has *said* what its frames cost, and from then on the wait is that | ||
| 1441 | +/// measure and not the ceiling. | ||
| 1442 | +#[test] | ||
| 1443 | +fn a_wedged_child_is_waited_out_by_its_own_measure_not_the_ceiling() { | ||
| 1444 | + let (width, height) = (320u32, 240u32); | ||
| 1445 | + // No `VE_HW_TIMEOUT_MS` at all: the wait is the one a person's export gets. | ||
| 1446 | + injected_with(None, Some("10"), None, || { | ||
| 1447 | + let mut encoder = | ||
| 1448 | + HwEncoder::open(width, height, 30, 1, 1_000_000).expect("the stand-in seat opened"); | ||
| 1449 | + let (y, u, v) = synthetic(0, width as usize, height as usize); | ||
| 1450 | + for frame in 0..10 { | ||
| 1451 | + assert!( | ||
| 1452 | + encoder.encode(&y, &u, &v, width, height, false).is_ok(), | ||
| 1453 | + "picture {frame} is before the wedge" | ||
| 1454 | + ); | ||
| 1455 | + } | ||
| 1456 | + let started = Instant::now(); | ||
| 1457 | + let wedged = encoder | ||
| 1458 | + .encode(&y, &u, &v, width, height, false) | ||
| 1459 | + .expect_err("a child that stops answering is not an encode"); | ||
| 1460 | + let waited = started.elapsed(); | ||
| 1461 | + println!("wedge at the shipping default: contained in {waited:.2?}"); | ||
| 1462 | + assert!(engine::hwproc::is_lost(&wedged), "{wedged}"); | ||
| 1463 | + assert!( | ||
| 1464 | + waited < Duration::from_secs(6), | ||
| 1465 | + "waited {waited:.2?} on a wedged child at the shipping default" | ||
| 1466 | + ); | ||
| 1467 | + // ...and never so little that a slow picture could be mistaken for a | ||
| 1468 | + // dead one: the floor holds even where every measured frame was | ||
| 1469 | + // instant, which the stand-in encoder's are. | ||
| 1470 | + assert!(waited >= Duration::from_secs(2), "waited only {waited:.2?}"); | ||
| 1471 | + }); | ||
| 1472 | +} | ||
| 1473 | + | ||
| 1301 | /// The same death at the *open*: the child never gets far enough to say yes, so | 1474 | /// The same death at the *open*: the child never gets far enough to say yes, so |
| 1302 | /// there is no hardware seat at all and the software one takes the export from | 1475 | /// there is no hardware seat at all and the software one takes the export from |
| 1303 | /// the first frame. A refusal, not a crash, and the file is whole. | 1476 | /// the first frame. A refusal, not a crash, and the file is whole. |