From 1dccf8319feebba97acf5997885c7cc936cc96a4 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 01:53:36 -0800 Subject: [PATCH] D3D12.X: report retained pipeline age Measure completed background PSOs retained behind stand-ins and bucket them by frames since completion or last use. The activity-only bind path uses frame-counter atomics, while the two-second observer performs registry scans and deduplicates coalesced builds. Checked with cargo check -p xcl --tests --target x86_64-pc-windows-gnu using the host GCC and ar; the library and test target compiled with pre-existing dead-code warnings. Assisted-by: Codex (GPT-5.6-sol) --- README.md | 4 +- crates/xcl/src/hle/d3d12x/pso_async.rs | 79 ++++++++++++++++++++++++++ crates/xcl/src/host.rs | 8 +++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d2175d193..daeb5bc235 100644 --- a/README.md +++ b/README.md @@ -383,7 +383,9 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder line with what the title did in that time (presents, submits, draws, dispatches, frame-event waits, DirectStorage requests), a `memory:` line with the process's commit and video memory in use, a `memory by owner` line splitting the known XCL allocations from `other`, and sampled - texture resident / SRV-addressable / virtual texel totals, and the active foreground time plus + texture resident / SRV-addressable / virtual texel totals. It also reports completed background + pipelines retained behind stand-ins and how many frames ago they were last used, and the + active foreground time plus serialization wait spent looking for CPU writes before submits (write-watch bytes/ranges/pages, sparse queue/fence-state/copy phases and zero/dirty volume, and other refresh work). A negative `other` exposes an overlapping or stale sample; unavailable diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 556f23be35..4dbe225629 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -56,6 +56,10 @@ struct Build { /// the real pipeline state (one owned raw pointer shared by all `Pending`s), 0 until built /// or when building failed real: AtomicUsize, + /// Last frame in which any owner resolved this build, for `XCL_ACTIVITY` retention ages. + last_use_frame: AtomicU64, + /// Frame in which `real` became retained; age never begins before the build completed. + completed_frame: AtomicU64, done: Mutex, cv: Condvar, /// every stand-in interested in this build was released before it finished @@ -187,6 +191,8 @@ impl Build { tracked: AtomicBool::new(false), queue_state: AtomicU8::new(QUEUE_IDLE), real: AtomicUsize::new(0), + last_use_frame: AtomicU64::new(super::present::presents()), + completed_frame: AtomicU64::new(0), done: Mutex::new(false), cv: Condvar::new(), orphan: Default::default(), @@ -260,6 +266,9 @@ fn mark_queue_done(state: &AtomicU8) -> bool { impl Pending { fn note_use(&self) { + if super::memory_report::enabled() { + self.build.last_use_frame.fetch_max(super::present::presents(), Ordering::Relaxed); + } if self.key != 0 && !self.used.swap(true, Ordering::Relaxed) { used::note(self.key); let on_use = self.on_use.lock().unwrap().take(); @@ -373,6 +382,58 @@ pub fn compute_coalescing_stats() -> ComputeCoalescingStats { COALESCING.snapshot() } +/// Unique completed native pipelines retained behind title-visible stand-ins, by frames since +/// their last use. Measurement only: command lists do not retain bound D3D12 objects, so these +/// ages are not evidence that a pipeline is safe to release. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub struct RetentionStats { + pub retained_reals: usize, + pub unused_60_frames: usize, + pub unused_600_frames: usize, + pub oldest_unused_frames: u64, +} + +fn retention_stats_from( + now: u64, + builds: impl IntoIterator, +) -> RetentionStats { + let mut seen = std::collections::HashSet::new(); + let mut stats = RetentionStats::default(); + for (identity, real, last_use, completed) in builds { + if real == 0 || !seen.insert(identity) { + continue; + } + let age = now.saturating_sub(last_use.max(completed)); + stats.retained_reals += 1; + stats.unused_60_frames += (age >= 60) as usize; + stats.unused_600_frames += (age >= 600) as usize; + stats.oldest_unused_frames = stats.oldest_unused_frames.max(age); + } + stats +} + +/// Snapshot for the `XCL_ACTIVITY` thread. The registry locks are taken only by that observer; +/// bind-time tracking is a present counter load plus one relaxed atomic maximum. +pub fn retention_stats() -> RetentionStats { + if !super::memory_report::enabled() { + return RetentionStats::default(); + } + let mut builds = Vec::new(); + for shard in &PENDING.shards { + let entries = shard.lock().unwrap(); + builds.extend(entries.iter().flat_map(|map| map.values()).map(|pending| { + let build = &pending.build; + ( + Arc::as_ptr(build) as usize, + build.real.load(Ordering::Acquire), + build.last_use_frame.load(Ordering::Relaxed), + build.completed_frame.load(Ordering::Relaxed), + ) + })); + } + retention_stats_from(super::present::presents(), builds) +} + fn forget_in_flight(build: &Arc) { let Some(key) = &build.compute_key else { return }; let mut registry = IN_FLIGHT.lock().unwrap(); @@ -389,6 +450,7 @@ fn complete(build: &Arc, real: usize) { FAILED.fetch_add(1, Ordering::Relaxed); } else { BUILT.fetch_add(1, Ordering::Relaxed); + build.completed_frame.store(super::present::presents(), Ordering::Relaxed); } let orphan = { let mut done = build.done.lock().unwrap(); @@ -1235,6 +1297,23 @@ mod used_tests { assert_eq!(counters.snapshot().peak_in_flight, 2); } + #[test] + fn retention_age_counts_unique_reals_from_completion_or_last_use() { + let stats = retention_stats_from(1_000, [ + (1, 0x101, 200, 300), + (1, 0x101, 200, 300), // another stand-in sharing build 1 + (2, 0x202, 100, 995), + (3, 0, 0, 0), // failed or incomplete + (4, 0x404, 0, 0), + ]); + assert_eq!(stats, RetentionStats { + retained_reals: 3, + unused_60_frames: 2, + unused_600_frames: 2, + oldest_unused_frames: 1_000, + }); + } + fn compute_key(tag: u8) -> super::super::real::compute_build_key::ComputeBuildKey { super::super::real::compute_build_key::ComputeBuildKey::capture( 1, diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 5dbcfbce30..9986adaaf9 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -151,6 +151,14 @@ pub fn host_main(boot_path: &Path) -> i32 { mb(pools_mapped as u64) ); crate::xlog_info!("{}", owner.line()); + let pso = crate::hle::d3d12x::pso_async::retention_stats(); + crate::xlog_info!( + "pipeline retention: {} completed background PSOs behind stand-ins; {} unused for 60+ frames, {} for 600+ frames; oldest {} frames", + pso.retained_reals, + pso.unused_60_frames, + pso.unused_600_frames, + pso.oldest_unused_frames + ); let ts = crate::hle::d3d12x::vram::texel_stats(); if ts.sampled_textures > 0 { let pct = if ts.total_texels > 0 { ts.resident_texels.saturating_mul(100) / ts.total_texels } else { 0 };