diff --git a/crates/reims-vgpu/src/graphics/video_host.rs b/crates/reims-vgpu/src/graphics/video_host.rs index 2bd54a817..ab908b83e 100644 --- a/crates/reims-vgpu/src/graphics/video_host.rs +++ b/crates/reims-vgpu/src/graphics/video_host.rs @@ -44,12 +44,15 @@ //! alive what it lands from, so a decoder may go before its last pictures land. //! //! The session logs the first picture of each way it lands at `info` ("hardware video: ..."), -//! and its counts at its end. +//! and its counts at its end: its pictures by the way each landed, the outputs they landed in +//! ([`Outputs`]: how many surfaces' memory, and the most of it live at once), and the time the +//! CPU's copies of them took. #![allow(dead_code, reason = "each of the host's video arms uses a part of it")] +use std::collections::HashMap; use std::sync::{Arc, mpsc}; -use std::time::Duration; +use std::time::{Duration, Instant}; use reims_vgpu_video::core::device_memory::MemoryIdentity; use reims_vgpu_vulkan::core::host::HostPtr; @@ -225,6 +228,10 @@ pub(crate) struct Landing<'a, P: Platform> { /// What the hardware answered of a decode it failed whose picture lands all the same /// ([`Landing::hardware_failed`]). hardware_failed: Option, + /// When the CPU's copy of the picture started, once [`Landing::host_for_cpu`] answered. + cpu_from: Option, + /// How long [`Landing::copy_rows`]'s copy took. + cpu_took: Option, } /// Where the planes in a device cache are staged in the staging buffer: each one's first byte. @@ -308,10 +315,11 @@ impl Landing<'_, P> { } /// [`Self::host`], for a backend whose CPU copies the picture into them itself: the landing is - /// counted as the CPU's. + /// counted as the CPU's, and its copy timed from here to the landing's return. pub(crate) fn host_for_cpu(&mut self) -> Result, BackendError> { let planes = self.host()?; self.by_cpu = true; + self.cpu_from = Some(Instant::now()); Ok(planes) } @@ -330,6 +338,7 @@ impl Landing<'_, P> { chroma: SourcePlane, ) -> Result<(), BackendError> { let planes = self.host()?; + let started = Instant::now(); for (from, to) in [(luma, &planes.luma), (chroma, &planes.chroma)] { let rows = u64::try_from(from.rows).map_or(to.rows, |rows| rows.min(to.rows)); let bytes = u64::try_from(from.row_bytes).map_or(to.row_bytes, |b| b.min(to.row_bytes)); @@ -354,6 +363,7 @@ impl Landing<'_, P> { } } } + self.cpu_took = Some(started.elapsed()); self.by_cpu = true; Ok(()) } @@ -683,6 +693,48 @@ struct Counts { failed: u64, /// Pictures the hardware answered failed that landed as it left them. landed_failed: u64, + /// Pictures the CPU copied ([`Landing::copy_rows`], [`Landing::host_for_cpu`]) that landed. + cpu_copies: u64, + /// The time their copies took, all told. + cpu_total: Duration, + /// The longest of them. + cpu_most: Duration, +} + +/// The outputs a session's decodes landed in, told apart by their memory: the luma plane's +/// device cache's identity ([`OutputPlane::cache`]), else the identity of the mapping its pages +/// lie in ([`OutputPlane::pages`]). An output's memory is live until its mapping goes (the +/// surface's view of its pages, its block of the region until its `BLOCK_RELEASE`, or the device +/// cache), and an identity is never another mapping's, so the outputs live at a decode are the +/// surfaces whose memory the session's decodes have landed in and that the guest still holds. +#[derive(Debug, Default)] +struct Outputs { + /// Those whose memory lived at the last decode, by their identity's key. + live: HashMap, + /// The distinct outputs the decodes landed in. + distinct: u64, + /// The most live at once, at a decode. + most_live: usize, + /// Decodes into memory of no identity: a buffer over memory of another kind. + unknown: u64, +} + +impl Outputs { + /// Counts `output`, a decode's, and the outputs live at it. + fn saw(&mut self, output: &DecodeOutput

) { + let plane = &output.luma; + let identity = (plane.cache.clone()) + .or_else(|| plane.pages.as_ref().map(|pages| pages.memory.identity())); + let Some(identity) = identity else { + self.unknown += 1; + return; + }; + self.live.retain(|_, live| live.is_live()); + if self.live.insert(identity.key(), identity).is_none() { + self.distinct += 1; + } + self.most_live = self.most_live.max(self.live.len()); + } } /// A video session on a host API ([`HostDecoder`]): its decodes' pictures landed by its landing @@ -702,6 +754,8 @@ struct State { decoder: Box>, jobs: Option>>, value: u64, + /// The outputs its decodes landed in, logged at its end. + outputs: Outputs, } impl HostVideoSession

{ @@ -746,6 +800,7 @@ impl HostVideoSession

{ decoder, jobs: Some(jobs), value: 0, + outputs: Outputs::default(), }), event, counts, @@ -772,6 +827,7 @@ impl GraphicsVideoSession

for HostVideoSession

{ .map_err(|error| decode_fault(format_args!("its parameter sets: {error}")))?; } let picture = state.decoder.decode(au, &output).map_err(decode_fault)?; + state.outputs.saw(&output); let skipped = picture.is_none(); let value = state.value + 1; let landed = Arc::new(Landed::default()); @@ -797,10 +853,10 @@ impl GraphicsVideoSession

for HostVideoSession

{ impl Drop for HostVideoSession

{ fn drop(&mut self) { - let last = { + let (last, outputs) = { let mut state = self.state.lock(); drop(state.jobs.take()); - state.value + (state.value, std::mem::take(&mut state.outputs)) }; let millis = u64::try_from(TEARDOWN_WAIT.as_millis()).unwrap_or(u64::MAX); let landed = last == 0 || self.event.wait_until_signaled_value(last, millis); @@ -822,9 +878,28 @@ impl Drop for HostVideoSession

{ .filter(|(_, n)| *n > 0) .map(|(way, n)| format!("{n} {}", way.says())) .collect::>(); + let mut outputs_said = format!( + "; its outputs lay in {} surfaces' memory, {} live at once at most", + outputs.distinct, outputs.most_live + ); + if outputs.unknown > 0 { + outputs_said += &format!(", and {} in memory of no identity", outputs.unknown); + } + let cpu_said = if counts.cpu_copies > 0 { + let ms = |d: Duration| d.as_secs_f64() * 1e3; + let mean = counts.cpu_total / u32::try_from(counts.cpu_copies).unwrap_or(u32::MAX); + format!( + "; the CPU copied {}, {:.3} ms each, {:.3} ms at most", + counts.cpu_copies, + ms(mean), + ms(counts.cpu_most) + ) + } else { + String::new() + }; log::info!( "hardware video: {name}: a session ended after {last} decodes: landed {}; {} skipped, \ - {} failed, {} landed as the hardware failed them", + {} failed, {} landed as the hardware failed them{outputs_said}{cpu_said}", if landed.is_empty() { "none".to_owned() } else { @@ -861,8 +936,13 @@ fn land_pictures( } = job; match picture.map(|picture| land(picture, &output, &mut stage)) { None => counts.lock().skipped += 1, - Some(Ok((way, hardware_failed))) => { + Some(Ok((way, hardware_failed, cpu))) => { let mut counts = counts.lock(); + if let Some(took) = cpu { + counts.cpu_copies += 1; + counts.cpu_total += took; + counts.cpu_most = counts.cpu_most.max(took); + } if let Some(why) = hardware_failed { counts.landed_failed += 1; if counts.landed_failed <= FAILURES_LOGGED { @@ -909,13 +989,14 @@ fn land_pictures( } /// Lands `picture` in `output`, copying its staged planes into their device caches, and answers -/// how, and what the hardware answered where it failed the decode and the picture landed all the -/// same ([`Landing::hardware_failed`]). +/// how, what the hardware answered where it failed the decode and the picture landed all the +/// same ([`Landing::hardware_failed`]), and how long the CPU's copy took where the CPU copied it +/// (from its start to the end of [`Landing::copy_rows`], or to the picture's landing's return). fn land( picture: Box>, output: &DecodeOutput

, stage: &mut Stage

, -) -> Result<(Way, Option), BackendError> { +) -> Result<(Way, Option, Option), BackendError> { let mut landing = Landing { luma: &output.luma, chroma: &output.chroma, @@ -924,8 +1005,11 @@ fn land( by_cpu: false, decoded: false, hardware_failed: None, + cpu_from: None, + cpu_took: None, }; picture.land(&mut landing)?; + let cpu = (landing.cpu_took).or_else(|| landing.cpu_from.map(|from| from.elapsed())); let Landing { staged, by_cpu, @@ -940,9 +1024,9 @@ fn land( } let cached = output.luma.cached || output.chroma.cached; if decoded && cached && !is_staged && !by_cpu { - return Ok((Way::Decoded, hardware_failed)); + return Ok((Way::Decoded, hardware_failed, cpu)); } - Ok((Way::of(is_staged, cached, by_cpu), hardware_failed)) + Ok((Way::of(is_staged, cached, by_cpu), hardware_failed, cpu)) } mod encode; diff --git a/crates/reims-vgpu/src/graphics/video_host/tests.rs b/crates/reims-vgpu/src/graphics/video_host/tests.rs index 219c99a7c..a6b930ac1 100644 --- a/crates/reims-vgpu/src/graphics/video_host/tests.rs +++ b/crates/reims-vgpu/src/graphics/video_host/tests.rs @@ -2,6 +2,7 @@ use std::collections::VecDeque; use std::time::Duration; use reims_vgpu_graphics::wire::RGFX_VIDEO_CODEC_H264; +use reims_vgpu_video::core::device_memory::MemoryLife; use reims_vgpu_vulkan::core::testkit::FakePlatform; use reims_vgpu_vulkan::core::testkit::calllog::Arg; use reims_vgpu_vulkan::core::testkit::objects::CompleteOn; @@ -503,6 +504,60 @@ fn a_picture_the_hardware_failed_lands_as_any_and_is_counted() { assert_eq!((counts.failed, counts.landed_failed), (0, 1)); } +/// A session counts the outputs its decodes landed in by their memory's identity, once each, +/// and the most whose memory lived at once at a decode: an output whose memory went is no longer +/// live. A decode into memory of no identity is counted apart. +#[test] +fn a_session_counts_its_outputs_by_their_memory_and_the_most_live_at_once() { + let p = FakePlatform::new(); + let log = Log::default(); + let s = session(&p, Vec::new(), &log); + let bitstream = buffer(&p, 4096, STORAGE_SHARED); + let pages = buffer(&p, 16384, STORAGE_SHARED); + let (a, b, c) = (MemoryLife::new(), MemoryLife::new(), MemoryLife::new()); + let into = |life: Option<&MemoryLife>| { + let (mut luma, chroma) = planes(&pages, &pages, 96); + luma.cache = life.map(MemoryLife::identity); + s.decode(&decode(&bitstream, &[], (luma, chroma))).unwrap(); + }; + into(Some(&a)); + into(Some(&b)); + into(Some(&a)); + drop(a); + into(Some(&c)); + into(Some(&b)); + into(None); + let state = s.state.lock(); + let outputs = &state.outputs; + assert_eq!( + (outputs.distinct, outputs.most_live, outputs.live.len()), + (3, 2, 2) + ); + assert_eq!(outputs.unknown, 1); +} + +/// A picture the CPU copied is counted with its copy's time; one the hardware wrote is not. +#[test] +fn a_session_times_the_cpu_s_copies() { + let p = FakePlatform::new(); + let log = Log::default(); + let answers = vec![ + Ok(Some(Picture::Cpu(9, Arc::clone(&log)))), + Ok(Some(Picture::Fill(4, Arc::clone(&log)))), + ]; + let s = session(&p, answers, &log); + let bitstream = buffer(&p, 4096, STORAGE_SHARED); + let pages = buffer(&p, 16384, STORAGE_SHARED); + for _ in 0..2 { + s.decode(&decode(&bitstream, &[], planes(&pages, &pages, 96))) + .unwrap(); + } + assert!(event_of(&s).wait_until_signaled_value(2, 10_000)); + let counts = s.counts.lock(); + assert_eq!(counts.cpu_copies, 1); + assert!(counts.cpu_most <= counts.cpu_total); +} + /// A session's end waits for its last picture to land. #[test] fn an_end_waits_for_the_last_picture() { diff --git a/docs/video/vaapi.md b/docs/video/vaapi.md index 85974d941..31242aa3f 100644 --- a/docs/video/vaapi.md +++ b/docs/video/vaapi.md @@ -497,7 +497,10 @@ the render node of the runtime device's own GPU, whatever its vendor copies at its end, as `a session's pictures copied by the GPU into their outputs: 300, 0.812 ms of GPU time each, 1.204 ms at most (300 timed)`, and its counts, as `a session ended after 300 decodes: landed 300 in the - surface's pages, by the hardware; 0 skipped, 0 failed`. A picture that + surface's pages, by the hardware; 0 skipped, 0 failed`, followed by how + many surfaces' memory its outputs lay in and the most of it live at once + (`video_host`'s `Outputs`), and, where the CPU copied pictures, the + copies' mean and longest time. A picture that does not land fails its decode's command buffer ([devices.md](devices.md), "A decode"). - **Surfaces.** A picture holds its decode surface until it lands, and