From 273107b9070a6d639640226711df9c4c35e64ace Mon Sep 17 00:00:00 2001 From: KOLESNiii <102358205+KOLESNiii@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:42:06 +0000 Subject: [PATCH] xcl run: a profile-tracy build records a Tracy timeline of the run XCL has no profiler: `XCL_ACTIVITY` counts presents, draws and DirectStorage requests every 2 s, and the PSO logs time single compiles, but nothing shows what a slow frame or a loading stall spent its time on, or on which thread. `cargo build --release -p xcl --features profile-tracy` builds an XCL whose host process starts a Tracy client: zones on shader translation, DXC, driver PSO compiles (with the record hash), async PSO builds, ExecuteCommandLists, translated dispatches, PresentX, WaitFrameEventX, Wait32/64BitValueX and DirectStorage entries, a frame mark per PresentX, and plots of commit, video memory and the activity counters. Without the feature the macros expand to nothing. Tracy's crash handler is off (XCL's own exception handlers must see the title's faults) and its lifetime is manual, so only the host runs it. With Tracy's C++ linked in, xcl.exe's CRT startup registers its thread_local destructor callback with the UCRT, which takes one per process: the title's CRT startup registering its own then ended the process in terminate() (0xC0000409, found under a debugger). In this build only, the title's _register_thread_local_exe_atexit_callback import resolves to a no-op. On a retail title (RTX 2070 SUPER) a 9 min 40 s capture held 9,117 frames and 328,501 zones: 30 fps menus, ~5 fps in the world with ~85 ms of ExecuteCommandLists per frame, and a 190 s freeze in which two title threads built huge pipelines one after another (16-24 s each). Assisted-by: Claude Code (Claude Opus 5.5) --- Cargo.lock | 298 +++++++++++++++++++++++-- README.md | 11 + crates/xcl/Cargo.toml | 4 + crates/xcl/src/gpu/recomp/dxc.rs | 2 + crates/xcl/src/gpu/recomp/mod.rs | 1 + crates/xcl/src/hle/d3d12x/bind.rs | 1 + crates/xcl/src/hle/d3d12x/frame.rs | 1 + crates/xcl/src/hle/d3d12x/list_x.rs | 1 + crates/xcl/src/hle/d3d12x/present.rs | 2 + crates/xcl/src/hle/d3d12x/pso_async.rs | 1 + crates/xcl/src/hle/d3d12x/real.rs | 7 + crates/xcl/src/hle/dstorage.rs | 1 + crates/xcl/src/host.rs | 8 + crates/xcl/src/lib.rs | 1 + crates/xcl/src/prof.rs | 83 +++++++ crates/xcl/src/resolver.rs | 4 + 16 files changed, 411 insertions(+), 15 deletions(-) create mode 100644 crates/xcl/src/prof.rs diff --git a/Cargo.lock b/Cargo.lock index cb2a50ac1e..8d9081150f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -42,6 +51,20 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" +[[package]] +name = "generator" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6c6a73cd1aa4f4f00c02b109fa55d3a9d6ecc2fc4f53177b74ed6ee23b0c53" +dependencies = [ + "cc", + "libc", + "log", + "rustversion", + "windows-core 0.100.0", + "windows-sys 0.61.2", +] + [[package]] name = "generic-array" version = "0.14.9" @@ -74,6 +97,12 @@ version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" + [[package]] name = "libc" version = "0.2.191" @@ -90,6 +119,25 @@ dependencies = [ "libc", ] +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + +[[package]] +name = "loom" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1611ddbf89dc6ae42cfbd51fc207836140130257646eab8d6d2fb16577db2d2" +dependencies = [ + "cfg-if", + "generator", + "scoped-tls", + "tracing", + "tracing-subscriber", +] + [[package]] name = "mach2" version = "0.4.3" @@ -99,6 +147,15 @@ dependencies = [ "libc", ] +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + [[package]] name = "memchr" version = "2.8.3" @@ -125,12 +182,27 @@ dependencies = [ "winapi", ] +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "once_cell" version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "pin-project-lite" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c938015719552d665fd0553b90d96cd7ff58e83e4c061ca99e6bb78865a72752" + [[package]] name = "proc-macro2" version = "1.0.107" @@ -149,6 +221,23 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex-automata" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d60ef07df457506a0a1a2073112eb4aae5733dbd2813c3da77d1b0fd441158" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d902046e6a28a1e4c3cab97258a0ce11e994903f01c2d97a9447a0b3df85f4f6" + [[package]] name = "region" version = "3.0.2" @@ -177,6 +266,24 @@ dependencies = [ "slice-pool2", ] +[[package]] +name = "rustc-demangle" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "serde" version = "1.0.229" @@ -229,6 +336,15 @@ dependencies = [ "serde_core", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "2.0.1" @@ -247,6 +363,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a3d689654af89bdfeba29a914ab6ac0236d382eb3b764f7454dde052f2821f8" +[[package]] +name = "smallvec" +version = "1.16.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3244542535d5de959d90ec55d1b5ff0fd2bb7b1310223907d5f11932847f7b59" + [[package]] name = "syn" version = "2.0.119" @@ -273,6 +395,15 @@ dependencies = [ name = "testgame-mw" version = "0.1.0" +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + [[package]] name = "toml" version = "1.2.1+spec-1.1.0" @@ -312,6 +443,77 @@ version = "1.2.0+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "009dbb7700f11c55ef83aebbe4c0e9cf1f1ac0844bd8d6f00dc38f302026e13a" +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "tracy-client" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d93fd676d8946eaeaff4881d26d2240a8d106fcaccbd47cb9f1389e3561ba490" +dependencies = [ + "loom", + "once_cell", + "rustc-demangle", + "tracy-client-sys", +] + +[[package]] +name = "tracy-client-sys" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f4b6d2481c5e266e9def4c10e1680dbdfdd69523ed555a83d6675953a2785e" +dependencies = [ + "cc", + "windows-targets", +] + [[package]] name = "typenum" version = "1.20.1" @@ -324,6 +526,12 @@ version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + [[package]] name = "version_check" version = "0.9.5" @@ -359,7 +567,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" dependencies = [ "windows-collections", - "windows-core", + "windows-core 0.62.2", "windows-future", "windows-numerics", ] @@ -370,7 +578,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610" dependencies = [ - "windows-core", + "windows-core 0.62.2", ] [[package]] @@ -379,11 +587,24 @@ version = "0.62.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" dependencies = [ - "windows-implement", - "windows-interface", - "windows-link", - "windows-result", - "windows-strings", + "windows-implement 0.60.2", + "windows-interface 0.59.3", + "windows-link 0.2.1", + "windows-result 0.4.1", + "windows-strings 0.5.1", +] + +[[package]] +name = "windows-core" +version = "0.100.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "803590c6856f51d15fa2e9fabe4f4703122c8737f72ef2741b4ac09d4c2192f5" +dependencies = [ + "windows-implement 0.100.0", + "windows-interface 0.100.0", + "windows-link 0.100.0", + "windows-result 0.100.0", + "windows-strings 0.100.0", ] [[package]] @@ -392,8 +613,8 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" dependencies = [ - "windows-core", - "windows-link", + "windows-core 0.62.2", + "windows-link 0.2.1", "windows-threading", ] @@ -408,6 +629,17 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "windows-implement" +version = "0.100.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc1bc82e5a1b1a4d7255861b07881a0407b552f50a4d8543c0c7276fa96f6e8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "windows-interface" version = "0.59.3" @@ -419,20 +651,37 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "windows-interface" +version = "0.100.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3f7005c3b757b9b82c2f40c0e71094ff1080adddaf3e8b6ce99b545d3c48796" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-link" +version = "0.100.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b39de7c7fe78858b0144c50c3fc000bab3be17d5e9b85b1053871693c7f7415" + [[package]] name = "windows-numerics" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" dependencies = [ - "windows-core", - "windows-link", + "windows-core 0.62.2", + "windows-link 0.2.1", ] [[package]] @@ -441,7 +690,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" dependencies = [ - "windows-link", + "windows-link 0.2.1", +] + +[[package]] +name = "windows-result" +version = "0.100.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44873a1ef61c85d5e2991db91fe656cfcf0731097e00dff1ac860f1cb3b752a0" +dependencies = [ + "windows-link 0.100.0", ] [[package]] @@ -450,7 +708,16 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" dependencies = [ - "windows-link", + "windows-link 0.2.1", +] + +[[package]] +name = "windows-strings" +version = "0.100.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a70b590c684f1d2854f74999896f7c0558f1bdde62f599088599e20ea1d2f60" +dependencies = [ + "windows-link 0.100.0", ] [[package]] @@ -468,7 +735,7 @@ version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -493,7 +760,7 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -559,6 +826,7 @@ dependencies = [ "serde", "serde_json", "toml", + "tracy-client", "windows", "windows-core", "windows-sys 0.61.2", diff --git a/README.md b/README.md index f5ba717560..b7f3b68105 100644 --- a/README.md +++ b/README.md @@ -406,6 +406,17 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper. console shaders' hashes (`cs`, `vs` / `ps`, `ms` / `ps`, `vs` / `gs` / `ps`). - `XCL_TEXTURE_LEVEL_PAGES=1` gives sampled title textures backing as their mip levels stream in. Zero-filled mip uploads in a batch share one staging span and still clear any mapped tiles. +- **Profiling with [Tracy](https://github.com/wolfpld/tracy)**: `cargo build --release -p xcl + --features profile-tracy` builds an XCL that records a timeline, every thread's zones + (shader translation, DXC, driver PSO compiles with their record hash, async PSO builds, + `ExecuteCommandLists`, translated dispatches, `PresentX`, `WaitFrameEventX`, + `Wait32/64BitValueX`, DirectStorage entries), a frame mark at each `PresentX`, and plots of + commit, video memory, presents, draws, dispatches and DirectStorage MB. It shows what a frame + or a loading stall spent its time on, and on which thread: the 2 s `XCL_ACTIVITY` lines only + count what happened. Connect Tracy 0.14.1 (`tracy-profiler`, or `tracy-capture -o run.tracy`) + on localhost; run elevated for its CPU sampling and context switches. Without the feature + the zones compile to nothing. Tracy's crash handler is left out (XCL's exception handlers + must see the title's faults). - `XCL_DSTORAGE_PARALLEL_DECOMPRESS=1` enables parallel CPU decompression for file-backed zlib requests with virtual destinations. Defaults: four workers and a shared 32 MiB staging budget (`XCL_DSTORAGE_WORKERS`, `XCL_DSTORAGE_MEMORY_BUDGET_MIB`). Requests retire in queue order. diff --git a/crates/xcl/Cargo.toml b/crates/xcl/Cargo.toml index 83cc661b3e..ac54510cdd 100644 --- a/crates/xcl/Cargo.toml +++ b/crates/xcl/Cargo.toml @@ -12,6 +12,7 @@ path = "src/lib.rs" [features] pipeline-resolution-bench = [] texture-selection-bench = [] +profile-tracy = ["dep:tracy-client"] [[bin]] name = "xcl" @@ -28,6 +29,9 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" toml = "1" windows-core = "0.62" +# local profiling only (not for a PR): `--features profile-tracy`. No crash handler: XCL's own +# vectored handlers must see the title's faults and illegal instructions first. +tracy-client = { version = "0.19", optional = true, default-features = false, features = ["enable", "sampling", "system-tracing", "context-switch-tracing", "code-transfer", "flush-on-exit", "demangle", "only-localhost", "manual-lifetime"] } [dependencies.windows] version = "0.62" diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index 7199f741d9..2dad6f1b12 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -875,6 +875,7 @@ fn pool() -> &'static Arc { /// Compile HLSL to signed DXIL. `profile` e.g. "cs_6_6"; `extra` are dxc arguments. pub fn compile(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result, String> { + crate::prof_zone!("DXC compile (incl. queue wait)", format!("{profile} {entry}, {} B HLSL", src.len())); let (reply, rx) = std::sync::mpsc::channel(); let job = Job { src: src.into(), @@ -894,6 +895,7 @@ pub fn compile(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result< pub const UNOPTIMISED_ABOVE: usize = 8 * 1024 * 1024; fn compile_here(src: &str, entry: &str, profile: &str, extra: &[&str]) -> Result, String> { + crate::prof_zone!("DXC compile (worker)", format!("{profile} {entry}, {} B HLSL", src.len())); // compute (also what MS and GS programs run as): DXC's time for its memory accesses is // linear this way (`emit::linked_form`). Anything wrong with it: as one program, as before. if profile.starts_with("cs_") && linking() { diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 8452602e8a..f13ac9bd83 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -1940,6 +1940,7 @@ pub(crate) fn needs_coherent_mem(insts: &[Decoded]) -> bool { #[inline] pub fn translate(code: &[u8], opt: &Options) -> Result { let _stall = crate::stall::span(crate::stall::StallKind::ShaderTranslation); + crate::prof_zone!("recompile RDNA2 -> HLSL", format!("{} B", code.len())); translate_inner(code, opt) } diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index 7938495b9f..25f0f7be41 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -4981,6 +4981,7 @@ unsafe fn translated_dispatch( z: u32, limits: [u32; 3], ) -> Result<(), String> { + crate::prof_zone!("translated dispatch"); if super::present::trace_frame() { trace_fullscreen(this, pso, [x, y, z]); } diff --git a/crates/xcl/src/hle/d3d12x/frame.rs b/crates/xcl/src/hle/d3d12x/frame.rs index 11f2c9efa9..1cdd7e5dbe 100644 --- a/crates/xcl/src/hle/d3d12x/frame.rs +++ b/crates/xcl/src/hle/d3d12x/frame.rs @@ -454,6 +454,7 @@ fn trace_calls(what: std::fmt::Arguments) { pub unsafe extern "system" fn wait_frame_event_x(_this: *mut std::ffi::c_void, ty: u32, timeout_ms: u32, block: *const u8, flags: u32, token: *mut u64) -> HRESULT { // (`XCL_LOOK_STATS=2`: a frame's time on the thread that submits, added up) let _cost = super::look_stats::deep().then(|| crate::hle::api_cost::time(crate::hle::api_cost::Call::WaitFrameEvent)); + crate::prof_zone!("WaitFrameEventX"); trace_calls(format_args!("WaitFrameEventX(type {ty}, timeout {timeout_ms} ms, block {block:p}, flags {flags:#x}, token in {:#x})", if token.is_null() { 0 } else { *token })); let scheduled = with(|c| { ((ty as usize) < TYPES && c.kinds[ty as usize].scheduled) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 3a324390bc..78ab8cbb9a 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -4915,6 +4915,7 @@ unsafe fn prepare_deferred_segment(wrapper: *mut c_void, list: *mut c_void, ty: /// segment and complete its deferred store before this wait is evaluated. A prior `Write64` to /// another cell keeps its existing split so its signal runs before the wait. unsafe fn wait_memory(list: *mut c_void, va: u64, width: u8, comparison: u32, value: u64, mask: u64, what: &str) { + crate::prof_zone!("Wait32/64BitValueX (CPU)"); static N: AtomicU64 = AtomicU64::new(0); let n = width as u64; if !va.is_multiple_of(n) { diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index 3c09818662..7181a50b26 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -54,6 +54,7 @@ static PRESENTX_LOCK: Mutex<()> = Mutex::new(()); pub unsafe extern "system" fn present_x(queue: *mut c_void, plane_count: u32, planes: *const u8, _params: *const u8) -> i32 { // (`XCL_LOOK_STATS=2`: a frame's time on the thread that submits, added up) let _cost = super::look_stats::deep().then(|| crate::hle::api_cost::time(crate::hle::api_cost::Call::Present)); + crate::prof_zone!("PresentX"); if plane_count == 0 || planes.is_null() || !crate::gpu::shader::readable(planes as usize, 0x18) { return 0; } @@ -63,6 +64,7 @@ pub unsafe extern "system" fn present_x(queue: *mut c_void, plane_count: u32, pl // The title can use flip statistics to decide when to reuse its buffer. // Queue our copy before reporting the flip. super::frame::mark_flipped(token); + crate::prof_frame!(); 0 } diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index bea254f6bd..c992dde2b3 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -464,6 +464,7 @@ fn run_build(build: &Arc, job: Job) { use windows_sys::Win32::System::Threading::{GetCurrentThread, GetCurrentThreadId, SetThreadPriority, THREAD_PRIORITY_NORMAL}; let preload = PRELOAD_WORKER.get(); BUILDING.with(|b| *b.borrow_mut() = Some(build.clone())); + crate::prof_zone!("async PSO build", if preload { "preload" } else { "on demand" }); if preload { let mut d = build.demand.lock().unwrap(); d.preload_thread = unsafe { GetCurrentThreadId() }; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index fc78555403..17d4260fed 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -5505,6 +5505,7 @@ fn driver_compile_identified(kind: &str, bytes: usize, gate: bool, record: Op let medium = gate && worker && medium_compute_compile(kind, bytes, huge_bytes, ADAPTER_VENDOR.load(Ordering::Acquire)); let huge = gate && worker && bytes >= huge_bytes; let gated = medium || huge; + crate::prof_zone!("driver PSO compile", format!("{kind}, {bytes} B DXIL{}", record.map_or(String::new(), |h| format!(", record {h:016x}, blob {}", if cached { "supplied" } else { "absent" })))); let waited = std::time::Instant::now(); let gate_stall = gated.then(|| crate::stall::span(crate::stall::StallKind::DriverCompileGate)).flatten(); let _slot = gated.then(|| { @@ -6003,6 +6004,7 @@ unsafe fn translated_compute_pso(this: *mut c_void, f: PsoFn, desc: *const c_voi } unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> Option> { + crate::prof_zone!("translate+create compute PSO"); use crate::gpu::recomp::pso::{console_to_dxil_for, is_console, Stage}; if !crate::gpu::shader::readable(desc as usize, std::mem::size_of::()) { return None; @@ -7929,6 +7931,7 @@ unsafe fn translated_graphics_pso(this: *mut c_void, f: PsoFn, desc: *const c_vo } unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void, cacheable: bool, depth_bounds: bool) -> Option { + crate::prof_zone!("translate+create graphics PSO"); use crate::gpu::recomp::pso::{console_to_dxil_for, is_console, Stage}; if !crate::gpu::shader::readable(desc as usize, std::mem::size_of::()) { return None; @@ -8650,6 +8653,7 @@ pub(crate) unsafe fn prepare_graphics(device: usize, desc: &D3D12_GRAPHICS_PIPEL } unsafe fn build_compute(dev: Dev, d: &OwnedDesc, out: *mut *mut c_void) -> bool { + crate::prof_zone!("build compute (split)"); let iid = pc_iid(Kind::PipelineState); matches!(translated_compute_pso(dev.0 as *mut c_void, dev.1, d.ptr(), &iid, out), Some(Ok(hr)) if hr >= 0) } @@ -9692,6 +9696,7 @@ unsafe fn translated_mesh_stream(this: *mut c_void, desc: *const c_void, riid: * } unsafe fn translated_mesh_stream_once(this: *mut c_void, desc: *const c_void, riid: *const GUID, ppv: *mut *mut c_void) -> Option { + crate::prof_zone!("translate+create mesh PSO"); use crate::gpu::recomp::pso::{console_mesh, is_console}; if !crate::gpu::shader::readable(desc as usize, 16) { return None; @@ -9794,6 +9799,7 @@ unsafe fn translated_mesh_stream_once(this: *mut c_void, desc: *const c_void, ri /// A graphics PSO with a console VS + GS: the GS pipeline in two passes, as a mesh pipeline /// with the description's state. unsafe fn translated_gs_pso(this: *mut c_void, d: &D3D12_GRAPHICS_PIPELINE_STATE_DESC, vs: &[u8], gs: &[u8], riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + crate::prof_zone!("translate+create GS PSO"); let rt32 = rt32_mask_d3d12(d.NumRenderTargets, &d.RTVFormats); let (tp, (ps, ps_samplers)) = match ( crate::gpu::recomp::pso::console_geometry(vs, gs, shader_bytes(&d.PS), wave32_only(this)), @@ -11834,6 +11840,7 @@ unsafe extern "system" fn fence_signal(this: *mut c_void, value: u64) -> HRESULT } unsafe extern "system" fn q_execute_command_lists(this: *mut c_void, n: u32, lists: *const *mut c_void) { + crate::prof_zone!("ExecuteCommandLists", format!("{n} lists")); type F = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void); let f: F = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12CommandQueue", "ExecuteCommandLists"))); // Internal XCL submits (`sparse` uploads/readbacks, `title_textures` uploads, tile zeroing) diff --git a/crates/xcl/src/hle/dstorage.rs b/crates/xcl/src/hle/dstorage.rs index 92cd2e86d3..028afb559d 100644 --- a/crates/xcl/src/hle/dstorage.rs +++ b/crates/xcl/src/hle/dstorage.rs @@ -1267,6 +1267,7 @@ impl QueueInner { #[inline] fn run_entry(&self, e: Entry, since_status: &mut HRESULT, file_input: Option<&mut Vec>) { + crate::prof_zone!("DirectStorage entry"); if self.closed.load(Ordering::Acquire) { if let Entry::Status(s, i) = e { if let Some(slot) = s.slots.get(i as usize) { diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index f76e9a7a8d..a066101523 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -66,6 +66,7 @@ pub fn host_main(boot_path: &Path) -> i32 { boot.config.log_to_stderr, ); crate::log::log_panics(); + crate::prof::start(); crate::xlog_info!("XCL {} – host for '{}'", env!("CARGO_PKG_VERSION"), boot.title); crate::xlog_info!("game: {} (read-only folder: {})", boot.exe_path.display(), boot.game_root.display()); crate::xlog_info!("XCL data: {}", boot.data_dir.display()); @@ -234,6 +235,13 @@ pub fn host_main(boot_path: &Path) -> i32 { if let Some(over) = crate::hle::d3d12x::real::over_video_budget(local, budget) { crate::xlog_warn!("video memory: {} MB in use, {} MB over the budget of {} MB Windows gives this process", mb(local), mb(over), mb(budget)); } + crate::prof_plot!("commit MB", mb(private as u64)); + crate::prof_plot!("video local MB", mb(local)); + crate::prof_plot!("video non-local MB", mb(non_local)); + crate::prof_plot!("presents / 2 s", d[0]); + crate::prof_plot!("draws / 2 s", d[2]); + crate::prof_plot!("dispatches / 2 s", d[3]); + crate::prof_plot!("DirectStorage MB / 2 s", d[9]); last = Some(now); } }); diff --git a/crates/xcl/src/lib.rs b/crates/xcl/src/lib.rs index df5aada1b7..dedff35c39 100644 --- a/crates/xcl/src/lib.rs +++ b/crates/xcl/src/lib.rs @@ -23,6 +23,7 @@ pub mod loader; pub mod modules; pub mod pe; pub mod preflight; +pub mod prof; pub mod report; pub mod resolver; pub mod rpf; diff --git a/crates/xcl/src/prof.rs b/crates/xcl/src/prof.rs new file mode 100644 index 0000000000..4a07f56f9c --- /dev/null +++ b/crates/xcl/src/prof.rs @@ -0,0 +1,83 @@ +//! Tracy instrumentation (`--features profile-tracy`); without the feature every macro here +//! expands to nothing. Zones are only recorded once `start()` ran (the host process), so the +//! launcher, `xcl shaders` and tests are unaffected even with the feature on. + +/// Starts the Tracy client in this process (host mode only). +pub fn start() { + #[cfg(feature = "profile-tracy")] + { + let c = tracy_client::Client::start(); + c.set_thread_name("xcl-host-main"); + c.message("xcl host started", 0); + } +} + +/// What the title's `_register_thread_local_exe_atexit_callback` import gets with the feature +/// on. Tracy's C++ `thread_local`s make xcl.exe's own CRT startup register a callback, and the +/// UCRT takes one per process: the title's CRT startup registering its own would `terminate()` +/// (0xC0000409). The title's main-thread `thread_local` destructors are then not run at exit; +/// the other threads' run from its TLS callbacks as before. +#[cfg(feature = "profile-tracy")] +pub unsafe extern "C" fn register_thread_local_exe_atexit_callback(_callback: usize) {} + +/// A zone for the rest of the enclosing block: `prof_zone!("name")`, or +/// `prof_zone!("name", text_expr)` to attach a text (evaluated only while profiling). +#[macro_export] +macro_rules! prof_zone { + ($name:literal) => { + #[cfg(feature = "profile-tracy")] + let _prof_zone = tracy_client::Client::running().map(|c| c.span(tracy_client::span_location!($name), 0)); + }; + ($name:literal, $text:expr) => { + #[cfg(feature = "profile-tracy")] + let _prof_zone = tracy_client::Client::running().map(|c| { + let s = c.span(tracy_client::span_location!($name), 0); + s.emit_text(&$text); + s + }); + }; +} + +/// The end of a presented frame. +#[macro_export] +macro_rules! prof_frame { + () => { + #[cfg(feature = "profile-tracy")] + if let Some(c) = tracy_client::Client::running() { + c.frame_mark(); + } + }; +} + +/// A value on a named plot. +#[macro_export] +macro_rules! prof_plot { + ($name:literal, $value:expr) => { + #[cfg(feature = "profile-tracy")] + if let Some(c) = tracy_client::Client::running() { + c.plot(tracy_client::plot_name!($name), $value as f64); + } + }; +} + +#[cfg(test)] +mod tests { + // Tests never call `start()`: the macros must be no-ops without a running client (with the + // feature on, `Client::running()` is `None`; off, they expand to nothing), and a zone's text + // must not be evaluated then. + #[test] + fn macros_are_no_ops_without_a_running_client() { + let evaluated = std::cell::Cell::new(false); + let _text = || { + evaluated.set(true); + String::from("text") + }; + { + crate::prof_zone!("plain zone"); + crate::prof_zone!("zone with text", _text()); + crate::prof_plot!("plot", 1u64); + crate::prof_frame!(); + } + assert!(!evaluated.get(), "a zone's text was built without a running client"); + } +} diff --git a/crates/xcl/src/resolver.rs b/crates/xcl/src/resolver.rs index 72dbacae7b..e6c977e154 100644 --- a/crates/xcl/src/resolver.rs +++ b/crates/xcl/src/resolver.rs @@ -324,6 +324,10 @@ pub fn resolve_import(dll: &str, what: &ImportName, importer: &str, delay: bool) if matches!(source, Source::GameDll(_)) && crate::hle::bink::is_bink_dll(&key) { addr = crate::hle::bink::wrap(&fname(what), addr); } + #[cfg(feature = "profile-tracy")] + if fname(what) == "_register_thread_local_exe_atexit_callback" { + addr = crate::prof::register_thread_local_exe_atexit_callback as usize; + } crate::xlog_debug!("import {importer}: {key}!{what} → {source:?}"); if let Some(f) = trace_filter() { if f.wants(&key, &fname(what), &source) {