diff --git a/crates/reims-vgpu-graphics/src/device.rs b/crates/reims-vgpu-graphics/src/device.rs index 187eecd31..5771e7a4f 100644 --- a/crates/reims-vgpu-graphics/src/device.rs +++ b/crates/reims-vgpu-graphics/src/device.rs @@ -622,6 +622,22 @@ pub trait Host: Send + Sync + 'static { fn display_color(&self, color: &RgfxDisplayColor, entries: &[RgfxColorEntry]) { let _ = (color, entries); } + + /// One event of ring 0's timeline for the host's profiling trace, `kind` and its `values`, + /// timed by the host as it is called, on the thread it happened on: a diagnostic, which + /// changes nothing the device does. Ring 0's events and their values: + /// - `r0wake`: the control thread woke from its sleep (a doorbell, a `STATUS` write or the + /// device's end); + /// - `r0take type seqno obj_id`: the control thread took a message (`obj_id` the 8 bytes after + /// its header, 0 for a message shorter); + /// - `r0job type seqno`, `r0jobdone seqno`: a lane's job began and was carried out; + /// - `r0post type seqno raise`: an answer or event went on the control completion ring, + /// `raise` 1 where its interrupt is raised next. + /// + /// The default writes nothing. + fn trace(&self, kind: &'static str, values: &[u64]) { + let _ = (kind, values); + } } /// The largest `COMMAND_BUFFER` message a doorbell's thread offers the host @@ -1502,6 +1518,7 @@ impl Shared { break; } self.wake.sleep(self.doorbell.get().map(AsRawFd::as_raw_fd)); + self.host.trace("r0wake", &[]); // A doorbell may follow the guest's take of completions. self.room.wake(); // A reset while it slept set the ring up anew, its `consumer_idle` 1, so the guest @@ -1544,6 +1561,19 @@ impl Shared { if let Ok(Some(message)) = &next { let through = submit.consumer.consumed(); submit.backlog.took(message, through); + let obj_id = message + .bytes + .get(16..24) + .and_then(|b| <[u8; 8]>::try_from(b).ok()) + .map_or(0, u64::from_le_bytes); + self.host.trace( + "r0take", + &[ + u64::from(message.header.r#type), + message.header.seqno, + obj_id, + ], + ); } match next { Ok(Some(message)) if control::is_unheld(&message) => { @@ -1733,6 +1763,10 @@ impl Shared { } } }; + if let Ok((header, _)) = RgfxMsgHeader::read_from_prefix(message) { + let raised = u64::from(raise); + (self.host).trace("r0post", &[u64::from(header.r#type), header.seqno, raised]); + } if raise { self.host.raise_interrupt(RGFX_CONTROL_VECTOR); } @@ -2781,11 +2815,14 @@ fn serve_lane(lanes: &Lanes, shared: &Weak, key: Key, spare: Option Option(frames: &[&'a [u8]]) -> (Vec<&'a [u8]>, usize) { - let unfinished: HashSet<&[u8]> = frames - .iter() - .filter_map(|frame| frame.strip_prefix(UNFINISHED)) - .collect(); - let records = frames.iter().filter(|frame| !frame.starts_with(UNFINISHED)); - let (left_out, handed): (Vec<&[u8]>, Vec<&[u8]>) = - records.partition(|record| unfinished.contains(*record)); - (handed, left_out.len()) -} - -/// The records of the file at `path` a warm pass is handed, in its order ([`warmable`]), and how -/// many its tombstones leave out. -pub(crate) fn read(path: &Path) -> (Vec, usize) { +/// The records of the file at `path`, in its order. +pub(crate) fn read(path: &Path) -> Vec { let bytes = std::fs::read(path).unwrap_or_default(); - let (records, left_out) = warmable(&split(&bytes)); - let records = records + split(&bytes) .into_iter() .filter_map(rt::PipelineRecord::from_bytes) - .collect(); - (records, left_out) -} - -/// Appends to the file at `path` the tombstone of `record` (the module docs): one write of its -/// frame, as the keeper's ([`Keeper::keep`]), past [`CAPACITY`] too, since no later pass makes a -/// record with a tombstone, so tombstones stay as few as the records whose makings stood. -pub(crate) fn leave_out(path: &Path, record: &[u8]) -> std::io::Result<()> { - let mut tombstone = UNFINISHED.to_vec(); - tombstone.extend_from_slice(record); - OpenOptions::new() - .append(true) - .open(path)? - .write_all(&frame(&tombstone)) -} - -/// The warm thread's watch over the record the pass makes (the module docs): the runtime names -/// it by its place among the records handed (`WarmStatistics::record_in_making`), with how long -/// its making worked, and none on a generation a recovery left, whose pass ends. -#[derive(Default)] -struct Watch { - /// The place of the record that overran last. - overran: Option, -} - -impl Watch { - /// The place of the record whose making worked [`RECORD_BOUND`], by the pass's `statistics`, - /// once a record; `None` while none has. - fn overran(&mut self, statistics: &rt::WarmStatistics) -> Option { - let at = statistics.record_in_making?; - let worked = statistics.record_in_making_nanos >= RECORD_BOUND.as_nanos() as u64; - (worked && self.overran.replace(at) != Some(at)).then_some(at) - } -} - -/// The records a warm pass's counts say ended, each warmed, translated only, failed, unread, not -/// warmable or not run. -fn counted(s: &rt::WarmStatistics) -> u64 { - s.records_warmed - + s.records_translated - + s.records_failed - + s.records_unread - + s.records_not_warmable - + s.records_not_run + .collect() } /// Hands `device` the records of the file at `path`, from a thread of its own that reads the @@ -337,25 +249,22 @@ pub(crate) fn warm( let spawned = std::thread::Builder::new() .name("reims-vgpu-warm".into()) .spawn(move || { - let (records, left_out) = read(&path); + let records = read(&path); if records.is_empty() || stop.stopped() { return; } log::info!( target: "reims_vgpu_vulkan::records", - "warming {} pipeline records from {} ({left_out} left out: their warm making \ - worked {} s in an earlier pass)", + "warming {} pipeline records from {}", records.len(), - path.display(), - RECORD_BOUND.as_secs() + path.display() ); let handed = records.len() as u64; - device.warm(records.clone(), WARM_BUDGET); + device.warm(records, WARM_BUDGET); // The pass runs on the runtime's thread and says nothing when it ends: its counts // reach the records handed then. The device's end stops the polls, which hold its // runtime device. let clock = crate::os::OsClock::new(); - let mut watch = Watch::default(); let mut polls = 0u64; loop { let statistics = device.warm_statistics(); @@ -367,21 +276,6 @@ pub(crate) fn warm( ); return; } - if let Some(at) = watch.overran(&statistics) - && let Some(record) = records.get(at as usize) - { - let left = leave_out(&path, &record.to_bytes()); - log::warn!( - target: "reims_vgpu_vulkan::records", - "the warm pass's making of pipeline record {at} of {handed} worked {} s \ - without ending: {}; the guest's own creation of its state still makes it", - RECORD_BOUND.as_secs(), - match left { - Ok(()) => "later passes leave it out".to_owned(), - Err(e) => format!("its tombstone was not appended ({e})"), - } - ); - } polls += 1; if stop.wait_until(clock.now_mono().saturating_add(WARM_POLL)) { return; @@ -396,7 +290,13 @@ pub(crate) fn warm( /// while the pass runs. pub(crate) fn summary(statistics: &rt::WarmStatistics, handed: u64) -> Option { let s = statistics; - (counted(s) >= handed).then(|| { + let ended = s.records_warmed + + s.records_translated + + s.records_failed + + s.records_unread + + s.records_not_warmable + + s.records_not_run; + (ended >= handed).then(|| { format!( "warmed {handed} pipeline records: {} made, {} translated only, {} failed, {} unread, \ {} not warmable, {} not run (the budget spent or the process ending); {} bytes of \ @@ -496,92 +396,6 @@ mod tests { assert!(split(&frame(&a)[..FRAME_HEAD]).is_empty()); } - /// The bound, in nanoseconds. - const BOUND: u64 = RECORD_BOUND.as_nanos() as u64; - - /// The counts of a pass making the record at `at`, whose making worked `nanos`; none between - /// records. - fn making(at: Option, nanos: u64) -> rt::WarmStatistics { - let mut s = rt::WarmStatistics::default(); - s.records_warmed = at.unwrap_or_default(); - s.record_in_making = at; - s.record_in_making_nanos = nanos; - s - } - - /// A record whose warm making works the bound is named once, its tombstone appended, and the - /// next pass is handed every record but it - #[test] - fn an_unfinished_record_is_not_handed_to_the_next_pass() { - let dir = Scratch::new("unfinished"); - let path = dir.0.join("records"); - let records = [b"one".as_slice(), b"two", b"three"]; - let mut keeper = Keeper::open(path.clone()); - for record in records { - keeper.keep(record); - } - let mut watch = Watch::default(); - assert_eq!(watch.overran(&making(Some(0), 1_000)), None); - assert_eq!(watch.overran(&making(None, 0)), None); - assert_eq!(watch.overran(&making(Some(1), BOUND - 1)), None); - assert_eq!(watch.overran(&making(Some(1), BOUND)), Some(1)); - assert_eq!( - watch.overran(&making(Some(1), 2 * BOUND)), - None, - "named once" - ); - leave_out(&path, records[1]).unwrap(); - let bytes = std::fs::read(&path).unwrap(); - assert_eq!(warmable(&split(&bytes)), (vec![records[0], records[2]], 1)); - // The keeper, the guest's creations' end of the file, still holds it, and keeps it once. - let mut keeper = Keeper::open(path.clone()); - keeper.keep(records[1]); - assert_eq!(std::fs::read(&path).unwrap(), bytes); - } - - /// A record whose making ends within the bound is named by no poll, and the next pass is - /// handed it; so is one the runtime names no more (a recovery's new generation, a pass ended) - #[test] - fn a_finished_record_is_still_handed_to_the_next_pass() { - let dir = Scratch::new("finished"); - let path = dir.0.join("records"); - let mut keeper = Keeper::open(path.clone()); - keeper.keep(b"one"); - keeper.keep(b"two"); - let mut watch = Watch::default(); - assert_eq!(watch.overran(&making(Some(0), BOUND - 1)), None); - assert_eq!(watch.overran(&making(Some(1), BOUND - 1)), None); - assert_eq!(watch.overran(&making(None, 0)), None); - let bytes = std::fs::read(&path).unwrap(); - assert_eq!( - warmable(&split(&bytes)), - (vec![b"one".as_slice(), b"two"], 0) - ); - } - - /// A file of records alone, as every earlier build wrote it, hands a pass all of them in its - /// order; a tombstone of a record the file does not hold leaves out none; and a tombstone's - /// bytes, which an earlier build hands the runtime, are no record to it - #[test] - fn the_file_s_existing_frames_still_read() { - let (a, b) = (b"first".to_vec(), vec![7u8; 300]); - let mut file = frame(&a); - file.extend_from_slice(&frame(&b)); - assert_eq!( - warmable(&split(&file)), - (vec![a.as_slice(), b.as_slice()], 0) - ); - let mut tombstone = UNFINISHED.to_vec(); - tombstone.extend_from_slice(b"absent"); - file.extend_from_slice(&frame(&tombstone)); - assert_eq!(split(&file).len(), 3); - assert_eq!( - warmable(&split(&file)), - (vec![a.as_slice(), b.as_slice()], 0) - ); - assert!(rt::PipelineRecord::from_bytes(&tombstone).is_none()); - } - /// The keeper appends each record once, across reopenings of the file, and nothing past its /// capacity #[test] @@ -636,12 +450,12 @@ mod tests { } // The writer appends on its own thread, within 30 s. for _ in 0..3000 { - if !read(&path).0.is_empty() { + if !read(&path).is_empty() { break; } std::thread::sleep(std::time::Duration::from_millis(10)); } - assert_eq!(read(&path).0.len(), 1); + assert_eq!(read(&path).len(), 1); let device = rt::Device::system_default_with(&options()).unwrap(); let stop = Arc::new(crate::let_go::Stop::default()); let _polls = warm(&device, path, &stop); diff --git a/crates/reims-vgpu/src/graphics.rs b/crates/reims-vgpu/src/graphics.rs index 4b412c558..0d2aa3fcd 100644 --- a/crates/reims-vgpu/src/graphics.rs +++ b/crates/reims-vgpu/src/graphics.rs @@ -2531,6 +2531,10 @@ impl Host for CallbackHost

{ } } + fn trace(&self, kind: &'static str, values: &[u64]) { + reims_vgpu_vulkan::core::trace::event(kind, values); + } + fn present_surface(&self, iosurface_id: u32, generation: Option, answer: PresentAnswer) { stepped(|| { self.control diff --git a/crates/reims-vgpu/src/graphics/compile.rs b/crates/reims-vgpu/src/graphics/compile.rs index 382e6b765..4dcacee82 100644 --- a/crates/reims-vgpu/src/graphics/compile.rs +++ b/crates/reims-vgpu/src/graphics/compile.rs @@ -21,7 +21,9 @@ //! behind no other context's. Library makes go before every pipeline compile: a pipeline that //! names a library another context is still making waits for it on its thread, and that library //! has then always been taken by a thread already. Each compile's wait and run time is logged at -//! debug (`reims_vgpu::graphics::compile`). +//! debug (`reims_vgpu::graphics::compile`), and, in the profiling trace +//! ([`reims_vgpu_vulkan::core::trace`]), its hand-over, start and end as `cmphand`, `cmpstart` +//! and `cmpend` events, each with the object's id. //! //! **Order.** From its dispatch until it is stored, the pipeline is pending. A command buffer //! that names a pending pipeline (`SET_RENDER_PIPELINE_STATE`, `SET_COMPUTE_PIPELINE_STATE`: the @@ -61,6 +63,7 @@ use reims_vgpu_graphics::wire::{ }; use reims_vgpu_vulkan::core::platform::os::clock::MonoTime; use reims_vgpu_vulkan::core::sync::{Condvar, Mutex}; +use reims_vgpu_vulkan::core::trace; use reims_vgpu_vulkan::exit_gate; use zerocopy::FromBytes; @@ -330,6 +333,7 @@ impl CompilePool { state.turns.push_back(context); } drop(state); + trace::event("cmphand", &[of.obj_id]); self.shared.waiting.notify_one(); Ok(()) } @@ -422,7 +426,9 @@ fn run(shared: &Shared, queued: Queued) { ahead, } = queued; let started = (shared.now)(); + trace::event("cmpstart", &[of.obj_id]); job(); + trace::event("cmpend", &[of.obj_id]); let ended = (shared.now)(); let ms = |d: Duration| d.as_secs_f64() * 1e3; log::debug!(