diff --git a/crates/reims-vgpu/src/graphics/decode/cmdbuf.rs b/crates/reims-vgpu/src/graphics/decode/cmdbuf.rs index 0e089c347..f03876dc4 100644 --- a/crates/reims-vgpu/src/graphics/decode/cmdbuf.rs +++ b/crates/reims-vgpu/src/graphics/decode/cmdbuf.rs @@ -5898,6 +5898,97 @@ mod tests { ); } + /// Session 40 of H.264 decode on the fake's video, and buffer 6 with a texture over each of + /// IOSurface 0x99's two planes, 7 and 8, made on `dec`; what the video saw. + fn decode_setup(p: &FakePlatform, dec: &mut GraphicsDecoder) -> Arc { + let device = p.device(); + let seen = Arc::new(VideoSeen::default()); + dec.set_video(Arc::new(FakeVideo(Arc::clone(&seen), p.device()))); + let create = video_session_create(40); + assert_eq!(dec.decode(&device, create.as_bytes()).unwrap(), Ok(())); + let buffer = RgfxBufferCreate { + header: RgfxMsgHeader { + r#type: RGFX_CMD_BUFFER_CREATE, + size: size_of::() as u32, + seqno: 0, + }, + obj_id: 6, + length: 65536, + ..RgfxBufferCreate::default() + }; + assert_eq!(dec.decode(&device, buffer.as_bytes()).unwrap(), Ok(())); + for t in [ + surface_plane_texture(7, 0, 10, 32, 64, 16), + surface_plane_texture(8, 1, 30, 32 + 64 * 16, 32, 8), + ] { + assert_eq!(dec.decode(&device, t.as_bytes()).unwrap(), Ok(())); + } + seen + } + + /// A video encoder on session 40 with one decode into textures 7 and 8 ([`decode_setup`]). + fn decode_ops() -> Vec { + let decode = RgfxVideoOpDecodeH264 { + bitstream_buffer_obj_id: 2, + bitstream_offset: 16, + bitstream_size: 100, + luma_texture_obj_id: 7, + chroma_texture_obj_id: 8, + parameter_sets_size: 0, + output_rows: 16, + }; + let pass = RgfxOpVideoPass { session_obj_id: 40 }; + let mut ops = op(RGFX_OP_ENCODER_BEGIN_VIDEO, pass.as_bytes()); + ops.extend(op(RGFX_OP_VIDEO_DECODE_H264, decode.as_bytes())); + ops.extend(op(RGFX_OP_ENCODER_END, &[])); + ops + } + + /// The device's reset releases the waits it would leave hanging + /// ([`GraphicsDecoder::release_waits_for_reset`]) by setting the events of the guest's + /// contexts past every value, never a video session's own: a decode's command buffer, which + /// waits for its session's event to reach the decode's value, waits on for the host's landing + /// of the picture, so the reset's settle, which waits for the command buffers committed + /// before it, waits for the landing too, a stalled one's hold among them (`video_host`'s + /// `hold_stalled_landing`), and no write of the host's lands in the output once the reset + /// returns. A shared event of the session's context is set past every value beside it. + #[test] + fn a_reset_s_release_leaves_a_decode_s_wait_for_its_session_s_event() { + let (p, mut dec) = setup(); + let device = p.device(); + let seen = decode_setup(&p, &mut dec); + let event = RgfxSharedEventCreate { + header: RgfxMsgHeader { + r#type: RGFX_CMD_SHARED_EVENT_CREATE, + size: size_of::() as u32, + seqno: 0, + }, + obj_id: 70, + initial_value: 0, + }; + assert_eq!(dec.decode(&device, event.as_bytes()).unwrap(), Ok(())); + let message = command_buffer_message(100, 1, &decode_ops()); + assert_eq!(dec.decode(&device, &message).unwrap(), Ok(())); + let calls = p.call_log().calls(); + let wait = calls + .iter() + .find(|c| c.sel == "encodeWaitForEvent:value:") + .unwrap(); + assert_eq!(wait.args.get(1), Some(&Arg::U64(1)), "the decode's value"); + let session = seen.events.lock()[0].clone_ref(); + let within = || Duration::from_secs(30); + assert_eq!(dec.release_waits_for_reset(&device, &within), Ok(())); + let Some(ObjHandle::SharedEvent(guests)) = dec.named(70) else { + panic!("no shared event 70"); + }; + assert_eq!(guests.signaled_value(), u64::MAX, "the context's, set"); + assert_eq!( + session.signaled_value(), + 0, + "the session's, the landing's to set" + ); + } + /// A plane of a buffer over host memory in a block of the region reaches the host's video as /// the block's memory: the block's bytes, the buffer's place in them, and an identity that /// ends at the block's `BLOCK_RELEASE`, one for the block whichever decode names it. diff --git a/crates/reims-vgpu/src/graphics/video_host.rs b/crates/reims-vgpu/src/graphics/video_host.rs index 781d5c37c..e3e94c65f 100644 --- a/crates/reims-vgpu/src/graphics/video_host.rs +++ b/crates/reims-vgpu/src/graphics/video_host.rs @@ -141,15 +141,19 @@ use super::decode::{ #[cfg(windows)] use super::drawn::DrawnTarget; -/// How long a session's end waits for its last picture to land. -pub(crate) const TEARDOWN_WAIT: Duration = Duration::from_secs(10); +/// How long a session's end waits for its last picture to land: 15 s, the hardware's own +/// deadline (`reims_vgpu_video::core::device_wait::DEVICE_WAIT`, 5 s) and [`STALL_HOLD`] after +/// it, the longest a landing whose work stalled holds before the process dies, so that an end +/// waits a held landing out. Ours. +pub(crate) const TEARDOWN_WAIT: Duration = Duration::from_secs(15); /// How long a picture's landing that failed in device caches when the hardware's work ran past /// its deadline waits for that work to be let go, before it acquires the caches -/// (`Landing::failed_in_caches`), and a VideoToolbox picture's that failed so in the surface the -/// guest lent, before its decoder answers (`super::vt`): 10 s on the session's clock, twice the -/// hardware's own deadline (`reims_vgpu_video::core::device_wait::DEVICE_WAIT`), so that the work -/// has 15 s from its first wait to finish. Ours. Past it, the process dies. +/// (`Landing::failed_in_caches`); one's that failed so in its output's pages, before its failure +/// is signalled ([`hold_stalled_landing`]); and a VideoToolbox picture's that failed so in the +/// surface the guest lent, before its decoder answers (`super::vt`): 10 s on the session's +/// clock, twice the hardware's own deadline (`reims_vgpu_video::core::device_wait::DEVICE_WAIT`), +/// so that the work has 15 s from its first wait to finish. Ours. Past it, the process dies. pub(crate) const STALL_HOLD: Duration = Duration::from_secs(10); /// The code of a host video session whose staging ended (`Stage::copy_with_caches`): a second @@ -184,6 +188,13 @@ pub(crate) const CARRIED_CODE: &str = "video-landing-failure-carried"; /// boot, then counted"). pub(crate) const UNTOLD_CODE: &str = "video-landing-failure-untold"; +/// The code of a picture that failed to land while the hardware's work, which may still write +/// its output's pages, ran past its deadline: its failure, and the session's signal of its +/// value, which the decode's command buffer waits for, held until the device is done with the +/// work, up to [`STALL_HOLD`] ([`hold_stalled_landing`]; docs/conventions.md, "Each degradation +/// is logged once per boot, then counted"). +pub(crate) const STALL_HELD_CODE: &str = "video-landing-stall-held"; + /// The code of a VideoToolbox decoder's picture the CPU wrote into its surface's two planes from /// a host surface of another layout: the range extensions' packed 4:2:2 and 4:4:4 samples a /// VA-API driver decodes into (YUY2, Y210, Y212, Y410, Y412, and XYUV where the GPU's gather does @@ -1855,6 +1866,8 @@ fn land_pictures( landed.fail(); reims_vgpu_vulkan::answer::count_kind(CARRIED_CODE); } + // What the landing keeps of the hardware's work that ran past its deadline, from here. + let since = KeptMark::here(); match picture.map(|picture| land(picture, &output, &mut stage, clock, &landed)) { None => { counts.lock().skipped += 1; @@ -1890,6 +1903,9 @@ fn land_pictures( } } Some(Err(error)) => { + // Before the clear of its planes and the signal: the hardware may still write + // the output. + hold_stalled_landing(clock, since, &error, name); let unmade = stage.take_unmade(); #[cfg(unix)] clear_unlanded(&output, &mut stage, name); @@ -2066,6 +2082,33 @@ impl Landing<'_, P> { #[cfg(unix)] const IN_CACHES: &str = "a picture's landing in its device caches"; +/// The landing [`hold_stalled_landing`] holds, as its death names it. +const IN_PAGES: &str = "a picture's landing in its output"; + +/// A picture's failed landing, `error`, held while work the landing thread kept since `since`, +/// which may still write the output's pages, is held, its device not done with it +/// ([`kept_since`]): NVDEC's copies into the output's registered pages, VA-API's video +/// processing into their import. Counted ([`STALL_HELD_CODE`]) and logged once a boot, it waits +/// on `clock` up to [`STALL_HOLD`], and the process dies past it ([`hold_while_kept`]), so the +/// output's clear and the session's signal of its value, which completes the decode's command +/// buffer and lets a reset's wait for that buffer end, come only once nothing of the host's +/// writes the output. A landing that failed in device caches waited for its work there +/// (`Landing::failed_in_caches`), so nothing it kept is held any more, and none is held twice. +fn hold_stalled_landing(clock: &dyn Runtime, since: KeptMark, error: &BackendError, name: &str) { + if !kept_since(since) { + return; + } + reims_vgpu_vulkan::answer::count_kind(STALL_HELD_CODE); + log::warn!( + kind:% = STALL_HELD_CODE; + "hardware video: {name}: a picture failed to land ({error}) while the hardware's work, \ + which may still write its output, ran past its deadline: its failure, and the signal \ + its command buffer waits for, wait until the device is done, up to {STALL_HOLD:?}, and \ + the process dies past it ({STALL_HELD_CODE})" + ); + hold_while_kept(clock, since, STALL_HOLD, IN_PAGES, error); +} + /// Whether work the thread of `since` kept after the mark is held, its device not done with it /// ([`Lingering::holds_since`]). pub(super) fn kept_since(since: KeptMark) -> bool { diff --git a/crates/reims-vgpu/src/graphics/video_host/tests.rs b/crates/reims-vgpu/src/graphics/video_host/tests.rs index 3ed792a67..b4bf1342d 100644 --- a/crates/reims-vgpu/src/graphics/video_host/tests.rs +++ b/crates/reims-vgpu/src/graphics/video_host/tests.rs @@ -1,11 +1,9 @@ use std::collections::VecDeque; -#[cfg(unix)] use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use reims_vgpu_graphics::wire::RGFX_VIDEO_CODEC_H264; use reims_vgpu_video::core::device_memory::MemoryLife; -#[cfg(unix)] use reims_vgpu_video::core::device_wait::Unfinished; use reims_vgpu_video::core::runtime::StdRuntime; use reims_vgpu_vulkan::core::testkit::FakePlatform; @@ -132,14 +130,17 @@ enum Picture { /// once the flag is set ([`Stuck`]). #[cfg(unix)] StallInCaches(Option>), + /// A failure to land in the output's pages, where they lie, whose work ran past its + /// deadline, as NVDEC's copies into the pages or VA-API's video processing into their import + /// fail: the work it keeps on the landing's thread, which its device is done with once the + /// flag is set ([`Stuck`]). + StallInPages(Arc), } /// A stalled picture's work, kept on the process's list until its device is done with it, once /// the flag is set. -#[cfg(unix)] struct Stuck(Arc); -#[cfg(unix)] impl Unfinished for Stuck { fn finished(&mut self) -> bool { self.0.load(Ordering::SeqCst) @@ -257,6 +258,11 @@ impl HostPicture

for Picture { let failed = "the hardware's write ran past its deadline".into(); Err(to.failed_in_caches(&[to.luma], Failed::Stalled(since), failed)) } + Picture::StallInPages(done) => { + let runtime = StdRuntime::shared(); + Lingering::process(&runtime).keep("a test's stalled write", Box::new(Stuck(done))); + Err("the hardware's write ran past its deadline".into()) + } } } } @@ -746,7 +752,6 @@ fn a_failed_landing_in_a_cache_says_it_written_before_its_signal() { /// Whether `s`'s event reaches `value` within `within` of real time, the landing's clock (the /// fake event's own waits are on the fake world's virtual clock), asked every 10 ms. -#[cfg(unix)] fn signalled_within(s: &HostVideoSession

, value: u64, within: Duration) -> bool { let pause = Duration::from_millis(10); for _ in 0..=within.as_millis() / pause.as_millis() { @@ -837,6 +842,40 @@ fn a_stalled_landing_whose_kept_work_is_not_let_go_ends_the_process() { } } +/// A picture whose landing in its output's pages ran past the hardware's deadline (a stall), +/// with work kept that may still write them, fails, and the session signals its value only once +/// that work is let go, its device done with it ([`hold_stalled_landing`]): the command buffer +/// that waits for the value completes, failed, only then, and the held stall is counted. The +/// later pictures land behind it as any, and the process lives. A death past the hold is +/// [`hold_while_kept`]'s (`a_stalled_landing_whose_kept_work_is_not_let_go_ends_the_process`). +#[test] +fn a_stalled_landing_in_its_pages_is_signalled_once_its_kept_work_is_let_go() { + use crate::platform::counted; + let p = FakePlatform::new(); + let log = Log::default(); + let done = Arc::new(AtomicBool::new(false)); + let answers = vec![Ok(Some(Picture::StallInPages(Arc::clone(&done))))]; + let s = session(&p, answers, &log); + let bitstream = buffer(&p, 4096, STORAGE_SHARED); + let pages = buffer(&p, 16384, STORAGE_SHARED); + let d = || decode(&bitstream, &[], planes(&pages, &pages, 96)); + let held = counted(STALL_HELD_CODE); + let stalled = s.decode(&d()).unwrap(); + let next = s.decode(&d()).unwrap(); + assert_eq!((stalled.value, next.value), (1, 2)); + assert!( + !signalled_within(&s, 1, Duration::from_millis(500)), + "no signal while the work is kept" + ); + assert_eq!(counted(STALL_HELD_CODE), held + 1, "the held stall"); + done.store(true, Ordering::SeqCst); + assert!(signalled_within(&s, 2, Duration::from_secs(10))); + assert!(stalled.landed.as_deref().is_some_and(Landed::failed)); + assert!(next.landed.as_deref().is_some_and(|l| !l.failed())); + assert_eq!(s.counts.lock().failed, 1); + assert_eq!(counted(STALL_HELD_CODE), held + 1); +} + /// A copy into the output's caches that the platform makes no blit encoder for fails the /// picture's landing: the caches never held the picture, so its value is signalled with its /// command buffer failing, counted as a failure to land, and no reader is handed the caches' diff --git a/docs/video/nvidia.md b/docs/video/nvidia.md index 5edbc8bee..98db81c52 100644 --- a/docs/video/nvidia.md +++ b/docs/video/nvidia.md @@ -156,7 +156,10 @@ where it is NVIDIA's and CUDA opens it (`graphics/video_choice.rs`). caches are acquired first (`video_host`'s `Landing::failed_in_caches`). Where the work ran past its deadline and may still write, the landing first waits for the work it kept, with the imports, to be let go, up to - `video_host::STALL_HOLD` (10 s), and the process dies past it. For HEVC + `video_host::STALL_HOLD` (10 s), and the process dies past it. A stall of + the copies into the output's registered pages is held the same way before + the session signals the failure (`video_host`'s `hold_stalled_landing`). + For HEVC the guest's video driver's schedule of the picture waits for that verdict and fails (`kVTVideoDecoderMalfunctionErr`), diff --git a/docs/video/vaapi.md b/docs/video/vaapi.md index ac24cacd5..a599ee588 100644 --- a/docs/video/vaapi.md +++ b/docs/video/vaapi.md @@ -582,7 +582,11 @@ answers for its device, `rt_platform::HostGpu`). The arm IOSurface do. A stall's work, which may still write, is waited for first: the landing waits for the work it kept (the stalled pool, the frame's target, a stalled write into the cache) to be let go, up to - `video_host::STALL_HOLD` (10 s), and the process dies past it. + `video_host::STALL_HOLD` (10 s), and the process dies past it. A picture + whose video processing into its output's pages, where they lie, stalled + is held the same way before the session signals its failure + (`video_host`'s `hold_stalled_landing`), so the guest's command buffer + completes only once nothing of the host's writes the pages. - **Surfaces.** A picture holds its decode surface until it lands, and the session holds no more pictures unlanded than the decoder's spare frames (`SPARE_FRAMES`, 2). The decoder's pool makes a surface only for