From e3bbc955bcdf1d04e4072643ebdd914098ac025e Mon Sep 17 00:00:00 2001
From: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
Date: Tue, 24 Nov 2026 18:38:18 +0530
Subject: [PATCH] ffi: share one Metal device and queue across frame-loop
sessions (#290)
Each frame-loop session opened its own Metal command queue. The GPU
driver allocates a per-queue working set on the first command buffer
(about 45 MiB on an Apple M5 with validation) and frees it
asynchronously 10-50 ms after the queue is released, so the footprint
read right after a session ended swung by about 50 MiB and the
repeated-sessions footprint test failed intermittently (5 of 30 runs).
There was no leak. Sessions now share one process-wide NativeCompute,
which is Send + Sync, so the working set is allocated once and kept.
Remove the settled_footprint wait added in #292: the original test
measures the immediate footprint again. Add a 50-session regression
test (completion, pause and resume, destroy while running) and a
concurrent two-session test for shared-queue correctness and session
isolation.
Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com>
---
CHANGELOG.md | 1 +
crates/ffi/src/frame_loop.rs | 27 +++++-
crates/ffi/tests/frame_loop.rs | 160 ++++++++++++++++++++++++++++-----
3 files changed, 163 insertions(+), 25 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 01c019c..f56e089 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -196,6 +196,7 @@ remains unsupported.
### Fixed
+- Fixed the intermittent failure of the frame-loop memory-footprint test (#290). Each frame-loop session opened its own Metal command queue. The first command buffer on a queue makes the GPU driver allocate a working set for it (about 45 MiB on an Apple M5 with Metal validation), and the driver frees it asynchronously 10–50 ms after the queue is released, so the process footprint read right after a session ended depended on how far that release had got. Measured immediately after each of 50 destroys, it ranged from 45 to 96 MiB; the unchanged test failed 5 times in 30 runs, with up to 26 MiB of apparent growth. There was no leak: one second after each destroy the footprint was back at the same floor every time. Frame-loop sessions now share one process-wide Metal device and command queue, so the working set is allocated once and kept: the same readings stay within 93–98 MiB across 50 sessions, and the unchanged test passed 30 runs in a row (largest growth 0.31 MiB). The cost is that the process keeps that GPU memory (about 50 MiB with validation, 58 MiB total footprint without) after its first frame loop. A new 50-session test (normal completion, pause and resume, destroy while running) checks the readings stay within 16 MiB; it fails without the fix.
- Stop ignoring `fixtures/pe/trusted-runner.exe`. The runner embeds it with `include_bytes!`, so ignoring the file made a fresh checkout fail to compile.
- Corrected the ARM64 STP pre-index encoder, which had a hardcoded -32-byte immediate and therefore adjusted the host stack by the wrong amount once the translator frame grew to 48 bytes. The wrong frame silently corrupted the caller's stack and caused SIGSEGV in every native translation test. Emitter encodings are now checked against LLVM's assembler, and a test asserts that each translated block opens and closes the same 48-byte frame.
- Translated guest memory faults now stop the block before it commits further architectural effects. A faulting push, call or store leaves guest RSP and registers unchanged, the reported instruction count covers only completed work, and guest RIP identifies the faulting instruction. Previously the block continued after the helper recorded a fault, so later instructions and writes still took effect.
diff --git a/crates/ffi/src/frame_loop.rs b/crates/ffi/src/frame_loop.rs
index 8357944..1f85a9a 100644
--- a/crates/ffi/src/frame_loop.rs
+++ b/crates/ffi/src/frame_loop.rs
@@ -10,7 +10,7 @@
//! the guest leaves its loop and halts.
use std::cell::RefCell;
-use std::sync::{Arc, Condvar, Mutex, MutexGuard};
+use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock};
use std::thread::JoinHandle;
use xenora_loader::startup::{StartupLayout, run_synthetic_startup};
@@ -171,8 +171,8 @@ impl FrameLoop {
include_bytes!("../../../fixtures/rdna2/x9-constant-ps.bin"),
)
.map_err(|e| format!("shader translation: {e}"))?;
- let gpu = NativeCompute::new().map_err(|e| format!("Metal device: {e}"))?;
- // The session's GPU resources live on the guest thread, where the import runs.
+ let gpu = shared_gpu()?;
+ // The import runs on the guest thread, so the session's shader lives there too.
GPU.with(|slot| *slot.borrow_mut() = Some((gpu, shader.msl)));
let shared = Arc::clone(self);
@@ -268,7 +268,26 @@ impl FrameLoop {
}
thread_local! {
- static GPU: RefCell