diff --git a/backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h b/backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h new file mode 100644 index 0000000..c572c8a --- /dev/null +++ b/backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h @@ -0,0 +1,117 @@ +#pragma once + +#include + +// Private, optional, copied-data telemetry. This interface does not alter +// API1-7 or the diagnostics/lifecycle vtables. All timestamps use the same +// QPC-derived nanosecond clock as BridgeMeasurementRuntime::NowNs(). +inline constexpr GUID IID_D3D9BridgeTelemetry = + {0x5722e0c7,0xa495,0x4e93,{0xb7,0xcd,0x13,0x87,0x8c,0xb2,0x94,0x31}}; +inline constexpr UINT D3D9_BRIDGE_TELEMETRY_VERSION = 1; +inline constexpr UINT D3D9_BRIDGE_TELEMETRY_MAX_EVENTS = 65536; +inline constexpr UINT D3D9_BRIDGE_TELEMETRY_MAX_CHUNKS = 4096; +inline constexpr UINT D3D9_BRIDGE_TELEMETRY_MAX_MASKS = 65536; + +enum D3D9BridgeTelemetryKind : UINT { + D3D9_BRIDGE_TELEMETRY_ALLOC = 1, + D3D9_BRIDGE_TELEMETRY_FREE, + D3D9_BRIDGE_TELEMETRY_MAP, + D3D9_BRIDGE_TELEMETRY_UNMAP, + D3D9_BRIDGE_TELEMETRY_CS_WAIT, + D3D9_BRIDGE_TELEMETRY_RESOURCE_WAIT, + D3D9_BRIDGE_TELEMETRY_GPU_WAIT, + // CPU waiting for submit/present status, not GPU completion. + D3D9_BRIDGE_TELEMETRY_SUBMISSION_WAIT, + // Signal was observed incomplete before the frame-latency wait call. + // It may become ready before the calling thread actually blocks. + D3D9_BRIDGE_TELEMETRY_FRAME_LATENCY_WAIT, + // Inclusive acquireNextImage call, including CPU, driver and any waits. + D3D9_BRIDGE_TELEMETRY_PRESENT_ACQUIRE, + // Sampler-pool pressure: waiting for a submission fence observed incomplete. + D3D9_BRIDGE_TELEMETRY_SAMPLER_POOL_WAIT, + // Enqueue-to-completion latency of unreachable file mappings, not CPU time. + D3D9_BRIDGE_TELEMETRY_MAPPING_RETIRE, + D3D9_BRIDGE_TELEMETRY_CS_CHUNK, + D3D9_BRIDGE_TELEMETRY_COMMAND_ALLOCATE, + D3D9_BRIDGE_TELEMETRY_COMMAND_BEGIN, + D3D9_BRIDGE_TELEMETRY_COMMAND_RESET, +}; +enum D3D9BridgeTelemetryFlags : UINT { + D3D9_BRIDGE_TELEMETRY_FILE_BACKED = 1u << 0, + D3D9_BRIDGE_TELEMETRY_FAILED = 1u << 1, + D3D9_BRIDGE_TELEMETRY_RESOURCE = 1u << 2, + D3D9_BRIDGE_TELEMETRY_DEVICE_MEMORY = 1u << 3, + D3D9_BRIDGE_TELEMETRY_OS_MAPPING = 1u << 4, + // GPU_WAIT includes queue draining plus vkDeviceWaitIdle. + D3D9_BRIDGE_TELEMETRY_DEVICE_IDLE = 1u << 5, + D3D9_BRIDGE_TELEMETRY_CACHE_HIT = 1u << 6, + D3D9_BRIDGE_TELEMETRY_CACHE_RETURN = 1u << 7, + D3D9_BRIDGE_TELEMETRY_CACHE_FILL = 1u << 8, + D3D9_BRIDGE_TELEMETRY_CACHE_EVICT = 1u << 9, + D3D9_BRIDGE_TELEMETRY_DEFERRED = 1u << 10, + D3D9_BRIDGE_TELEMETRY_HIGH_PRIORITY = 1u << 11, + D3D9_BRIDGE_TELEMETRY_SECONDARY = 1u << 12, +}; +enum D3D9BridgeMemoryFlags : UINT { + D3D9_BRIDGE_MEMORY_CHUNKS_TRUNCATED = 1u << 0, + D3D9_BRIDGE_MEMORY_MASKS_TRUNCATED = 1u << 1, +}; + +struct D3D9BridgeTelemetryConfig { + UINT StructSize, Version; + UINT64 CaptureId; + UINT MaxEvents, Reserved; + // Successful spans are filtered only if BOTH duration and bytes are below + // these thresholds. Zero thresholds retain all completed spans. Failures + // remain eligible regardless of size and duration. + UINT64 MinDurationNs, MinBytes; +}; +struct D3D9BridgeTelemetryEvent { + UINT64 CaptureId, BeginNs, EndNs, Identity, Bytes; + UINT Kind, ThreadId; + INT Result; + UINT Flags; +}; +struct D3D9BridgeTelemetryRead { + UINT StructSize, Version; + UINT64 CaptureId, Dropped, Filtered; + // Events are completed spans only. InFlightAtStop records spans still open + // at the stop boundary; they cannot be interpreted as completed timings. + UINT64 Started, InFlight, InFlightAtStop, LateDropped; + UINT Written, Remaining, Capacity, Active; +}; +struct D3D9BridgeMemoryChunk { + UINT64 Cookie, Capacity, Used; + UINT Heap, Type, Mapped, Active; + // Offset/count refer to the caller's mask buffer. If the entire mask for a + // chunk cannot fit, MaskCount is zero and MASKS_TRUNCATED is set. + UINT PageCount, MaskOffset, MaskCount, Reserved; +}; +struct D3D9BridgeMemorySnapshot { + UINT StructSize, Version; + // Timestamp is taken under the allocator lock. ScanDurationNs includes + // time spent waiting for that lock. Totals describe HUD-visible pooled + // chunks, not dedicated allocations. + UINT64 CaptureId, TimestampNs, ScanDurationNs, TotalCapacity, TotalUsed; + UINT TotalChunks, CopiedChunks, TotalMasks, CopiedMasks, Flags, Reserved; +}; + +// Calls may run concurrently. Capture IDs must increase strictly over the +// device lifetime, so an old reader cannot consume a later generation. +// Start creates a new generation only when no +// capture is active. Stop is idempotent for that generation; completed data +// remains readable until the next Start. A stale/mismatched ID never consumes +// events of the current generation. Late operation completions from older +// generations are discarded. ReadEvents consumes copied entries in order; +// pass capacity zero and a null array to inspect counters without consuming. +// GetMemorySnapshot succeeds only for the active capture; its arrays and all +// returned data are caller owned. No recorder lock spans device operations, +// waits, or allocator lock acquisition. +struct ID3D9BridgeTelemetry : IUnknown { + virtual HRESULT STDMETHODCALLTYPE StartCapture(const D3D9BridgeTelemetryConfig*) = 0; + virtual HRESULT STDMETHODCALLTYPE StopCapture(UINT64 captureId) = 0; + virtual HRESULT STDMETHODCALLTYPE ReadEvents(UINT64 captureId, D3D9BridgeTelemetryRead*, + D3D9BridgeTelemetryEvent*, UINT capacity) = 0; + virtual HRESULT STDMETHODCALLTYPE GetMemorySnapshot(UINT64 captureId, D3D9BridgeMemorySnapshot*, + D3D9BridgeMemoryChunk*, UINT chunkCapacity, UINT* masks, UINT maskCapacity) = 0; +}; diff --git a/backend/dxvk/src/d3d9/d3d9_common_texture.h b/backend/dxvk/src/d3d9/d3d9_common_texture.h index 5989859..add4f3c 100644 --- a/backend/dxvk/src/d3d9/d3d9_common_texture.h +++ b/backend/dxvk/src/d3d9/d3d9_common_texture.h @@ -161,6 +161,14 @@ namespace dxvk { */ void* GetData(UINT Subresource); + // Used only by initial texture creation, before any client can write it. + bool HasZeroInitializedMemory() const { + return !m_buffer && m_data.wasInitiallyZero(); + } + + bool WasMappingUsed() const { return m_mappingWasUsed; } + void MarkMappingUsed() { m_mappingWasUsed = true; } + const Rc& GetBuffer(); DxvkBufferSlice GetBufferSlice(UINT Subresource); @@ -505,6 +513,7 @@ namespace dxvk { Rc m_resolveImage; Rc m_buffer; MemoryFileRegion m_data = { }; + bool m_mappingWasUsed = false; D3D9SubresourceArray< uint64_t> m_seqs = { }; diff --git a/backend/dxvk/src/d3d9/d3d9_device.cpp b/backend/dxvk/src/d3d9/d3d9_device.cpp index a1d8a2a..41f3d13 100644 --- a/backend/dxvk/src/d3d9/d3d9_device.cpp +++ b/backend/dxvk/src/d3d9/d3d9_device.cpp @@ -351,6 +351,38 @@ namespace dxvk { HRESULT STDMETHODCALLTYPE D3D9BridgeThreadStats::GetThreadStats(D3D9BridgeThreadSnapshot* output) { return m_device->GetBridgeThreadStats(output); } + ULONG STDMETHODCALLTYPE D3D9BridgeTelemetry::AddRef() { return m_device->AddRef(); } + ULONG STDMETHODCALLTYPE D3D9BridgeTelemetry::Release() { return m_device->Release(); } + HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::QueryInterface(REFIID iid, void** output) { + return m_device->QueryInterface(iid, output); + } + HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::StartCapture(const D3D9BridgeTelemetryConfig* c) { + return m_device->GetDXVKDevice()->telemetry().start(c); + } + HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::StopCapture(UINT64 id) { + return m_device->GetDXVKDevice()->telemetry().stop(id); + } + HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::ReadEvents(UINT64 id, D3D9BridgeTelemetryRead* out, + D3D9BridgeTelemetryEvent* events, UINT capacity) { + return m_device->GetDXVKDevice()->telemetry().read(id,out,events,capacity); + } + HRESULT STDMETHODCALLTYPE D3D9BridgeTelemetry::GetMemorySnapshot(UINT64 id, D3D9BridgeMemorySnapshot* out, + D3D9BridgeMemoryChunk* chunks, UINT chunkCapacity, UINT* masks, UINT maskCapacity) { + if (!out) return E_POINTER; + if (out->StructSize!=sizeof(*out) || out->Version!=D3D9_BRIDGE_TELEMETRY_VERSION || + chunkCapacity>D3D9_BRIDGE_TELEMETRY_MAX_CHUNKS || maskCapacity>D3D9_BRIDGE_TELEMETRY_MAX_MASKS || + (chunkCapacity && !chunks) || (maskCapacity && !masks)) return D3DERR_INVALIDCALL; + auto device=m_device->GetDXVKDevice(); auto& recorder=device->telemetry(); + const auto generation=recorder.activeGeneration(id); + if (!generation) return D3DERR_INVALIDCALL; + D3D9BridgeMemorySnapshot snapshot{}; + snapshot.StructSize=sizeof(snapshot); snapshot.Version=D3D9_BRIDGE_TELEMETRY_VERSION; + snapshot.CaptureId=id; const auto scanStart=DxvkTelemetry::nowNs(); + device->getTelemetryMemorySnapshot(snapshot,chunks,chunkCapacity,masks,maskCapacity); + snapshot.ScanDurationNs=DxvkTelemetry::nowNs()-scanStart; + if (!recorder.isGenerationActive(id,generation)) return D3DERR_INVALIDCALL; + *out=snapshot; return S_OK; + } HRESULT D3D9DeviceEx::GetBridgeThreadStats(D3D9BridgeThreadSnapshot* output) { if (!output || output->StructSize != sizeof(*output) || output->Version != 1) return D3DERR_INVALIDCALL; @@ -533,6 +565,12 @@ namespace dxvk { , m_d3d8Bridge ( this ) , m_bridgeDiagnostics ( this ) { + m_memoryAllocator.setTelemetry(&m_dxvkDevice->telemetry()); +#ifdef DXVK_USE_UNMAPPABLE_MEMORY + m_memoryAllocator.enableRetirement(); +#endif + m_shaderAllocator.setTelemetry(&m_dxvkDevice->telemetry()); + InitShaderOptions(); // If we can SWVP, then we use an extended constant set @@ -657,6 +695,11 @@ namespace dxvk { return S_OK; } + if (riid == IID_D3D9BridgeTelemetry) { + *ppvObject = ref(&m_bridgeTelemetry); + return S_OK; + } + if (riid == IID_D3D9BridgeDiagnostics) { *ppvObject = ref(&m_bridgeDiagnostics); return S_OK; @@ -6267,6 +6310,8 @@ namespace dxvk { ExecuteFlush(false); SynchronizeCsThread(SequenceNumber); + DxvkTelemetryScope scope(&m_dxvkDevice->telemetry(),D3D9_BRIDGE_TELEMETRY_RESOURCE_WAIT, + reinterpret_cast(&Resource)); m_dxvkDevice->waitForResource(Resource, access); } } @@ -6419,6 +6464,14 @@ namespace dxvk { // Don't use MapTexture here to keep the mapped list small while the resource is still locked. void* mapPtr = pResource->GetData(Subresource); +#ifdef DXVK_USE_UNMAPPABLE_MEMORY + if (pResource->GetMapMode() == D3D9_COMMON_TEXTURE_MAP_MODE_UNMAPPABLE + && !pResource->WasMappingUsed()) { + pResource->MarkMappingUsed(); + m_mappedTextures.touch(pResource); + } +#endif + if (unlikely(needsReadback)) { // The texture was written to on the GPU. // This can be either the image (for D3DPOOL_DEFAULT) @@ -9417,7 +9470,13 @@ namespace dxvk { uint64_t submissionId = m_submissionFence->value(); while (++submissionId <= m_submissionId) { - m_submissionFence->wait(submissionId); + if (unlikely(m_dxvkDevice->telemetry().enabled() && m_submissionFence->value()telemetry(),D3D9_BRIDGE_TELEMETRY_SAMPLER_POOL_WAIT, + reinterpret_cast(m_submissionFence.ptr())); + m_submissionFence->wait(submissionId); + } else { + m_submissionFence->wait(submissionId); + } // Need to manually update sampler stats here since we // might otherwise hit this path again the next time @@ -10867,6 +10926,7 @@ namespace dxvk { #ifdef DXVK_USE_UNMAPPABLE_MEMORY if (likely(pTexture->GetMapMode() == D3D9_COMMON_TEXTURE_MAP_MODE_UNMAPPABLE)) { + pTexture->MarkMappingUsed(); m_mappedTextures.insert(pTexture); } #endif @@ -10875,12 +10935,54 @@ namespace dxvk { } + void D3D9DeviceEx::RetainInitializedTextureMapping(D3D9CommonTexture* pTexture) { +#ifdef DXVK_USE_UNMAPPABLE_MEMORY + if (pTexture->GetMapMode() != D3D9_COMMON_TEXTURE_MAP_MODE_UNMAPPABLE) + return; + + D3D9DeviceLock lock = LockDevice(); + // Initial mappings form a cold prefix of the LRU until actually accessed. + // Replace only this prefix during admission, never the hot working set. + const uint64_t budget = uint64_t(m_d3d9Options.textureMemory); + if (m_memoryAllocator.getStats().memoryMapped > budget) + m_memoryAllocator.drainRetired(); + auto iter = m_mappedTextures.leastRecentlyUsedIter(); + while (m_memoryAllocator.getStats().memoryMapped > budget + && iter != m_mappedTextures.leastRecentlyUsedEndIter() + && !(*iter)->WasMappingUsed()) { + if ((*iter)->IsAnySubresourceLocked() + || (m_textureUploadMappingScope && m_textureUploadMappingScope->Contains(*iter))) { + ++iter; + continue; + } + (*iter)->UnmapData(); + iter = m_mappedTextures.remove(iter); + } + if (m_memoryAllocator.getStats().memoryMapped > budget) { + pTexture->UnmapData(); + UnmapTextures(); + return; + } + + try { + m_mappedTextures.insert(pTexture, false); + } catch (const std::bad_alloc&) { + // A cache admission failure must not turn successful initialization into + // resource creation failure, nor leave an untracked resident mapping. + pTexture->UnmapData(); + } + UnmapTextures(); +#endif + } + + void D3D9DeviceEx::TouchMappedTexture(D3D9CommonTexture* pTexture) { #ifdef DXVK_USE_UNMAPPABLE_MEMORY if (pTexture->GetMapMode() != D3D9_COMMON_TEXTURE_MAP_MODE_UNMAPPABLE) return; D3D9DeviceLock lock = LockDevice(); + pTexture->MarkMappingUsed(); m_mappedTextures.touch(pTexture); #endif } @@ -10901,15 +11003,18 @@ namespace dxvk { // Will only be called inside the device lock #ifdef DXVK_USE_UNMAPPABLE_MEMORY - uint32_t mappedMemory = m_memoryAllocator.getStats().memoryMapped; + const uint64_t budget = uint64_t(m_d3d9Options.textureMemory); + const uint64_t mappedMemory = m_memoryAllocator.getStats().memoryMapped; - if (likely(mappedMemory < uint32_t(m_d3d9Options.textureMemory))) + if (likely(!budget || mappedMemory <= budget)) return; - uint32_t threshold = (m_d3d9Options.textureMemory / 4) * 3; + // Pending final releases still count against this budget. Reclaim them + // before evicting a live, useful texture mapping. + m_memoryAllocator.drainRetired(); auto iter = m_mappedTextures.leastRecentlyUsedIter(); - while (m_memoryAllocator.getStats().memoryMapped >= threshold && iter != m_mappedTextures.leastRecentlyUsedEndIter()) { + while (m_memoryAllocator.getStats().memoryMapped > budget && iter != m_mappedTextures.leastRecentlyUsedEndIter()) { if (unlikely((*iter)->IsAnySubresourceLocked() != 0 || (m_textureUploadMappingScope && m_textureUploadMappingScope->Contains(*iter)))) { iter++; diff --git a/backend/dxvk/src/d3d9/d3d9_device.h b/backend/dxvk/src/d3d9/d3d9_device.h index a8a2028..b4e8634 100644 --- a/backend/dxvk/src/d3d9/d3d9_device.h +++ b/backend/dxvk/src/d3d9/d3d9_device.h @@ -57,6 +57,21 @@ namespace dxvk { D3D9DeviceEx* m_device; }; + class D3D9BridgeTelemetry final : public ID3D9BridgeTelemetry { + public: + explicit D3D9BridgeTelemetry(D3D9DeviceEx* device) : m_device(device) { } + ULONG STDMETHODCALLTYPE AddRef() override; + ULONG STDMETHODCALLTYPE Release() override; + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID, void**) override; + HRESULT STDMETHODCALLTYPE StartCapture(const D3D9BridgeTelemetryConfig*) override; + HRESULT STDMETHODCALLTYPE StopCapture(UINT64) override; + HRESULT STDMETHODCALLTYPE ReadEvents(UINT64, D3D9BridgeTelemetryRead*, D3D9BridgeTelemetryEvent*, UINT) override; + HRESULT STDMETHODCALLTYPE GetMemorySnapshot(UINT64, D3D9BridgeMemorySnapshot*, + D3D9BridgeMemoryChunk*, UINT, UINT*, UINT) override; + private: + D3D9DeviceEx* m_device; + }; + // Delegating private interface: appending this helper preserves all existing // D3D9DeviceEx member offsets and never takes a self-reference. class D3D9BridgeDiagnostics final : public ID3D9BridgeDiagnostics { @@ -1278,6 +1293,9 @@ namespace dxvk { */ void* MapTexture(D3D9CommonTexture* pTexture, UINT Subresource); + /** Registers an already initialized mapping, then enforces its budget. */ + void RetainInitializedTextureMapping(D3D9CommonTexture* pTexture); + /** * \brief Moves the texture to the front of the LRU list of mapped textures */ @@ -1992,6 +2010,7 @@ namespace dxvk { D3D9BridgeDiagnostics m_bridgeDiagnostics; D3D9BridgeThreadStats m_threadStats { this }; + D3D9BridgeTelemetry m_bridgeTelemetry { this }; }; } diff --git a/backend/dxvk/src/d3d9/d3d9_initializer.cpp b/backend/dxvk/src/d3d9/d3d9_initializer.cpp index 8ff313e..ab20a31 100644 --- a/backend/dxvk/src/d3d9/d3d9_initializer.cpp +++ b/backend/dxvk/src/d3d9/d3d9_initializer.cpp @@ -57,10 +57,14 @@ namespace dxvk { if (mapPtr != nullptr) { InitHostVisibleTexture(pTexture, pInitialData, mapPtr); - pTexture->UnmapData(); } SyncSharedTexture(pTexture); + + // Publish only once initialization and shared synchronization no longer + // access the mapped pointer. Creation does not hold the device lock. + if (mapPtr != nullptr) + m_parent->RetainInitializedTextureMapping(pTexture); } @@ -135,7 +139,7 @@ namespace dxvk { pTexture->Desc()->ArraySize, formatInfo, VK_IMAGE_ASPECT_COLOR_BIT); - } else { + } else if (!pTexture->HasZeroInitializedMemory()) { // All subresources are allocated in one chunk of memory. // So we can just get the pointer for subresource 0 and memset all of them at once. std::memset( diff --git a/backend/dxvk/src/d3d9/d3d9_swapchain.cpp b/backend/dxvk/src/d3d9/d3d9_swapchain.cpp index 496f08b..bba6c00 100644 --- a/backend/dxvk/src/d3d9/d3d9_swapchain.cpp +++ b/backend/dxvk/src/d3d9/d3d9_swapchain.cpp @@ -853,7 +853,14 @@ namespace dxvk { PresenterSync sync = { }; Rc backBuffer; - status = m_wctx->presenter->acquireNextImage(sync, backBuffer); + if (unlikely(m_device->telemetry().enabled())) { + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_PRESENT_ACQUIRE, + reinterpret_cast(m_wctx->presenter.ptr())); + status = m_wctx->presenter->acquireNextImage(sync, backBuffer); + scope.outcome(status); + } else { + status = m_wctx->presenter->acquireNextImage(sync, backBuffer); + } if (status >= 0 && status != VK_NOT_READY) { VkRect2D srcRect = { @@ -1165,7 +1172,15 @@ namespace dxvk { void D3D9SwapChainEx::SyncFrameLatency() { // Wait for the sync event so that we respect the maximum frame latency - m_wctx->frameLatencySignal->wait(m_wctx->frameId - GetActualFrameLatency()); + const auto target=m_wctx->frameId - GetActualFrameLatency(); + auto& signal=m_wctx->frameLatencySignal; + if (unlikely(m_device->telemetry().enabled() && signal->value()telemetry(),D3D9_BRIDGE_TELEMETRY_FRAME_LATENCY_WAIT, + reinterpret_cast(signal.ptr())); + signal->wait(target); + } else { + signal->wait(target); + } } uint32_t D3D9SwapChainEx::GetActualFrameLatency() { diff --git a/backend/dxvk/src/dxvk/dxvk_cmdlist.cpp b/backend/dxvk/src/dxvk/dxvk_cmdlist.cpp index 26dd159..ec2a86b 100644 --- a/backend/dxvk/src/dxvk/dxvk_cmdlist.cpp +++ b/backend/dxvk/src/dxvk/dxvk_cmdlist.cpp @@ -135,7 +135,12 @@ namespace dxvk { VkCommandBuffer commandBuffer = VK_NULL_HANDLE; - if (vk->vkAllocateCommandBuffers(vk->device(), &allocInfo, &commandBuffer)) + VkResult status; + { DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_COMMAND_ALLOCATE,uint64_t(m_commandPool)); + status = vk->vkAllocateCommandBuffers(vk->device(), &allocInfo, &commandBuffer); + scope.outcome(status); + } + if (status) throw DxvkError("DxvkCommandPool: Failed to allocate command buffer"); m_primaryBuffers.push_back(commandBuffer); @@ -148,7 +153,12 @@ namespace dxvk { VkCommandBufferBeginInfo info = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - if (vk->vkBeginCommandBuffer(commandBuffer, &info)) + VkResult status; + { DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_COMMAND_BEGIN,uint64_t(commandBuffer)); + status = vk->vkBeginCommandBuffer(commandBuffer, &info); + scope.outcome(status); + } + if (status) throw DxvkError("DxvkCommandPool: Failed to begin command buffer"); if (m_device->debugFlags().test(DxvkDebugFlag::Capture)) { @@ -186,7 +196,12 @@ namespace dxvk { VkCommandBuffer commandBuffer = VK_NULL_HANDLE; - if (vk->vkAllocateCommandBuffers(vk->device(), &allocInfo, &commandBuffer)) + VkResult status; + { DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_COMMAND_ALLOCATE,uint64_t(m_commandPool),0,D3D9_BRIDGE_TELEMETRY_SECONDARY); + status = vk->vkAllocateCommandBuffers(vk->device(), &allocInfo, &commandBuffer); + scope.outcome(status); + } + if (status) throw DxvkError("DxvkCommandPool: Failed to allocate secondary command buffer"); m_secondaryBuffers.push_back(commandBuffer); @@ -200,7 +215,12 @@ namespace dxvk { | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT; info.pInheritanceInfo = &inheritanceInfo; - if (vk->vkBeginCommandBuffer(commandBuffer, &info)) + VkResult status; + { DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_COMMAND_BEGIN,uint64_t(commandBuffer),0,D3D9_BRIDGE_TELEMETRY_SECONDARY); + status = vk->vkBeginCommandBuffer(commandBuffer, &info); + scope.outcome(status); + } + if (status) throw DxvkError("DxvkCommandPool: Failed to begin secondary command buffer"); return commandBuffer; @@ -211,7 +231,12 @@ namespace dxvk { auto vk = m_device->vkd(); if (m_nextPrimary || m_nextSecondary) { - if (vk->vkResetCommandPool(vk->device(), m_commandPool, 0)) + VkResult status; + { DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_COMMAND_RESET,uint64_t(m_commandPool)); + status = vk->vkResetCommandPool(vk->device(), m_commandPool, 0); + scope.outcome(status); + } + if (status) throw DxvkError("DxvkCommandPool: Failed to reset command pool"); m_nextPrimary = 0; diff --git a/backend/dxvk/src/dxvk/dxvk_cs.cpp b/backend/dxvk/src/dxvk/dxvk_cs.cpp index 6ae4673..a884364 100644 --- a/backend/dxvk/src/dxvk/dxvk_cs.cpp +++ b/backend/dxvk/src/dxvk/dxvk_cs.cpp @@ -179,6 +179,8 @@ namespace dxvk { } if (synchronize) { + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_CS_WAIT,timeline,0, + queue==DxvkCsQueue::HighPriority ? D3D9_BRIDGE_TELEMETRY_HIGH_PRIORITY : 0); std::unique_lock lock(m_counterMutex); m_condOnSync.wait(lock, [this, queue, timeline] { @@ -198,6 +200,8 @@ namespace dxvk { if (seq == SynchronizeAll) seq = m_queueOrdered.seqDispatch; + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_CS_WAIT,seq); + auto t0 = dxvk::high_resolution_clock::now(); { std::unique_lock lock(m_counterMutex); @@ -281,6 +285,10 @@ namespace dxvk { m_context->addStatCtr(DxvkStatCounter::CsChunkCount, 1); + // Sequence zero on unsynchronized high-priority chunks is intentional. + // Queue flags and time/thread overlap distinguish them from ordered work. + DxvkTelemetryScope chunkScope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_CS_CHUNK,entry.seq,0, + isHighPrio ? D3D9_BRIDGE_TELEMETRY_HIGH_PRIORITY : 0); entry.chunk->executeAll(m_context.ptr()); if (entry.seq) { diff --git a/backend/dxvk/src/dxvk/dxvk_device.cpp b/backend/dxvk/src/dxvk/dxvk_device.cpp index d66947d..728e53c 100644 --- a/backend/dxvk/src/dxvk/dxvk_device.cpp +++ b/backend/dxvk/src/dxvk/dxvk_device.cpp @@ -566,6 +566,11 @@ namespace dxvk { return m_objects.memoryManager().getAllocationCacheStats(); } + void DxvkDevice::getTelemetryMemorySnapshot(D3D9BridgeMemorySnapshot& snapshot, + D3D9BridgeMemoryChunk* chunks, uint32_t chunkCapacity, uint32_t* masks, uint32_t maskCapacity) { + m_objects.memoryManager().getTelemetrySnapshot(snapshot,chunks,chunkCapacity,masks,maskCapacity); + } + uint32_t DxvkDevice::getCurrentFrameId() const { return m_statCounters.getCtr(DxvkStatCounter::QueuePresentCount); @@ -643,6 +648,7 @@ namespace dxvk { VkResult result = status->result.load(); if (result == VK_NOT_READY) { + DxvkTelemetryScope scope(&m_telemetry,D3D9_BRIDGE_TELEMETRY_SUBMISSION_WAIT,reinterpret_cast(status)); m_submissionQueue.synchronizeSubmission(status); result = status->result.load(); } @@ -655,6 +661,8 @@ namespace dxvk { if (fence.value() >= value) return; + DxvkTelemetryScope scope(&m_telemetry,D3D9_BRIDGE_TELEMETRY_GPU_WAIT,reinterpret_cast(&fence)); + auto t0 = dxvk::high_resolution_clock::now(); fence.wait(value); @@ -669,6 +677,7 @@ namespace dxvk { void DxvkDevice::waitForResource(const DxvkPagedResource& resource, DxvkAccess access) { if (resource.isInUse(access)) { + DxvkTelemetryScope scope(&m_telemetry,D3D9_BRIDGE_TELEMETRY_GPU_WAIT,reinterpret_cast(&resource)); auto t0 = dxvk::high_resolution_clock::now(); m_submissionQueue.synchronizeUntil([&resource, access] { @@ -686,6 +695,7 @@ namespace dxvk { void DxvkDevice::waitForIdle() { + DxvkTelemetryScope scope(&m_telemetry,D3D9_BRIDGE_TELEMETRY_GPU_WAIT,0,0,D3D9_BRIDGE_TELEMETRY_DEVICE_IDLE); m_submissionQueue.waitForIdle(); m_submissionQueue.lockDeviceQueue(); diff --git a/backend/dxvk/src/dxvk/dxvk_device.h b/backend/dxvk/src/dxvk/dxvk_device.h index ca4be2d..dc60dd5 100644 --- a/backend/dxvk/src/dxvk/dxvk_device.h +++ b/backend/dxvk/src/dxvk/dxvk_device.h @@ -23,6 +23,7 @@ #include "dxvk_shader.h" #include "dxvk_sparse.h" #include "dxvk_stats.h" +#include "dxvk_telemetry.h" #include "dxvk_unbound.h" namespace dxvk { @@ -83,6 +84,10 @@ namespace dxvk { friend class DxvkSubmissionQueue; friend class DxvkDescriptorPoolTracker; public: + + DxvkTelemetry& telemetry() { return m_telemetry; } + void getTelemetryMemorySnapshot(D3D9BridgeMemorySnapshot& snapshot, + D3D9BridgeMemoryChunk* chunks, uint32_t chunkCapacity, uint32_t* masks, uint32_t maskCapacity); DxvkDevice( const Rc& instance, @@ -753,6 +758,7 @@ namespace dxvk { DxvkShaderOptions m_shaderOptions; DxvkDevicePerfHints m_perfHints; + DxvkTelemetry m_telemetry; DxvkObjects m_objects; sync::Spinlock m_statLock; diff --git a/backend/dxvk/src/dxvk/dxvk_memory.cpp b/backend/dxvk/src/dxvk/dxvk_memory.cpp index 65c3669..411ac55 100644 --- a/backend/dxvk/src/dxvk/dxvk_memory.cpp +++ b/backend/dxvk/src/dxvk/dxvk_memory.cpp @@ -304,6 +304,8 @@ namespace dxvk { if (unlikely(m_flags.test(DxvkAllocationFlag::OwnsMemory))) { auto vk = m_allocator->device()->vkd(); + DxvkTelemetryScope scope(&m_allocator->device()->telemetry(),D3D9_BRIDGE_TELEMETRY_FREE, + vk::getObjectHandle(m_memory),m_size,D3D9_BRIDGE_TELEMETRY_DEVICE_MEMORY); vk->vkFreeMemory(vk->device(), m_memory, nullptr); if (unlikely(m_sparsePageTable)) @@ -443,6 +445,9 @@ namespace dxvk { if (!allocation) return nullptr; + DxvkTelemetryScope scope(&m_allocator->device()->telemetry(),D3D9_BRIDGE_TELEMETRY_ALLOC, + reinterpret_cast(allocation),size, + D3D9_BRIDGE_TELEMETRY_RESOURCE|D3D9_BRIDGE_TELEMETRY_CACHE_HIT); m_pools[poolIndex] = allocation->m_nextCached; allocation->m_nextCached = nullptr; return allocation; @@ -811,6 +816,13 @@ namespace dxvk { Rc DxvkMemoryAllocator::allocateMemory( const VkMemoryRequirements& requirements, const DxvkAllocationInfo& allocationInfo) { + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_ALLOC,0, + requirements.size,D3D9_BRIDGE_TELEMETRY_RESOURCE); + auto done = [&scope] (DxvkResourceAllocation* allocation) -> Rc { + scope.identity(reinterpret_cast(allocation)); + if (!allocation) scope.fail(-1); + return allocation; + }; std::lock_guard lock(m_mutex); // If we're allocating device-local memory, only consider memory types from @@ -839,7 +851,7 @@ namespace dxvk { int64_t address = selectedPool.alloc(size, requirements.alignment); if (likely(address >= 0)) - return createAllocation(type, selectedPool, address, size, allocationInfo); + return done(createAllocation(type, selectedPool, address, size, allocationInfo)); // If we're not allowed to allocate additional device memory, move on. // Also do not try to revive any chunks marked for defragmentation since @@ -853,7 +865,7 @@ namespace dxvk { address = selectedPool.alloc(size, requirements.alignment); if (address >= 0) - return createAllocation(type, selectedPool, address, size, allocationInfo); + return done(createAllocation(type, selectedPool, address, size, allocationInfo)); } // If the allocation is very large, use a dedicated allocation instead @@ -890,7 +902,7 @@ namespace dxvk { continue; mapDeviceMemory(memory, allocationInfo.properties); - return createAllocation(type, memory, allocationInfo); + return done(createAllocation(type, memory, allocationInfo)); } // Try to allocate a new chunk that is large enough to hold @@ -902,11 +914,11 @@ namespace dxvk { if (allocateChunkInPool(type, selectedPool, allocationInfo.properties, size, desiredSize)) { address = selectedPool.alloc(size, requirements.alignment); - return createAllocation(type, selectedPool, address, size, allocationInfo); + return done(createAllocation(type, selectedPool, address, size, allocationInfo)); } } - return nullptr; + return done(nullptr); } @@ -914,6 +926,8 @@ namespace dxvk { const VkMemoryRequirements& requirements, const DxvkAllocationInfo& allocationInfo, const void* next) { + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_ALLOC,0, + requirements.size,D3D9_BRIDGE_TELEMETRY_RESOURCE); std::lock_guard lock(m_mutex); DxvkDeviceMemory memory = { }; @@ -924,10 +938,13 @@ namespace dxvk { if (likely(memory.memory != VK_NULL_HANDLE)) { mapDeviceMemory(memory, allocationInfo.properties); - return createAllocation(type, memory, allocationInfo); + auto* allocation=createAllocation(type, memory, allocationInfo); + scope.identity(reinterpret_cast(allocation)); + return allocation; } } + scope.fail(-1); return nullptr; } @@ -1390,10 +1407,18 @@ namespace dxvk { DxvkDeviceMemory result = { }; result.size = size; - if (vk->vkAllocateMemory(vk->device(), &memoryInfo, nullptr, &result.memory)) { + auto allocate = [&] { + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_ALLOC,0,size, + D3D9_BRIDGE_TELEMETRY_DEVICE_MEMORY); + VkResult status=vk->vkAllocateMemory(vk->device(), &memoryInfo, nullptr, &result.memory); + scope.identity(vk::getObjectHandle(result.memory)); + if (status!=VK_SUCCESS) scope.fail(status); + return status; + }; + if (allocate()) { freeEmptyChunksInHeap(*type.heap, VkDeviceSize(-1), high_resolution_clock::time_point()); - if (vk->vkAllocateMemory(vk->device(), &memoryInfo, nullptr, &result.memory)) + if (allocate()) return DxvkDeviceMemory(); } @@ -1622,6 +1647,8 @@ namespace dxvk { DxvkDeviceMemory memory) { auto vk = m_device->vkd(); vk->vkDestroyBuffer(vk->device(), memory.buffer, nullptr); + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_FREE, + vk::getObjectHandle(memory.memory),memory.size,D3D9_BRIDGE_TELEMETRY_DEVICE_MEMORY); vk->vkFreeMemory(vk->device(), memory.memory, nullptr); type.stats.memoryAllocated -= memory.size; @@ -1630,6 +1657,10 @@ namespace dxvk { void DxvkMemoryAllocator::freeAllocation( DxvkResourceAllocation* allocation) { + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_FREE, + reinterpret_cast(allocation),allocation->m_size,D3D9_BRIDGE_TELEMETRY_RESOURCE); + if (allocation->m_flags.test(DxvkAllocationFlag::CanCache)) + scope.flags(D3D9_BRIDGE_TELEMETRY_CACHE_RETURN); if (allocation->m_flags.test(DxvkAllocationFlag::ClearOnFree)) { if (allocation->m_mapPtr) bit::bclear(allocation->m_mapPtr, allocation->m_size); @@ -1700,6 +1731,9 @@ namespace dxvk { void DxvkMemoryAllocator::freeCachedAllocationsLocked( DxvkResourceAllocation* allocation) { while (allocation) { + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_FREE, + reinterpret_cast(allocation),allocation->m_size, + D3D9_BRIDGE_TELEMETRY_RESOURCE|D3D9_BRIDGE_TELEMETRY_CACHE_EVICT); auto& pool = allocation->m_mapPtr ? allocation->m_type->mappedPool : allocation->m_type->devicePool; @@ -1828,8 +1862,13 @@ namespace dxvk { auto vk = m_device->vkd(); - VkResult vr = vk->vkMapMemory(vk->device(), - memory.memory, 0, memory.size, 0, &memory.mapPtr); + VkResult vr; + { + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_MAP, + vk::getObjectHandle(memory.memory),memory.size,D3D9_BRIDGE_TELEMETRY_DEVICE_MEMORY); + vr = vk->vkMapMemory(vk->device(), memory.memory, 0, memory.size, 0, &memory.mapPtr); + if (vr!=VK_SUCCESS) scope.fail(vr); + } if (vr != VK_SUCCESS) { throw DxvkError(str::format("Failed to map Vulkan memory: ", vr, @@ -1847,6 +1886,8 @@ namespace dxvk { return; auto vk = m_device->vkd(); + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_UNMAP, + vk::getObjectHandle(memory.memory),memory.size,D3D9_BRIDGE_TELEMETRY_DEVICE_MEMORY); vk->vkUnmapMemory(vk->device(), memory.memory); Logger::debug(str::format("Unmapped memory region 0x", std::hex, @@ -1903,15 +1944,20 @@ namespace dxvk { // Try to suballocate from existing chunks, but do not create // any new chunks. Let the regular code path handle that case // as necessary. + DxvkTelemetryScope scope(&m_device->telemetry(),D3D9_BRIDGE_TELEMETRY_ALLOC,0,allocationSize, + D3D9_BRIDGE_TELEMETRY_RESOURCE|D3D9_BRIDGE_TELEMETRY_CACHE_FILL); int64_t address = memoryPool.alloc(allocationSize, requirements.alignment); - if (address < 0) + if (address < 0) { + scope.fail(-1); break; + } // Add allocation to the list and mark it as cacheable, // so it will get recycled as-is after use. allocation = createAllocation(memoryType, memoryPool, address, allocationSize, DxvkAllocationInfo()); + scope.identity(reinterpret_cast(allocation)); allocation->m_flags.set(DxvkAllocationFlag::CanCache); if (tail) { @@ -2245,6 +2291,42 @@ namespace dxvk { } } + void DxvkMemoryAllocator::getTelemetrySnapshot(D3D9BridgeMemorySnapshot& s, + D3D9BridgeMemoryChunk* chunks, uint32_t chunkCapacity, uint32_t* masks, uint32_t maskCapacity) { + std::lock_guard lock(m_mutex); + s.TimestampNs=DxvkTelemetry::nowNs(); + for (uint32_t i=0; ichunks.size(); j++) { + const auto& chunk=pool->chunks[j]; + if (!chunk.memory.memory) continue; + const uint32_t pages=pool->pageAllocator.pageCount(j); + const uint32_t maskCount=(pages+31u)/32u; + const uint64_t used=pool->pageAllocator.pagesUsed(j)*DxvkPageAllocator::PageSize; + ++s.TotalChunks; s.TotalMasks+=maskCount; + s.TotalCapacity+=chunk.memory.size; s.TotalUsed+=used; + if (s.CopiedChunks>=chunkCapacity) { + s.Flags|=D3D9_BRIDGE_MEMORY_CHUNKS_TRUNCATED; + if (maskCount) s.Flags|=D3D9_BRIDGE_MEMORY_MASKS_TRUNCATED; + continue; + } + auto& out=chunks[s.CopiedChunks++]; out={}; + out.Cookie=chunk.memory.cookie; out.Capacity=chunk.memory.size; out.Used=used; + out.Heap=type.properties.heapIndex; out.Type=i; + out.Mapped=pool==&type.mappedPool; out.Active=pool->pageAllocator.chunkIsAvailable(j); + out.PageCount=pages; out.MaskOffset=s.CopiedMasks; + if (maskCount <= maskCapacity-s.CopiedMasks) { + out.MaskCount=maskCount; + if (maskCount) pool->pageAllocator.getPageAllocationMask(j,masks+s.CopiedMasks); + s.CopiedMasks+=maskCount; + } else if (maskCount) s.Flags|=D3D9_BRIDGE_MEMORY_MASKS_TRUNCATED; + } + } + } + } + DxvkSharedAllocationCacheStats DxvkMemoryAllocator::getAllocationCacheStats() const { DxvkSharedAllocationCacheStats result = { }; diff --git a/backend/dxvk/src/dxvk/dxvk_memory.h b/backend/dxvk/src/dxvk/dxvk_memory.h index a8e4486..55c5d4f 100644 --- a/backend/dxvk/src/dxvk/dxvk_memory.h +++ b/backend/dxvk/src/dxvk/dxvk_memory.h @@ -1,5 +1,8 @@ #pragma once +struct D3D9BridgeMemorySnapshot; +struct D3D9BridgeMemoryChunk; + #include #include @@ -1277,6 +1280,8 @@ namespace dxvk { * \param [out] stats Memory statistics */ void getAllocationStats(DxvkMemoryAllocationStats& stats); + void getTelemetrySnapshot(D3D9BridgeMemorySnapshot& snapshot, + D3D9BridgeMemoryChunk* chunks, uint32_t chunkCapacity, uint32_t* masks, uint32_t maskCapacity); /** * \brief Queries shared cache stats diff --git a/backend/dxvk/src/dxvk/dxvk_telemetry.cpp b/backend/dxvk/src/dxvk/dxvk_telemetry.cpp new file mode 100644 index 0000000..eee88fb --- /dev/null +++ b/backend/dxvk/src/dxvk/dxvk_telemetry.cpp @@ -0,0 +1,84 @@ +#include "dxvk_telemetry.h" +#include +#include + +namespace dxvk { + uint64_t DxvkTelemetry::nowNs() { + static const uint64_t frequency = [] { LARGE_INTEGER v{}; QueryPerformanceFrequency(&v); return uint64_t(v.QuadPart); }(); + LARGE_INTEGER ticks{}; QueryPerformanceCounter(&ticks); + if (!frequency || ticks.QuadPart < 0) return 0; + uint64_t t = uint64_t(ticks.QuadPart); + return (t / frequency) * 1000000000ull + (t % frequency) * 1000000000ull / frequency; + } + HRESULT DxvkTelemetry::start(const D3D9BridgeTelemetryConfig* c) { + if (!c) return E_POINTER; + if (c->StructSize != sizeof(*c) || c->Version != D3D9_BRIDGE_TELEMETRY_VERSION || + !c->CaptureId || !c->MaxEvents || c->MaxEvents > D3D9_BRIDGE_TELEMETRY_MAX_EVENTS || c->Reserved) + return D3DERR_INVALIDCALL; + std::lock_guard lock(m_mutex); + if (m_active.load(std::memory_order_relaxed) || c->CaptureId <= m_captureId) return D3DERR_INVALIDCALL; + try { m_events.resize(c->MaxEvents); } catch (const std::bad_alloc&) { return E_OUTOFMEMORY; } + if (++m_generation == 0) ++m_generation; + m_captureId=c->CaptureId; m_minDuration=c->MinDurationNs; m_minBytes=c->MinBytes; + m_started=m_inFlight=m_inFlightAtStop=m_dropped=m_filtered=m_lateDropped=0; + m_head=m_count=0; + m_active.store(m_generation,std::memory_order_release); + return S_OK; + } + HRESULT DxvkTelemetry::stop(uint64_t id) { + std::lock_guard lock(m_mutex); + if (!id || id != m_captureId) return D3DERR_INVALIDCALL; + if (!m_active.load(std::memory_order_relaxed)) return S_FALSE; + m_inFlightAtStop=m_inFlight; + m_active.store(0,std::memory_order_release); + return S_OK; + } + HRESULT DxvkTelemetry::read(uint64_t id, D3D9BridgeTelemetryRead* out, + D3D9BridgeTelemetryEvent* events, uint32_t capacity) { + if (!out) return E_POINTER; + if (out->StructSize != sizeof(*out) || out->Version != D3D9_BRIDGE_TELEMETRY_VERSION || + capacity>D3D9_BRIDGE_TELEMETRY_MAX_EVENTS || (capacity && !events)) return D3DERR_INVALIDCALL; + std::lock_guard lock(m_mutex); + if (!id || id != m_captureId) return D3DERR_INVALIDCALL; + D3D9BridgeTelemetryRead result{}; + result.StructSize=sizeof(result); result.Version=D3D9_BRIDGE_TELEMETRY_VERSION; + result.CaptureId=id; result.Dropped=m_dropped; result.Filtered=m_filtered; + result.Started=m_started; result.InFlight=m_inFlight; result.InFlightAtStop=m_inFlightAtStop; + result.LateDropped=m_lateDropped; result.Capacity=uint32_t(m_events.size()); + result.Active=m_active.load(std::memory_order_relaxed) != 0; + result.Written=std::min(capacity,m_count); + for (uint32_t i=0; i lock(m_mutex); + return id && id==m_captureId ? m_active.load(std::memory_order_relaxed) : 0; + } + bool DxvkTelemetry::isGenerationActive(uint64_t id, uint64_t generation) { + std::lock_guard lock(m_mutex); + return id && id==m_captureId && generation && m_active.load(std::memory_order_relaxed)==generation; + } + DxvkTelemetry::Token DxvkTelemetry::begin() { + uint64_t gen=m_active.load(std::memory_order_acquire); + if (!gen) return {}; + std::lock_guard lock(m_mutex); + if (gen!=m_active.load(std::memory_order_relaxed)) return {}; + ++m_started; ++m_inFlight; + return {gen,nowNs(),GetCurrentThreadId()}; + } + void DxvkTelemetry::finish(Token token, uint32_t kind, uint64_t identity, + uint64_t bytes, int32_t result, uint32_t flags) { + uint64_t end=nowNs(); + std::lock_guard lock(m_mutex); + if (token.generation != m_generation) return; + --m_inFlight; + if (!m_active.load(std::memory_order_relaxed)) { ++m_lateDropped; return; } + if (!(flags&D3D9_BRIDGE_TELEMETRY_FAILED) && end-token.beginNs +#include +#include +#include + +namespace dxvk { + // Device-owned. No recorder lock is held across the operation being timed. + class DxvkTelemetry { + public: + struct Token { uint64_t generation = 0, beginNs = 0; uint32_t threadId = 0; }; + bool enabled() const { return m_active.load(std::memory_order_acquire) != 0; } + static uint64_t nowNs(); + HRESULT start(const D3D9BridgeTelemetryConfig* config); + HRESULT stop(uint64_t captureId); + HRESULT read(uint64_t captureId, D3D9BridgeTelemetryRead* output, + D3D9BridgeTelemetryEvent* events, uint32_t capacity); + uint64_t activeGeneration(uint64_t captureId); + bool isGenerationActive(uint64_t captureId, uint64_t generation); + Token begin(); + void finish(Token token, uint32_t kind, uint64_t identity, uint64_t bytes, + int32_t result, uint32_t flags); + private: + std::atomic m_active = { 0 }; + std::mutex m_mutex; + std::vector m_events; + uint64_t m_generation = 0, m_captureId = 0; + uint64_t m_minDuration = 0, m_minBytes = 0; + uint64_t m_started = 0, m_inFlight = 0, m_inFlightAtStop = 0; + uint64_t m_dropped = 0, m_filtered = 0, m_lateDropped = 0; + uint32_t m_head = 0, m_count = 0; + }; + + class DxvkTelemetryScope { + public: + DxvkTelemetryScope(DxvkTelemetry* recorder, uint32_t kind, uint64_t identity = 0, + uint64_t bytes = 0, uint32_t flags = 0) + : m_recorder(recorder), m_kind(kind), m_flags(flags), m_identity(identity), m_bytes(bytes) { + if (m_recorder) m_token = m_recorder->begin(); + if (m_token.generation) m_uncaught = std::uncaught_exceptions(); + } + ~DxvkTelemetryScope() { + if (m_token.generation) { + if (std::uncaught_exceptions() > m_uncaught && !(m_flags & D3D9_BRIDGE_TELEMETRY_FAILED)) + fail(-1); + m_recorder->finish(m_token, m_kind, m_identity, m_bytes, m_result, m_flags); + } + } + void identity(uint64_t value) { m_identity = value; } + void bytes(uint64_t value) { m_bytes = value; } + void fail(int32_t result) { m_result = result; m_flags |= D3D9_BRIDGE_TELEMETRY_FAILED; } + void outcome(int32_t result) { m_result = result; if (result < 0) m_flags |= D3D9_BRIDGE_TELEMETRY_FAILED; } + void flags(uint32_t value) { m_flags |= value; } + private: + DxvkTelemetry* m_recorder; + DxvkTelemetry::Token m_token; + uint32_t m_kind, m_flags; + uint64_t m_identity, m_bytes; + int32_t m_result = 0; + int m_uncaught = 0; + }; +} diff --git a/backend/dxvk/src/dxvk/meson.build b/backend/dxvk/src/dxvk/meson.build index 40f4cc0..792f5a7 100644 --- a/backend/dxvk/src/dxvk/meson.build +++ b/backend/dxvk/src/dxvk/meson.build @@ -44,6 +44,7 @@ dxvk_src = [ 'dxvk_device.cpp', 'dxvk_device_filter.cpp', 'dxvk_device_info.cpp', + 'dxvk_telemetry.cpp', 'dxvk_fence.cpp', 'dxvk_format.cpp', 'dxvk_framebuffer.cpp', diff --git a/backend/dxvk/src/util/util_lru.h b/backend/dxvk/src/util/util_lru.h index 96076cc..4623428 100644 --- a/backend/dxvk/src/util/util_lru.h +++ b/backend/dxvk/src/util/util_lru.h @@ -10,15 +10,21 @@ namespace dxvk { public: typedef typename std::list::const_iterator const_iterator; - void insert(T value) { + void insert(T value, bool mostRecent = true) { auto cacheIter = m_cache.find(value); - if (cacheIter != m_cache.end()) - m_list.erase(cacheIter->second); - - m_list.push_back(value); - auto iter = m_list.cend(); - iter--; - m_cache[value] = iter; + if (cacheIter != m_cache.end()) { + if (mostRecent) + m_list.splice(m_list.end(), m_list, cacheIter->second); + return; + } + + auto iter = m_list.insert(mostRecent ? m_list.cend() : m_list.cbegin(), value); + try { + m_cache.emplace(value, iter); + } catch (...) { + m_list.erase(iter); + throw; + } } void remove(const T& value) { @@ -41,11 +47,7 @@ namespace dxvk { if (cacheIter == m_cache.end()) return; - m_list.erase(cacheIter->second); - m_list.push_back(value); - auto iter = m_list.cend(); - --iter; - m_cache[value] = iter; + m_list.splice(m_list.end(), m_list, cacheIter->second); } const_iterator leastRecentlyUsedIter() const { diff --git a/backend/dxvk/src/util/util_unmap.cpp b/backend/dxvk/src/util/util_unmap.cpp index 40678d4..e957673 100644 --- a/backend/dxvk/src/util/util_unmap.cpp +++ b/backend/dxvk/src/util/util_unmap.cpp @@ -1,4 +1,6 @@ #include "util_unmap.h" +#include "../dxvk/dxvk_telemetry.h" +#include #include "./log/log.h" @@ -9,19 +11,21 @@ namespace dxvk { #ifdef DXVK_USE_UNMAPPABLE_MEMORY - MemoryFile::MemoryFile(size_t size) { + MemoryFile::MemoryFile(size_t size, DxvkTelemetry* telemetry) : m_telemetry(telemetry) { create(size); } MemoryFile::MemoryFile(MemoryFile&& other) - : m_data(std::exchange(other.m_data, Data())) { } + : m_data(std::exchange(other.m_data, Data())) + , m_telemetry(std::exchange(other.m_telemetry, nullptr)) { } MemoryFile& MemoryFile::operator = (MemoryFile&& other) { destroy(); m_data = std::exchange(other.m_data, Data()); + m_telemetry = std::exchange(other.m_telemetry, nullptr); return *this; } @@ -52,12 +56,19 @@ namespace dxvk { void* MemoryFile::map(size_t offset, size_t size) { - return MapViewOfFile(m_data.mapping, FILE_MAP_ALL_ACCESS, 0, offset, size); + DxvkTelemetryScope scope(m_telemetry,D3D9_BRIDGE_TELEMETRY_MAP,0,size, + D3D9_BRIDGE_TELEMETRY_FILE_BACKED|D3D9_BRIDGE_TELEMETRY_OS_MAPPING); + auto* result=MapViewOfFile(m_data.mapping, FILE_MAP_ALL_ACCESS, 0, offset, size); + scope.identity(reinterpret_cast(result)); + if (!result) scope.fail(int32_t(GetLastError())); + return result; } - void MemoryFile::unmap(void* ptr) { - UnmapViewOfFile(ptr); + void MemoryFile::unmap(void* ptr, size_t size, DxvkTelemetry* telemetry) { + DxvkTelemetryScope scope(telemetry,D3D9_BRIDGE_TELEMETRY_UNMAP,reinterpret_cast(ptr),size, + D3D9_BRIDGE_TELEMETRY_FILE_BACKED|D3D9_BRIDGE_TELEMETRY_OS_MAPPING); + if (!UnmapViewOfFile(ptr)) scope.fail(int32_t(GetLastError())); } @@ -87,14 +98,104 @@ namespace dxvk { MemoryFilePool::~MemoryFilePool() { + if (m_retireThread.joinable()) { + if (this_thread::isInModuleDetachment()) { + m_retireThread.detach(); + return; + } + { std::lock_guard lock(m_retireMutex); m_retireStopped = true; } + m_retireChanged.notify_all(); + m_retireThread.join(); + } + } + void MemoryFilePool::drainRetired() { + std::unique_lock lock(m_retireMutex); + m_retireChanged.wait(lock, [this] { return !m_retireCount; }); } + void MemoryFilePool::finishRetired(Retired& entry) { + if (entry.view) + MemoryFile::unmap(entry.view, entry.viewBytes, m_telemetry); + m_memoryMapped -= entry.mappedBytes; + entry.file = MemoryFile(); + m_memoryAllocated -= entry.allocatedBytes; + if (entry.token.generation) + m_telemetry->finish(entry.token,D3D9_BRIDGE_TELEMETRY_MAPPING_RETIRE, + entry.identity,entry.mappedBytes,0,D3D9_BRIDGE_TELEMETRY_FILE_BACKED|D3D9_BRIDGE_TELEMETRY_DEFERRED); + } - int32_t MemoryFilePool::alloc(size_t size) { + void MemoryFilePool::retirementWorker() { + env::setThreadName("dxvk-map-free"); + for (;;) { + Retired entry; + { std::unique_lock lock(m_retireMutex); + m_retireChanged.wait(lock, [this] { return m_retireCount || m_retireStopped; }); + if (!m_retireCount) return; + entry = std::move(m_retired[m_retireHead]); + } + finishRetired(entry); + { std::lock_guard lock(m_retireMutex); + m_retireMappedBytes -= entry.viewBytes; + m_retireAllocatedBytes -= entry.allocatedBytes; + m_retireHead = (m_retireHead + 1) % RetireCapacity; + --m_retireCount; + } + m_retireChanged.notify_all(); + } + } + + bool MemoryFilePool::retire(Retired&& entry) { + if (!entry.view && !entry.file) { + m_memoryMapped -= entry.mappedBytes; + return false; + } + // A small create/release loop would immediately wait for this same worker + // when acquiring its next chunk. Keep those releases synchronous; continue + // an already queued burst until it is fully retired. + if (m_allowRetirement + && (m_memoryMapped.load(std::memory_order_relaxed) >= RetireWorkingSet + || m_retireCount.load(std::memory_order_relaxed)) + && entry.viewBytes <= RetireByteLimit && entry.allocatedBytes <= RetireByteLimit) { + std::unique_lock lock(m_retireMutex); + if (!m_retireThread.joinable()) { + try { + m_retireThread = dxvk::thread([this] { retirementWorker(); }); + } catch (...) { + // Final resource release must still succeed if worker creation fails. + m_allowRetirement = false; + } + } + if (m_allowRetirement) { + m_retireChanged.wait(lock, [this, &entry] { + return m_retireCount < RetireCapacity + && m_retireMappedBytes + entry.viewBytes <= RetireByteLimit + && m_retireAllocatedBytes + entry.allocatedBytes <= RetireByteLimit; + }); + m_retireMappedBytes += entry.viewBytes; + m_retireAllocatedBytes += entry.allocatedBytes; + if (m_telemetry) entry.token = m_telemetry->begin(); + m_retired[(m_retireHead + m_retireCount) % RetireCapacity] = std::move(entry); + ++m_retireCount; + lock.unlock(); + m_retireChanged.notify_all(); + return true; + } + } + finishRetired(entry); + return false; + } + + + int32_t MemoryFilePool::alloc(size_t size, bool* initiallyZero) { + DxvkTelemetryScope scope(m_telemetry,D3D9_BRIDGE_TELEMETRY_ALLOC,0,size,D3D9_BRIDGE_TELEMETRY_FILE_BACKED); std::lock_guard lock(m_mutex); + if (initiallyZero) + *initiallyZero = false; + int32_t index; + const bool newSlot = m_freeList.empty(); if (!m_freeList.empty()) { index = m_freeList.back(); @@ -106,43 +207,78 @@ namespace dxvk { auto& allocInfo = m_allocations.at(index); - if (size <= DxvkPageAllocator::MaxChunkSize) { - auto address = (size <= DxvkPoolAllocator::MaxSize) - ? m_poolAllocator.alloc(size) - : m_pageAllocator.alloc(size, 1u); - - if (address < 0) { - addChunk(size); - - address = (size <= DxvkPoolAllocator::MaxSize) + try { + if (size <= DxvkPageAllocator::MaxChunkSize) { + auto address = (size <= DxvkPoolAllocator::MaxSize) ? m_poolAllocator.alloc(size) : m_pageAllocator.alloc(size, 1u); - } - if (address < 0) { - m_freeList.push_back(index); - return -1; - } + if (address < 0) { + addChunk(size); - allocInfo.address = address; - allocInfo.size = size; - } else { - allocInfo.file = MemoryFile(size); - allocInfo.size = size; + address = (size <= DxvkPoolAllocator::MaxSize) + ? m_poolAllocator.alloc(size) + : m_pageAllocator.alloc(size, 1u); + } + + if (address < 0) { + if (newSlot) + m_allocations.pop_back(); + else + m_freeList.push_back(index); + scope.fail(-1); + return -1; + } - m_memoryAllocated += size; + allocInfo.address = address; + allocInfo.size = size; + auto& chunk = m_chunks.at(address >> DxvkPageAllocator::ChunkAddressBits); + auto offset = size_t(address & DxvkPageAllocator::ChunkAddressMask); + if (initiallyZero) + *initiallyZero = offset >= chunk.allocatedEnd; + chunk.allocatedEnd = std::max(chunk.allocatedEnd, offset + size); + } else { + drainRetired(); + allocInfo.file = MemoryFile(size,m_telemetry); + allocInfo.size = size; + + m_memoryAllocated += size; + if (initiallyZero) + *initiallyZero = true; + } + } catch (...) { + // A failed backing-file creation must not strand an allocation slot. + if (newSlot) + m_allocations.pop_back(); + else + m_freeList.push_back(index); + scope.fail(-1); + throw; } + if (++m_nextCookie==0) ++m_nextCookie; + allocInfo.cookie=(uint64_t(reinterpret_cast(this))<<32)|m_nextCookie; + scope.identity(allocInfo.cookie); m_memoryUsed += size; return index; } void MemoryFilePool::free(int32_t allocation) { + DxvkTelemetryScope scope(m_telemetry,D3D9_BRIDGE_TELEMETRY_FREE,0,0,D3D9_BRIDGE_TELEMETRY_FILE_BACKED); std::lock_guard lock(m_mutex); auto& allocInfo = m_allocations.at(allocation); + scope.identity(allocInfo.cookie); scope.bytes(allocInfo.size); m_memoryUsed -= allocInfo.size; + Retired retired; + std::optional unmapScope; + if (allocInfo.mapCount) { + unmapScope.emplace(m_telemetry,D3D9_BRIDGE_TELEMETRY_UNMAP, + allocInfo.cookie,allocInfo.size,D3D9_BRIDGE_TELEMETRY_FILE_BACKED); + retired = detachMapping(allocInfo); + } + retired.identity = allocInfo.cookie; if (allocInfo.size <= DxvkPageAllocator::MaxChunkSize) { auto chunkIndex = allocInfo.address >> DxvkPageAllocator::ChunkAddressBits; @@ -152,29 +288,49 @@ namespace dxvk { else m_pageAllocator.free(allocInfo.address, allocInfo.size); - if (!m_pageAllocator.pagesUsed(chunkIndex)) - freeChunk(chunkIndex); + if (!m_pageAllocator.pagesUsed(chunkIndex)) { + auto& chunk = m_chunks.at(chunkIndex); + retired.file = std::move(chunk.file); + retired.allocatedBytes = chunk.size; + m_pageAllocator.removeChunk(chunkIndex); + chunk = ChunkInfo(); + } } else { - m_memoryAllocated -= allocInfo.size; + retired.file = std::move(allocInfo.file); + retired.allocatedBytes = allocInfo.size; } allocInfo = AllocInfo(); m_freeList.push_back(allocation); + const bool deferred = retire(std::move(retired)); + if (deferred && unmapScope) + unmapScope->flags(D3D9_BRIDGE_TELEMETRY_DEFERRED); } void* MemoryFilePool::map(int32_t allocation) { + DxvkTelemetryScope scope(m_telemetry,D3D9_BRIDGE_TELEMETRY_MAP,0,0,D3D9_BRIDGE_TELEMETRY_FILE_BACKED); std::lock_guard lock(m_mutex); auto& allocInfo = m_allocations.at(allocation); - - if (!(allocInfo.mapCount++)) { - m_memoryMapped += allocInfo.size; - + scope.identity(allocInfo.cookie); scope.bytes(allocInfo.size); + + if (!allocInfo.mapCount) { + auto mapFile = [this] (MemoryFile& file, size_t offset, size_t size) { + const bool pending = m_retireCount.load(std::memory_order_relaxed) != 0; + auto ptr = file.map(offset, size); + if (!ptr && pending) { + // A final-release view may still occupy scarce 32-bit address space. + // Complete it and retry once before reporting an allocation failure. + drainRetired(); + ptr = file.map(offset, size); + } + return ptr; + }; if (allocInfo.file) { // Map entire file if the allocation owns it - allocInfo.mapPtr = allocInfo.file.map(0u, allocInfo.size); + allocInfo.mapPtr = mapFile(allocInfo.file, 0u, allocInfo.size); } else { // Check whether the allocation is contained inside one page auto& chunk = m_chunks.at(allocInfo.address >> DxvkPageAllocator::ChunkAddressBits); @@ -192,33 +348,57 @@ namespace dxvk { auto& page = chunk.pages.at(pageLo); - if (!(page.mapCount++)) - page.mapPtr = chunk.file.map(chunkOffset - pageOffset, pageSize); + if (!page.mapCount) + page.mapPtr = mapFile(chunk.file, chunkOffset - pageOffset, pageSize); - allocInfo.mapPtr = reinterpret_cast(page.mapPtr) + pageOffset; + if (page.mapPtr) { + ++page.mapCount; + allocInfo.mapPtr = reinterpret_cast(page.mapPtr) + pageOffset; + } } else { // We're straddling multiple pages, map them all and offset the base address - auto mapPtr = chunk.file.map(pageLo * pageSize, (pageHi - pageLo + 1u) * pageSize); - allocInfo.mapPtr = reinterpret_cast(mapPtr) + pageOffset; + auto mapPtr = mapFile(chunk.file, pageLo * pageSize, (pageHi - pageLo + 1u) * pageSize); + if (mapPtr) + allocInfo.mapPtr = reinterpret_cast(mapPtr) + pageOffset; } } + + if (!allocInfo.mapPtr) { + scope.fail(-1); + return nullptr; + } + + // Failed views own no reference and consume no residency budget. In + // particular, never turn a failed shared-page map into a non-null offset. + m_memoryMapped += allocInfo.size; } + ++allocInfo.mapCount; return allocInfo.mapPtr; } void MemoryFilePool::unmap(int32_t allocation) { + DxvkTelemetryScope scope(m_telemetry,D3D9_BRIDGE_TELEMETRY_UNMAP,0,0,D3D9_BRIDGE_TELEMETRY_FILE_BACKED); std::lock_guard lock(m_mutex); auto& allocInfo = m_allocations.at(allocation); + scope.identity(allocInfo.cookie); scope.bytes(allocInfo.size); + + auto retired = detachMapping(allocInfo); + finishRetired(retired); + } + + MemoryFilePool::Retired MemoryFilePool::detachMapping(AllocInfo& allocInfo) { + Retired retired; if (!(--allocInfo.mapCount)) { - m_memoryMapped -= allocInfo.size; + retired.mappedBytes = allocInfo.size; if (allocInfo.file) { // Unmap entire file - allocInfo.file.unmap(allocInfo.mapPtr); + retired.view = allocInfo.mapPtr; + retired.viewBytes = allocInfo.size; } else { // Check whether the allocation is contained inside one page auto& chunk = m_chunks.at(allocInfo.address >> DxvkPageAllocator::ChunkAddressBits); @@ -234,46 +414,54 @@ namespace dxvk { auto& page = chunk.pages.at(pageLo); if (!(--page.mapCount)) { - chunk.file.unmap(page.mapPtr); + retired.view = page.mapPtr; + retired.viewBytes = pageSize; page.mapPtr = nullptr; } } else { // Apply page offset to mapped pointer and unmap all pages auto basePtr = reinterpret_cast(allocInfo.mapPtr) - pageOffset; - chunk.file.unmap(basePtr); + retired.view = basePtr; + retired.viewBytes = (pageHi-pageLo+1u)*pageSize; } } allocInfo.mapPtr = nullptr; } + return retired; } void MemoryFilePool::addChunk(size_t minSize) { + // A freed chunk can still be owned by the retirement worker. Complete it + // before committing replacement backing storage, including under VA pressure. + drainRetired(); auto chunkSize = DefaultChunkSize; while (chunkSize < minSize) chunkSize <<= 1u; + // Establish backing storage before publishing the free range. Otherwise a + // failed CreateFileMapping leaves the next allocation pointing at no file. + MemoryFile file(chunkSize, m_telemetry); auto chunkIndex = m_pageAllocator.addChunk(chunkSize); - if (chunkIndex >= m_chunks.size()) - m_chunks.resize(chunkIndex + 1u); + try { + if (chunkIndex >= m_chunks.size()) + m_chunks.resize(chunkIndex + 1u); + } catch (...) { + m_pageAllocator.removeChunk(chunkIndex); + throw; + } auto& chunk = m_chunks.at(chunkIndex); - chunk.file = MemoryFile(chunkSize); + chunk.file = std::move(file); chunk.size = chunkSize; m_memoryAllocated += chunk.size; } - void MemoryFilePool::freeChunk(uint32_t index) { - auto& chunk = m_chunks.at(index); - m_memoryAllocated -= chunk.size; - m_pageAllocator.removeChunk(index); - chunk = ChunkInfo(); - } #endif } diff --git a/backend/dxvk/src/util/util_unmap.h b/backend/dxvk/src/util/util_unmap.h index fc20fcd..37f575f 100644 --- a/backend/dxvk/src/util/util_unmap.h +++ b/backend/dxvk/src/util/util_unmap.h @@ -1,11 +1,13 @@ #pragma once #include +#include #include #include #include #include "../dxvk/dxvk_allocator.h" +#include "../dxvk/dxvk_telemetry.h" #include "thread.h" @@ -15,6 +17,8 @@ namespace dxvk { + class DxvkTelemetry; + /** * \brief Unmappable memory statistics */ @@ -38,7 +42,7 @@ namespace dxvk { MemoryFile() = default; - MemoryFile(size_t size); + MemoryFile(size_t size, DxvkTelemetry* telemetry); MemoryFile(MemoryFile&& other); MemoryFile& operator = (MemoryFile&& other); @@ -67,7 +71,7 @@ namespace dxvk { * \brief Unmaps file * \param [in] ptr Pointer to mapped memory region */ - void unmap(void* ptr); + static void unmap(void* ptr, size_t size, DxvkTelemetry* telemetry); /** * \brief Checks whether the file is valid @@ -90,6 +94,7 @@ namespace dxvk { }; Data m_data; + DxvkTelemetry* m_telemetry = nullptr; void destroy(); @@ -113,6 +118,9 @@ namespace dxvk { MemoryFilePool(const MemoryFilePool&) = delete; MemoryFilePool& operator = (const MemoryFilePool&) = delete; + void setTelemetry(DxvkTelemetry* telemetry) { m_telemetry = telemetry; } + void enableRetirement() { m_allowRetirement = true; } + void drainRetired(); /** * \brief Allocates unmappable memory @@ -120,7 +128,7 @@ namespace dxvk { * \param [in] size Size, in bytes * \returns Allocation index, or -1 on error */ - int32_t alloc(size_t size); + int32_t alloc(size_t size, bool* initiallyZero = nullptr); /** * \brief Frees unmappable memory @@ -171,6 +179,9 @@ namespace dxvk { struct ChunkInfo { MemoryFile file = {}; size_t size = 0u; + // Exclusive end of every range ever handed out in this backing file. + // Holes below this mark are conservatively treated as recycled memory. + size_t allocatedEnd = 0u; std::vector pages; }; @@ -181,9 +192,12 @@ namespace dxvk { size_t size = 0u; void* mapPtr = nullptr; size_t mapCount = 0u; + uint64_t cookie = 0u; }; dxvk::mutex m_mutex; + DxvkTelemetry* m_telemetry = nullptr; + uint32_t m_nextCookie = 0; DxvkPageAllocator m_pageAllocator; DxvkPoolAllocator m_poolAllocator; @@ -197,9 +211,35 @@ namespace dxvk { std::vector m_allocations; std::vector m_freeList; - void addChunk(size_t minSize); + // Only unreachable, finally released views may enter this queue. Live + // texture budget eviction remains synchronous. No allocation on enqueue. + struct Retired { + MemoryFile file; + void* view = nullptr; + size_t viewBytes = 0; + uint64_t mappedBytes = 0; + uint64_t allocatedBytes = 0; + uint64_t identity = 0; + DxvkTelemetry::Token token; + }; + static constexpr size_t RetireCapacity = 128; + static constexpr uint64_t RetireByteLimit = 128ull << 20; + static constexpr uint64_t RetireWorkingSet = 8ull << 20; + std::array m_retired; + dxvk::mutex m_retireMutex; + dxvk::condition_variable m_retireChanged; + dxvk::thread m_retireThread; + size_t m_retireHead = 0; + std::atomic m_retireCount = { 0 }; + uint64_t m_retireMappedBytes = 0, m_retireAllocatedBytes = 0; + bool m_allowRetirement = false, m_retireStopped = false; + + Retired detachMapping(AllocInfo& info); + bool retire(Retired&& entry); + void finishRetired(Retired& entry); + void retirementWorker(); - void freeChunk(uint32_t index); + void addChunk(size_t minSize); }; @@ -215,8 +255,7 @@ namespace dxvk { MemoryFileRegion(MemoryFilePool& pool, size_t size) : m_pool (&pool) - , m_allocation(pool.alloc(size)) - , m_mapPtr (nullptr) { } + , m_allocation(pool.alloc(size, &m_initiallyZero)) { } ~MemoryFileRegion() { free(); @@ -224,6 +263,7 @@ namespace dxvk { MemoryFileRegion(MemoryFileRegion&& other) : m_pool (std::exchange(other.m_pool, nullptr)) + , m_initiallyZero(std::exchange(other.m_initiallyZero, false)) , m_allocation (std::exchange(other.m_allocation, -1)) , m_mapPtr (std::exchange(other.m_mapPtr, nullptr)) { @@ -233,6 +273,7 @@ namespace dxvk { free(); m_pool = std::exchange(other.m_pool, nullptr); + m_initiallyZero = std::exchange(other.m_initiallyZero, false); m_allocation = std::exchange(other.m_allocation, -1); m_mapPtr = std::exchange(other.m_mapPtr, nullptr); return *this; @@ -267,6 +308,9 @@ namespace dxvk { return m_mapPtr != nullptr; } + // Allocation provenance, not a claim about contents after the first write. + bool wasInitiallyZero() const { return m_initiallyZero; } + /** * \brief Checks whether allocation is valid * \returns \c true if allocation is valid @@ -278,14 +322,12 @@ namespace dxvk { private: MemoryFilePool* m_pool = nullptr; + bool m_initiallyZero = false; int32_t m_allocation = -1; void* m_mapPtr = nullptr; void free() { if (m_allocation >= 0) { - if (m_mapPtr) - m_pool->unmap(m_allocation); - m_pool->free(m_allocation); } } @@ -299,6 +341,8 @@ namespace dxvk { public: + void setTelemetry(DxvkTelemetry*) { } + DxvkMemoryFileStats getStats() const { return DxvkMemoryFileStats(); } @@ -346,6 +390,8 @@ namespace dxvk { return true; } + bool wasInitiallyZero() const { return false; } + explicit operator bool () const { return m_data != nullptr; } diff --git a/docs/superpowers/plans/2026-11-12-f8-measurement-coverage.md b/docs/superpowers/plans/2026-11-12-f8-measurement-coverage.md new file mode 100644 index 0000000..c41e18b --- /dev/null +++ b/docs/superpowers/plans/2026-11-12-f8-measurement-coverage.md @@ -0,0 +1,49 @@ +# F8 measurement coverage implementation plan + +> For agentic workers: use superpowers:subagent-driven-development for independent backend/runtime work and fresh review. Do not commit, push, or deploy. + +**Goal:** Fill the measured F8 coverage gaps without changing rendering, public interfaces or ordinary-frame behavior. + +**Architecture:** Device-owned bounded telemetry; optional copied-data COM bridge; lower-rate CS sampling; timestamped context and allocator snapshots; offline frame correlation. + +**Tech Stack:** Existing x86 C++/Windows/D3D9/Vulkan, MSVC Bridge and LLVM-MinGW DXVK, PowerShell/Python fixtures. + +**Spec:** `docs/superpowers/specs/2026-11-12-f8-measurement-coverage-design.md`. + +## Global constraints + +No game DLL/config/symbol changes, no public API1–7 or existing vtable changes, no Vulkan staging/copy removal, no bottom-left HUD layout changes, no MOD-name dependency or new external runtime dependencies. Instrumentation is inactive outside F8. Source changes stay on `codex/f8-measurement-coverage-20261112`. + +## Review focus + +Late writers across capture restart; lock ordering during CS suspension/Reset/destruction; snapshots or events exceeding bounded capacity; events crossing frame/phase boundaries; mistaking present reuse/background for a proven pixel freeze. Tests must cover each. + +## Task 1: Backend telemetry and actual-operation regression + +- [x] Define and document private structs/interface in `backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h`; agree boundary with runtime implementer. +- [x] Write native behavior tests that fail against immutable main DLL (`out/deploy/20261112-main-0d5680c09/runtime/backend/dxvk-gta/d3d9.dll`). Preserve red output. +- [x] Implement recorder, allocator/file mapping and CS/GPU wait instrumentation in backend files; expose bounded read/snapshot via optional QueryInterface. +- [x] Build and run real device tests including lifetime, overflow/restart, mapping, synchronization and pixels. Review backend changes independently. + +## Task 2: F8 runtime, CS stacks, context and HUD + +- [x] Add failing real runtime tests for CPU-running HUD, separate CS sample data, captured events/memory and context markers. +- [x] Implement in `src/bridge/legacy/BridgeMeasurementRuntime.cpp/.h` and narrowly related measurement files; preserve old report records, add labelled records. +- [x] Join spans/samples/snapshots to frame intervals, expose partial/unavailable/filtered/dropped data and overhead. +- [x] Run runtime native lifecycle/report regression and HUD pixel checks; independently review. + +## Task 3: Integration, measurement overhead and pipeline + +- [x] Wire new real-device gates into `tools/test.ps1` and document `docs/unified-measurement.md`. +- [x] Repeat paired A/B and A/A with fixed binaries, randomized/alternating order, independent processes; compare both inactive and active diagnostic cases. +- [x] Resolve meaningful regressions, run complete local validation with durable logs, and validate symbol/export/import/overlay consistency. +- [x] Run whole-change review, fix findings and rerun affected checks only. +- [x] Save evidence and candidate hashes; verify installed files equal `out/perf/20261112-measurement-coverage/game-before.json`. No deployment. + +Final verification: 88 passed, 0 failed, 2 optional external-input audits skipped. +Final backend benchmark: 36 serial processes. Final runtime benchmark: 96 captures +across 12 processes, with active A/B and A/A controls. Evidence and the validation +report are retained in `out/perf/20261112-measurement-coverage` and +`out/reports/20261112-f8-measurement-coverage-validation.md`. +Candidate: `out/candidates/20261112-f8-measurement-coverage`; both `.rsym` +identities match the copied DLLs. All eight protected game artifacts are unchanged. diff --git a/docs/superpowers/plans/2026-11-14-f8-tail-mapping.md b/docs/superpowers/plans/2026-11-14-f8-tail-mapping.md new file mode 100644 index 0000000..30797eb --- /dev/null +++ b/docs/superpowers/plans/2026-11-14-f8-tail-mapping.md @@ -0,0 +1,75 @@ +# F8 Tail and Mapping Implementation Plan + +> For agentic workers: use superpowers:subagent-driven-development task by task. +> No commits, pushes, deployment, or game runs. + +**Goal:** Preserve F8 tail evidence and reduce unnecessary main-thread texture mapping eviction. + +**Architecture:** Bounded asynchronous temporary event storage; streaming report reads; +strict-budget LRU reclamation preserving existing upload scopes and Vulkan copies. + +**Tech Stack:** Existing x86 C++ Windows/D3D9/Vulkan, MSVC Bridge, LLVM-MinGW DXVK, +native fixtures and PowerShell/Python verification. + +**Spec:** `docs/superpowers/specs/2026-11-14-f8-tail-mapping-design.md`. + +## Global Constraints + +- No game deployment, game run, commit or push. +- Preserve Vulkan staging/copy, rendering quality, public API1–7 and existing COM vtables. +- Respect explicit d3d9.textureMemory budget; no MOD-name dependency or new external runtime dependency. +- Preserve previous uncommitted telemetry work and compare this batch against initial-source. +- Run real behavior regressions, repeated fixed-binary A/B and A/A, and full local validation. +- All eight installed DLL/config/symbol artifacts must remain hash-identical to game-before.json. + +## Review Focus + +Late writers across capture restart and spool failure must not mix ownership. +No render-thread file I/O/wait or locks held over I/O; all temporary storage bounded. +Pinned/locked or oversized textures must preserve data and respect lifetime rules. +Mapped budget boundaries and failed allocation must not cause an endless retry loop. +Benchmarks must measure complete upload/Present work, not merely move work beyond the timer. + +### Task 1: F8 complete event stream + +Files: `src/bridge/legacy/BridgeMeasurementRuntime.cpp/.h`, narrowly required new +private storage helper, `tests/measurement-runtime-tests.cpp` and runtime fixture build files. +Interface: consume unchanged copied-data ID3D9BridgeTelemetry; keep existing event +records and frame association; add bounded storage status/counters. + +- [x] Preserve RED on old runtime: overflow history and miss a known late event. +- [x] Implement bounded background storage and streaming report iteration per spec. +- [x] Add real native high-volume capture, tail/frame association and count checks. +- [x] Cover open/write/read/queue pressure, cancellation/restart, worker and temp-file cleanup. +- [x] Run normal/old/system backend, fault and native lifecycle regressions; task review. + +### Task 2: Budget-respecting mapping reuse and smaller trims + +Files: narrowly related `backend/dxvk/src/d3d9/d3d9_device.cpp/.h` or memory helpers; +new real D3D9 mapping-budget probe/script and fixture build wiring as required. +Interface: existing configuration and D3D9 methods remain unchanged; native probes +observe OS operations through the existing private telemetry interface. + +- [x] Read mapping design review and test budget/reuse/locked/oversized behavior against baseline. +- [x] Preserve a failing real reuse/eviction regression before changing the policy. +- [x] Implement minimal reclamation without the fixed 75% low-water over-eviction. +- [x] Retain initialized mappings within the same LRU budget and remove allocating + LRU touches, preserving exception safety and initializer/device lock ordering. +- [x] Verify actual map/unmap counts, bounded mapped bytes, uploaded/readback pixels, + dirty mips/faces, locked/upload-pinned sources, multi-device and Reset/release. +- [x] Verify initialization/first-lock reuse, create-only pollution and allocation + failures; freeze reclamation-only and combined builds for attribution. +- [x] Independently review the policy and real behavior tests. + +### Task 3: Performance, integration and complete validation + +Root owns independent orchestration scripts, pipeline wiring, docs and packaging. +Consume final frozen Task 1/2 binaries and existing baseline; do not rebuild during benchmarks. + +- [x] Freeze version comparison fixtures; record both baseline and candidate source/binary hashes. +- [x] Run repeated F8 version A/B and A/A, normal and high-volume; measure inactive controls and completion costs. +- [x] Run repeated mapping workloads, including below-budget, burst reuse, cold and over-budget controls; + compare total/burst/Present latency and memory/OS map statistics. +- [x] Resolve actual regressions, run full pipeline, task/whole-change review and affected reruns. +- [x] Document observed benefits and limits; freeze local candidate with symbols and source manifest. +- [x] Verify installed game files unchanged and retain complete evidence; no deployment. diff --git a/docs/superpowers/plans/2026-11-14-release-cs-wait.md b/docs/superpowers/plans/2026-11-14-release-cs-wait.md new file mode 100644 index 0000000..b04d6be --- /dev/null +++ b/docs/superpowers/plans/2026-11-14-release-cs-wait.md @@ -0,0 +1,59 @@ +# Mapping release and CS wait implementation plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [x]`) syntax for tracking. + +**Goal:** Remove the concentrated release regression while retaining mapping wins, and make the observed CS wait precisely attributable. + +**Architecture:** Prototype bounded final-view retirement on the real OS boundary, then integrate only if full-lifetime timing improves. Add capture-only sequence and command-buffer spans without changing rendering order. + +**Tech Stack:** C++/Win32, D3D9/DXVK/Vulkan, Python, PowerShell. + +**Spec:** `docs/superpowers/specs/2026-11-14-release-cs-wait-design.md` + +## Global Constraints + +- Preserve Vulkan staging/copy, quality, public interfaces and configured memory budget. +- Keep all cleanup inside measured lifetime; inactive telemetry stays low overhead. +- No game deployment, game capture, commit or push. + +## Review Focus + +- Shared mapped page still referenced by another live texture. +- Recycled file range or chunk while an old view is pending. +- Cleanup worker startup failure and shutdown with work queued. +- Pending mapped bytes under budget pressure and capture termination. +- CS high-priority work, ordered sequences, driver spans and truncated captures. + +### Task 1: Explain and prototype final release + +**Files:** `tests/mapping-budget-probe.cpp`; task evidence under `out/perf/20261114-release-cs-wait`. +**Interfaces:** real `UnmapViewOfFile` import; separate untimed residency pass; `releaseNs` includes drain. + +- [x] Record old/current resident-page counts and existing release timing. +- [x] Add opt-in test-only bounded worker and compare same DLL with hook off/on. +- [x] Record full release, total, main cycles and true OS completion; reject if no repeatable gain. + +### Task 2: Implement retained mechanism + +**Files:** `backend/dxvk/src/util/util_unmap.{h,cpp}`, D3D9 mapping budget callers and focused tests as needed. +**Interfaces:** final region release distinct from synchronous eviction; pending bytes remain counted; pool destruction drains. + +- [x] Add real behavior checks for retained views, budget, shared pages, reuse, failure and teardown; run against baseline. +- [x] Implement only the mechanism supported by Task 1, recording any evidence-based plan revision. +- [x] Run focused pixels, concurrency, reset, failure and lifetime regressions. + +### Task 3: CS wait attribution + +**Files:** telemetry protocol/recorder, `dxvk_cs.cpp`, `dxvk_cmdlist.cpp`, bridge report decoder and real telemetry probe. +**Interfaces:** existing CS_WAIT identity is target sequence; capture-only chunk and driver spans preserve temporal nesting. + +- [x] Add real-device wait correlation test; demonstrate missing old evidence. +- [x] Instrument precise chunk/driver boundaries and extend report/test decoder. +- [x] Verify actual sequence correlation, tail handling and inactive/active overhead. + +### Task 4: Acceptance and packaging + +- [x] Freeze baseline/current/final binaries; alternating A/B and A/A across mapping and LockRect workloads. +- [x] Run complete local pipeline once focused gates pass; repair any introduced failure. +- [x] One independent whole-change review; resolve findings and rerun affected gates. +- [x] Write evidence report, package local candidate, verify game files remain unchanged. diff --git a/docs/superpowers/plans/2026-11-14-texture-init-lockwait.md b/docs/superpowers/plans/2026-11-14-texture-init-lockwait.md new file mode 100644 index 0000000..b35dbdc --- /dev/null +++ b/docs/superpowers/plans/2026-11-14-texture-init-lockwait.md @@ -0,0 +1,156 @@ +# Texture Initialization and Lock Wait Implementation Plan + +> For agentic workers: use superpowers:executing-plans natively, task by task. +> Preserve uncommitted baseline; no commits, pushes, deployment or game runs. + +**Goal:** Remove proven redundant initialization work and evaluate bounded command +pool growth at the observed LockRect synchronization path. + +**Architecture:** Fresh-allocation provenance under existing allocator ownership; +small command-buffer batches without changing synchronization or submission order. + +**Tech Stack:** Existing x86 C++, Windows D3D9/Vulkan, LLVM-MinGW/MSVC, native probes, +PowerShell/Python orchestration. + +**Spec:** docs/superpowers/specs/2026-11-14-texture-init-lockwait-design.md + +## Global Constraints + +- Preserve staging/copy, rendering quality, public interfaces, configured mapping budget. +- No MOD-name dependence, new runtime dependencies, deployment, game run, commit or push. +- Tests use real device/OS paths; repeated fixed-binary A/B and A/A and full pipeline. +- Preserve previous uncommitted changes and all eight protected game-file hashes. + +## Review Focus + +- Recycled/shared storage must never be mistaken for fresh zero memory. +- Failed allocation/map must not corrupt allocator state or expose stale bytes. +- Concurrent creation, active locks and GPU upload pointers preserve lifetime. +- Command buffer reserve is bounded; failed batches cannot strand handles. +- Required readback ordering, CPU/GPU synchronization and pixels remain unchanged. + +### Task 1: Fresh texture initialization + +Files: util_unmap.h/.cpp, d3d9_common_texture.h, d3d9_initializer.cpp, +tests/texture-init-probe.cpp and tests/texture-init-test.ps1. +Private interface: allocation-origin zero proof, consumed only at initialization. + +- [ ] Preserve source/runtime hashes; implement real zero-write observation and byte/pixel controls. +- [ ] Run baseline RED for redundant fresh initialization; keep failure log. +- [ ] Implement conservative fresh-range proof and zero-clear elision; preserve map and data paths. +- [ ] Run fresh/reused/shared/explicit-data/mip/face/volume/failure/concurrency/budget controls. +- [ ] Freeze initialization-only binary and focused validation evidence. + +### Task 2: CS command pool allocation experiment + +Files: dxvk_cmdlist.cpp, tests/lib/vulkan-dispatch-observer.h, +tests/lockrect-pool-probe.cpp, tests/lockrect-pool-test.ps1. +Public interfaces unchanged. Observe existing Vulkan dispatch boundary only in tests. + +- [ ] Build real readback/driver allocation observer; preserve baseline measurements and RED. +- [ ] Evaluate two-buffer growth with single-buffer fallback on allocation failure. +- [ ] Verify exact readback pixels, late writes, multiple resources, reset and allocation-failure cleanup. +- [ ] Freeze combined candidate; reject or refine if controls reveal unsupported tradeoffs. + +### Task 3: Performance and final validation + +Files: narrowly needed tools/test.ps1 wiring, benchmark runner/validator, validation report. + +- [ ] Run serial repeated A/B and A/A for initialization, existing mapping workloads and readback controls. +- [ ] Compare initialization-only vs combined to separate contributions; include total cleanup/drain. +- [ ] Request independent whole-change review under executing-plans; resolve important findings with RED/GREEN. +- [ ] Run complete tools/test.ps1 with real hardware and historical Bridge reference; inspect every result. +- [ ] Freeze verified local candidate/symbols/source/import manifests, verify game hashes unchanged. + +## Execution ledger + +Scope ruling: the latest user directly authorized optimization of the two measured +paths under the established regression/benchmark constraints. Routine implementation +choices remain with the implementer. Work is native and uncommitted; preserve an +immutable initial-source snapshot instead of using commit ranges for review. + +Ruling: extend the existing real mapping-budget probe and runner, rather than +duplicating its device/pixel/residency setup in new texture-init files. The same +native paths cover fresh and recycled memory; existing benchmark fixtures stay +fixed during each comparison. + +Task 1 evidence: baseline fresh-zero RED (262144 redundant bytes), candidate +GREEN (0 fresh / 262144 recycled bytes, 66 checks). Allocation failure retry +baseline RED (3 failures), candidate GREEN (21 checks). Initializer/concurrent +296449 checks and 1 MiB pressure 284 checks passed. + +Task 2 evidence: two-buffer growth passed 4974 checks and injected-failure +fallback 4975 checks, but only reduced 7 driver calls to 6 while allocating 12 +buffers instead of 7. Six A/B + three A/A pairs in two readback workloads showed +no repeatable benefit (A/B total +5.06%/+5.24%, broad A/A variation). Reject this +batching experiment; preserve its frozen binary and evidence, restore original +command allocation and retain generic real readback regression in the suite. + +Ruling: evaluate a second targeted mapping improvement: prioritize never-used +initialized mappings for eviction, without evicting hot mappings merely to admit +another initialized source. Source inspection found all retained initial maps +currently share the same recency queue as used maps; at the budget ceiling this +forces an immediate unmap/remap even while never-used maps remain resident. +Use the existing LRU with cold insertion and one per-texture use flag, not another +cache or an expanded memory budget. This directly addresses the requested +repeated mapping reclamation; retain only after exact bytes/pixels, pinned-map, +multiple-device, repeated A/B/A/A controls pass. + +Mapping admission RED: first-use maps=1 and hot-survivor maps=1; GREEN: both 0, +368 checks. Existing initializer/concurrency (296449) and pinned 1 MiB (284) +checks still passed. Added real volume/mip zero checks (296479 total initializer). + +Ruling: fix failed MapView ownership as part of the mapping budget path. The +new fault test proved one failed view permanently consumed 1 MiB + 64 bytes of +budget and evicted an otherwise resident hot source (3 baseline failures). +Commit map/page reference counts only after successful mapping, never offset a +null base pointer. Candidate passes both exact-budget retry and a failed shared +page mapping at a nonzero offset (170 checks). + +Benchmark evidence: init-only six A/B and three A/A pairs over six workloads +completed; create total -7.80%, cycles -3.82% (6/6 cycle pairs faster), hot cycles +-3.77% (6/6). Other controls near A/A spread. Final mapping candidate comparison +adds the admission workload and is in progress. Two-buffer experiment is removed +from product code; its source, binary, observer and fallback evidence are retained +under out/perf/20261114-init-map-lockwait/rejected-pool and combined. + +Independent whole-change review: no Critical/Important/Minor introduced findings. +All 14 review-file hashes matched, command pool source matched the initial snapshot. +Boundaries: observer verifies real readback/reset/lifecycle, not independent fence +completion proof; dedicated >256 MiB path code-reviewed, not specifically exercised. +Inherited allocation-index-zero bool and later LockImage HRESULT behavior left +outside this patch; no new dependency on either for the fresh-memory proof. + +Build audit caught a stale command-pool object after source rollback: Copy-Item +preserved an old source mtime, so the next incremental build retained batch2. +The mapping-candidate/final-ab files are INTERMEDIATE COMBINED experiments, +not final acceptance. Preserve them, force recompilation of the restored source, +verify observed batches=0, freeze a new final binary, then repeat final A/B/A/A +and the complete pipeline. Earlier init-only and pool-ab experiments retain their +original correct scope. This is a build-artifact correction, not a product change. + +Final source rebuilt; native observation batches=0, 7 calls/7 primary buffers, +4972 checks passed. Complete pipeline 2026-11-14 00:18:15–00:27:01 UTC completed +in one run: 102 passed, 0 failed, 2 optional external-reference audits skipped. +All required gates passed, including new mapping/failure/readback checks and F8 +tail/HUD/runtime checks. Frozen performance backend SHA256: +52e725a9758a976a111c9d4f34caf8af34177cccc91c25153022a414f5304b6b. +accepted-ab repeats all seven workloads (6 A/B + 3 A/A pairs each) against the +installed baseline. Final readback controls use 2048 operations per process. + +Integration ruling: the user's existing scope is local candidate, no deployment +or commit/push. Keep this branch and all evidence in place; no integration menu +or extra approval is needed. This supersedes the finishing skill's default menu. + +Final measurements completed: 126 mapping runs, 36 LockRect runs, all valid and +fixed-binary hashes preserved. Target admission: total -26.81%, main cycles +-29.18%, maps 2043→1024, operation P95 -25.66%; all 6 target pairs improve. +Creation total -3.97%, hot reuse cycles -4.30%; remaining controls near A/A. +Readback total -0.36% / upload-readback -0.72%, within A/A variation: do not claim +the 15.54 ms game CS wait is fixed. + +Explicit open tradeoff: admission end cleanup unmap 0.5866→7.1352 ms, complete +release 7.5314→12.3047 ms; both versions release 98 maps and have identical end +mapped bytes. Full-life unmap 93.8789→57.3136 ms and total cost improve, including +cleanup, but concentrated-release latency regresses. Keep this visible in the +local candidate/report; no deployment or claim that game long frames are solved. diff --git a/docs/superpowers/specs/2026-11-12-f8-measurement-coverage-design.md b/docs/superpowers/specs/2026-11-12-f8-measurement-coverage-design.md new file mode 100644 index 0000000..71d888f --- /dev/null +++ b/docs/superpowers/specs/2026-11-12-f8-measurement-coverage-design.md @@ -0,0 +1,40 @@ +# F8 measurement coverage design + +The user authorized implementation after the Grove Street capture exposed missing memory history, CS stacks and per-frame wait attribution. This document records the implementation choices within that scope; there is no deployment, commit or publication step. + +## Requirements + +- Preserve rendering, public API1–7 and existing diagnostic/lifecycle vtables; retain Vulkan staging and copies and the bottom-left HUD layout. +- Capture memory chunk changes (the data behind the bottom-right HUD), allocation/free and map/unmap duration, CS call stacks, resource/CS/GPU waits, and correlate timestamps with successful-Present intervals and slow frames. +- Record foreground/minimized transitions and render-reuse/freeze evidence. Foreground loss or low draw counts must not be presented as proof of pixel-identical output. Distinguish observed signals from inferred freeze candidates. +- Correct the CPU-running HUD text; main and CS sampling must be separately labelled with missed/dropped/chain quality and direct suspension overhead. +- Remain self-contained, without MOD-name checks, external collectors, extra runtime DLLs or symbol servers. +- Regress real device behavior, lifecycle/concurrency and pixels; repeat paired A/B plus A/A controls; run the full local validation pipeline. Do not deploy. + +## Approach and alternatives + +Use bounded in-process events plus low-frequency snapshots and post-capture correlation. This fits the existing F8 runtime and can work without administrator tracing. Always-on detailed tracing and per-frame full allocator scans are rejected because they add overhead outside the requested diagnostic window. GPU readback to detect frozen pixels is rejected because it adds synchronization and perturbs the workload; the report will identify render-reuse candidates explicitly. + +## Boundary and data + +Add one optional private COM interface, `ID3D9BridgeTelemetry`, with a distinct GUID, fixed-width copied-data structs, validated StructSize/Version and bounded caller capacities. Existing interfaces retain their ABI. Methods: StartCapture, StopCapture, ReadEvents and GetMemorySnapshot. Start/Stop identify the capture; old or mismatched capture IDs must not consume new data. Unsupported backends remain usable and report unavailable telemetry. + +Backend events carry capture ID, event kind, begin/end QPC nanoseconds, producer thread ID, opaque resource/allocation identity, byte count, outcome and flags. Timing scopes are inclusive and may nest. Buffers are bounded; dropped and filtered records are explicit. Disabled fast paths perform no clock query, allocation, file I/O or symbol work. A recorder lock must never be held over a Vulkan/OS resource operation or wait, or while acquiring a device/allocator lock. Concurrent late operations cannot write into a new capture generation. + +Record actual allocator operations, file-backed memory alloc/free/map/unmap and actual CS/GPU wait spans. Resource details below a documented duration/size threshold may be aggregated or filtered, with counts; failure and large/slow events remain eligible. Never silently claim every allocation was traced. + +Snapshots use the same allocator state as the memory HUD, recording heap/type, chunk cookie, capacity, used bytes, mapped/active state and bounded occupancy masks where supported. They include timestamps, scan overhead, totals versus copied counts and truncation flags. Default cadence is 500 ms during F8, plus capture boundaries. No snapshots outside an active capture. Bounds and cadence are explicit report metadata. + +## Runtime + +The main thread remains sampled at the configured cadence (default 5 ms); CS uses the measured CS thread ID and a lower default cadence (10 ms). Never suspend both at once. Copy preallocated context/stack data while suspended, resume before walking, allocating, resolving symbols or touching runtime/backend locks. Preserve worker cancellation, capture identity and Reset/final-release guarantees. CS failure is separately reported and must not discard valid main samples. + +Telemetry spans and both sample streams join to the same frame QPC intervals after collection; cross-frame events list overlap rather than being charged entirely to one frame. Do not add inclusive timing categories as if they were exclusive frame components. + +Foreground/minimized state is observed from the actual device window/process; game-frame progress can use the existing safe readFrame callback. Mark unchanged game-frame or present-without-render work as explicit freeze/reuse evidence with confidence/source. No dependence on Proper Shaders internals or log parsing. Preserve raw frames and provide counts/ranges excluded from steady-state interpretation. + +## Validation + +Real GPU tests create/use/release buffers/textures, force real host-visible/file mappings, issue resource and CS synchronization, compare copied/rendered pixels, and verify event timestamps/identities and chunk changes. Include capture restart, overflow, missing interface, invalid arguments, worker shutdown, secondary devices, Reset/final release and slow-frame cross-boundary correlation. + +Runtime tests verify the actual report and actual HUD published during CPU sampling, separate CS samples, foreground/freeze evidence and clean teardown. Benchmark the identical workload with collection off/on and A/A, alternate order across independent processes, and retain per-stage elapsed/cycle, recorded/filtered/dropped events and suspension overhead. Capture-off throughput must stay within measured noise; active costs are reported, not converted to a GTA FPS claim. Full validation includes exports/imports, source provenance/overlay, real device regressions and HUD pixel layout. Installed eight-file hashes remain unchanged. diff --git a/docs/superpowers/specs/2026-11-14-f8-tail-mapping-design.md b/docs/superpowers/specs/2026-11-14-f8-tail-mapping-design.md new file mode 100644 index 0000000..51591b5 --- /dev/null +++ b/docs/superpowers/specs/2026-11-14-f8-tail-mapping-design.md @@ -0,0 +1,115 @@ +# F8 tail retention and texture mapping reclamation + +User-authorized local work: fix lost F8 tail events, then optimize generic texture +mapping budget usage/reuse/reclamation to reduce main-thread long frames. Preserve +Vulkan staging/copy, rendering quality and public interfaces. Real behavior +regressions, repeated paired A/B with A/A, and full local validation are required. +No game deployment, game run, commit or push. + +## Evidence and baseline + +Capture 19168-1 retained 65536 events then discarded 21624 host-side events after +14.833 seconds. Its 114.83 ms peak at 10.61 seconds contained 299 OS unmaps costing +11.87 ms, 8/20 main samples through packImageData, and 9/10 CS samples waiting on +the command queue. The configured CPU mapping budget is 100 MiB; current eviction +continues to 75 MiB. Native allocation/copy/flush behavior must remain unchanged. + +The deployed baseline is the immutable candidate in +`out/perf/20261114-f8-tail-mapping/baseline-candidate`. Initial uncommitted source +is preserved in `initial-source` with hashes. Compare changes against that source, +not just git HEAD, which predates the telemetry implementation. + +## F8 retention decision + +Retain exact completed events across the normal capture, using a bounded producer +queue and a capture-owned temporary sequential binary stream written by a managed +background worker. This replaces a lifetime prefix-only vector. The render thread +does not perform event file writes or wait for disk progress. All symbol lookup, +frame association and text formatting remain in the report worker. + +Bound in-memory event staging to at most 2 MiB, allocated at capture startup; +bound temporary event storage to 128 MiB. Stream events back in bounded blocks for +the final report rather than loading the whole file into the 32-bit process. +The exact sizing and block packing are implementer choices within these bounds. +Use existing host worker ownership and stop/join conventions; no device reference +or COM access from the spool worker. No event-storage work outside active F8. + +Queue pressure, allocation/open/write/read failure, or the file cap must be +explicitly reported as incomplete telemetry, with accepted/written/omitted counts +and an unavailable/error indication. Never emit an apparently complete zero-loss +stream after such failures. Reset/release/shutdown and restart must not lose already +accepted data silently, mix capture IDs, leak worker handles or leave temporary +files after their owner is destroyed. Keep the private backend interface and +existing report event/frameOverlap records compatible; append storage metadata. + +At least one regression must exceed the former 65536 limit through the production +runtime and actual D3D9/Vulkan operations, verify an event at the late end of the +capture, verify frame association, and verify count conservation. Deterministic +failure injection belongs in fixtures at the existing dependency boundary. + +## Mapping decision + +Respect the explicit `d3d9.textureMemory` budget. The first candidate removes +unnecessary 25% over-eviction: when over budget, reclaim only enough LRU mappings +to return within the configured budget. Keep existing locks, active-upload scope, +subresource lifetime, and destruction/Reset ordering intact. This retains mappings +that fit within the budget and spreads small incoming demand across smaller trims. +Do not introduce an unbounded mapping cache, eviction worker or new loader hook. + +Budget zero, exact boundary, oversized texture, all-candidates-pinned, allocation +failure, multiple devices and Reset/release need explicit treatment consistent +with existing contracts. Do not claim a time cap on reclaiming an oversized source. +Assess a larger bounded budget as a separate experiment only; do not silently +override the configured budget or change the game configuration. + +An improvement must be visible in real OS map/unmap behavior and end-to-end +upload/Present latency, with identical uploaded/readback pixels. Controls include +already-below-budget reuse, cold sequential uploads and working sets larger than +the budget. If repeated A/B shows no useful improvement or unacceptable control +regression, reject/adjust the optimization rather than retaining a code-only win. + +### Evidence-led refinement: initialization and LRU reuse + +The retained real prefix also contains 879 cookie-matched remaps after unmap, 632 +within 16 ms. This does not identify every caller. A concrete source path exists in +InitTexture: initialization unconditionally unmaps before the first application +Lock. Evaluate retention of the already initialized unmappable source within the +same LRU/budget, after SyncSharedTexture succeeds. Register only after all pointer +uses finish; an internal device method takes the existing device lock. Initialized +objects are complete and held by the caller's Com. No new COM/API surface. + +The evaluated always-admit MRU variant polluted the `unused` control: a single +512-operation smoke observed501 additional OS remaps versus reclaim-only. Preserve +that discarded variant as evidence. The final policy admits an initialized mapping +only if total logical mapped bytes already fit the same explicit budget; otherwise +it unmaps that new source and applies ordinary reclamation. First actual Lock still +uses normal demand-driven LRU admission. Verify old hot data stays reusable across +create-but-never-use pressure, including existing pinned/locked mappings. + +Existing LRU touches allocate a fresh list node. Use list splice for existing +entries and strong exception safety for first insertion (rollback list node if +index allocation fails). Initializer admission allocation failure falls back to +unmapping the new source and ordinary reclamation, preserving creation behavior. +No new allocation failure may leave a dangling LRU pointer. + +Real tests must distinguish Create/initialization from first Lock, preserve initial +zero/provided-data pixels, bound create-only memory, protect existing locks, and +exercise MULTITHREADED creation/reclamation and allocation-failure cleanup. Include +create-and-never-use pollution control. Freeze reclamation-only and combined builds +to separate their contributions; keep any initializer-retention change only after +same-budget paired data supports it. Do not label all observed remaps initializer-caused. + +## Validation and delivery + +Run focused red/green native runtime and texture lifecycle/pixel tests, then task +review. Run fixed-binary version A/B and A/A serially without parallel builds or GPU +tests; include P95/P99/max/burst latency, unmap counts/timing and mapped-memory peak. +Measure F8 inactive, normal active and high-volume active cases, including report +completion latency and failure counters. Run the complete repository pipeline with +the verified historical Bridge reference and installed toolchain. Freeze the local +candidate and matching symbols; verify all eight protected installed artifacts +remain unchanged. Fixture results do not establish game FPS or exit-spike gains. + +Mapping lifetime totals include release of measured resources and the final GPU +drain. Report release time separately, within that total, so retaining an initializer +mapping cannot appear beneficial only by deferring its unmap beyond the timer. diff --git a/docs/superpowers/specs/2026-11-14-release-cs-wait-design.md b/docs/superpowers/specs/2026-11-14-release-cs-wait-design.md new file mode 100644 index 0000000..7c1c60f --- /dev/null +++ b/docs/superpowers/specs/2026-11-14-release-cs-wait-design.md @@ -0,0 +1,40 @@ +# Concentrated mapping release and CS wait attribution + +The previous candidate keeps the hot mappings resident. The final 98 views have +25,088 resident pages, versus 863 with the installed baseline. An untimed +QueryWorkingSetEx pass measured 4.7785 ms versus 0.4950 ms in UnmapViewOfFile. +The previously accepted repeated benchmark reported 12.3047 versus 7.5314 ms +for the complete release phase. Retain the measured main-thread improvements. + +First experiment: one bounded worker for final-release OS unmapping. Budget +evictions stay synchronous. Prototype at the actual OS import in the real-device +probe before changing product code. Include the worker drain inside release and +total timers. Reject a change which only moves cleanup beyond measurement. +Keep the resident-page query in a separate untimed observation. + +If the experiment passes, integrate only unreferenced final-release views. No +more than 128 jobs, 128 MiB of views or 128 MiB of backing files may be pending. +These are already-owned resources, and the two limits apply separately rather +than double-charging the same pages. Only mapped working sets of at least eight +MiB, or the remainder of an existing pending burst, use the worker: the initial +unconditional prototype regressed the real create/release control by 13%. +Pending bytes remain accounted +until OS unmapping completes; allocation/budget pressure drains pending cleanup +before evicting useful hot mappings. Shared-page references, recycled ranges, +thread-start failure, device reset/destruction and telemetry lifetime must remain +correct. New backing storage drains pending files first; failed maps with pending +views may drain and retry once. No additional DLL dependency. The worker must not +run on the CS queue. Report submit latency and full release completion separately; +the Windows teardown work is still present and included in the lifetime timer. + +Second, resolve the diagnostic gap around the 15.538 ms LockRect CS wait. The +existing capture proves a wait on a sequence and contains one command-buffer +allocation sample, but cannot attribute the whole wait to that sample. Add +capture-only CS chunk/sequence and Vulkan command-buffer boundary spans as needed +and verify their correlation against a real-device controlled wait. Never remove +a resource dependency based only on the sampled stack. + +Preserve Vulkan staging/copy, image quality, public interfaces, configured memory +budget and existing HUD placement. Run real pixels/lifetime/failure regressions, +fixed-binary alternating A/B and A/A, complete local validation and final review. +No game deployment, game capture, commit or push in this batch. diff --git a/docs/superpowers/specs/2026-11-14-texture-init-lockwait-design.md b/docs/superpowers/specs/2026-11-14-texture-init-lockwait-design.md new file mode 100644 index 0000000..ede95bf --- /dev/null +++ b/docs/superpowers/specs/2026-11-14-texture-init-lockwait-design.md @@ -0,0 +1,83 @@ +# Texture initialization and LockRect synchronization cost + +The user requested optimization of the two measured paths in capture 66984-1. +Implement within the existing authorized scope: generic behavior, unchanged +quality/public interfaces, preserved Vulkan staging and copies, real regression +tests, repeated A/B plus A/A, full local validation. No deployment, game run, +commit or push. Implementation choices below are engineering hypotheses, not +claims that the observed 114.96 ms or 15.54 ms intervals will disappear. + +## Evidence and alternatives + +The 114.96 ms frame has 12/20 main samples through packImageData and 13.40 ms +OS unmapping. A second 55.08 ms frame contains InitHostVisibleTexture samples. +The next 38.28 ms frame waits 15.54 ms in LockRect/WaitForResource/CS synchronize; +concurrent CS samples include getCommandBuffer/allocateCommandBuffer and driver +calls. See out/reports/20261114-grove-first-exit-f8-66984-1/report.md. + +Current initializer eagerly maps and zeros all non-default CPU texture storage. +Fresh paging-file-backed bytes are already zero. Existing reuse and 100 MiB +budget must remain. Skipping first mapping would move mapping failures to Lock; +the proposed path retains first mapping/failure behavior and only skips redundant +zero stores where the allocator proves bytes have never been handed out. +Reused or uncertain storage continues through the original clear path. + +Current primary command buffers grow one at a time on CS. Evaluate a small bounded +batch (initially two, optionally four as a separate experiment), retaining pool +reset/reuse and command ordering. A batch allocation failure must retry one buffer +before preserving the original failure. This is a candidate for lowering driver +allocation frequency, not permission to omit resource waits. Reject it if repeated +real-device controls do not justify the added reserved buffers. + +Alternatives not selected: larger texture budget (changes configured bounds), +unbounded mapping/buffer cache, background reclamation, skipping resource waits, +rewriting staging/copy, or eagerly flushing every readback (may create more +submissions). No MOD-name detection. + +## Initialization proof and lifecycle + +On 32-bit Windows, a chunk tracks an exclusive high-water mark of byte ranges +ever allocated since creation. An allocation wholly above this mark is provably +zero; advance the mark on every allocation, never on free. Holes below it are +conservatively treated as reused. Dedicated paging-file mappings are fresh. +Move/free and chunk destruction/recreation preserve/reset the proof. Allocator +metadata is protected by its existing lock. Non-Windows/64-bit paths retain +existing initialization unless a separate equivalent proof exists. + +The proof describes initial allocation contents, not live contents after writes. +Only the single resource initialization site consumes it; explicit initial data +is always copied. First mapping still occurs and initialization map failure still +returns creation failure. Existing device locking, LRU admission, budget, padding, +multi-mip/face/volume offsets, upload pinning, and GPU initialization remain. +If touching allocation rollback reveals the known CreateFileMapping failure +recovery issue, fix only the ownership/rollback needed by this path and cover it +with actual fail-once/retry behavior. + +## Tests and acceptance + +Use the actual x86 DLL and actual D3D9/Vulkan device. Observe OS/CRT/Vulkan call +boundaries in test-only import/dispatch hooks, forwarding to real implementations. +Prove fresh sources avoid redundant zero writes, while dirty reused slices, shared +pages, partial locks, mips/cubes/volumes and explicit data preserve exact bytes and +rendered/readback pixels. Include multiple devices, MULTITHREADED, reset/release, +map/allocation failures and budget pressure. + +For command pools, observe driver allocation requests and handle lifecycle through +real workload, verify reserved-buffer reuse, reset only after GPU completion, +pixel correctness, and batch-failure single fallback. Timed runs do not inject +delays or allocation failure. Capture driver-call counts/timing, LockRect and total +work/Present/drain/release time, main cycles, latency quantiles, and memory bounds. +Include cold, warm, create-but-unused, over-budget and readback controls. Compare +separate initialization-only and combined binaries; freeze all binaries before +serial benchmark runs. Retain rejected experiments and all raw observations. + +The baseline is the currently installed 1e8120cc DXVK / 11f47624 Bridge, copied to +out/perf/20261114-init-map-lockwait/baseline-runtime. The initial dirty source is +archived separately; git HEAD predates both telemetry and the previous candidate. +Verify all eight installed artifacts unchanged after validation; freeze candidate +DLLs/symbol identities and document imports/dependency changes (none intended). + +References: Microsoft CreateFileMappingA documents zero initial paging-file pages: +https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createfilemappinga +Vulkan documents multi-buffer allocation and atomic failure cleanup: +https://docs.vulkan.org/refpages/latest/refpages/source/vkAllocateCommandBuffers.html diff --git a/docs/unified-measurement.md b/docs/unified-measurement.md index a7292b1..1e0b01f 100644 --- a/docs/unified-measurement.md +++ b/docs/unified-measurement.md @@ -18,6 +18,7 @@ CpuHotspots=1 JournalAttribution=1 BaselineMs=5000 SlowFrameMs=50 +CsIntervalMs=10 ``` `Enable=0` retains the standalone F7/F8 behavior. `ShowHud=0` keeps collection @@ -81,12 +82,181 @@ These counters are not additive frame costs, and pending excludes injected high-priority commands. Missing support, incomplete windows or changed caller thread identity produce `available=0`, without interrupting other diagnostics. +The extended F8 capture records allocator and synchronization evidence through +an optional, separate private telemetry interface. Collection is off outside +F8; compatibility mode and third-party MODs are not prerequisites. The existing +Vulkan staging allocations, texture copies and rendering order remain intact. + +`telemetryEvent` records contain QPC-derived nanosecond begin/end times, calling +thread, operation kind, identity, bytes, result and scope flags. The scopes +distinguish resource suballocations, Vulkan backing allocations, file-pool +operations and actual OS map/unmap calls. A logical map can reuse an existing +mapping, and a resource free can return space to a pool without freeing its +backing allocation. Reusing an existing mapped pointer does not produce an OS +map call; these events do not time every D3D9 Lock/Unlock API invocation. +Allocation bytes can be requested size while free bytes include alignment, so +their difference is not a physical-memory balance. Submission-status waits +are distinguished from GPU-completion waits, and a device-idle span can include +queue draining and driver work. These inclusive spans may nest and must not be added as +independent frame costs. Successful operations below both 50 microseconds and +64 KiB are filtered; failures remain eligible. The event queue and retained +history are bounded, and the report includes filtered, dropped, late and +in-flight counts. Completed events do not describe an operation still waiting +when collection stops. + +Completed backend events are drained in bounded batches on active Presents, +independently of the 500 ms memory-snapshot cadence. Capture history uses a +32,768-event staging ring and a managed background writer, with less than 2 MiB +of simultaneous event staging and a 128 MiB temporary binary file limit. The +render callback neither writes event files nor waits for the writer. The report +worker streams the file in 256-event blocks, preserving late events beyond the +former 65,536-event lifetime limit. Temporary files are deleted when the capture's +writer/reader ownership ends; idle F8 does not start an event-storage worker. +After observing seal or abort, the writer acquires the queue head again before +deciding that the stream is drained. This preserves events published between an +earlier empty-queue observation and the final stop publication. + +`telemetryStorage` is the final storage-completeness record. It separates received, +accepted, written, reported, omitted and unread events, and records queue pressure, +file open/write/read errors, file-cap exhaustion, worker-start failure and shutdown +cancellation. `complete=0` and the telemetry unavailable bit make partial evidence +explicit. The report header is finalized after streaming so a read failure is also +visible there. Backend drops and operations still open or unread after stop also set +the telemetry unavailable bit; their individual counters remain in the report. +An operation in flight while capture is active is normal and does not alone mark +the capture unavailable. Do not infer zero lost events merely from a report file +existing or the phase controller reaching its end. +Terminal accounting errors use bit 256: accepted/written/received ordering, +written plus omissions, drained head/tail, and reported bounds must agree. The +check flags a discrepancy without inventing omissions to repair the totals. +A complete stream additionally requires received=accepted=written=reported and +zero omissions and errors. + +The final `reportEnd captureId=... completed=1` row confirms that report generation +reached its terminal record; storage completeness remains a separate check. Active +runtime snapshots count fully spooled events. After reporting finishes they count +only events in the successfully finalized report, and include omitted or unread +events in the loss count. Report-open/write failure leaves zero finalized events +and an explicit report error rather than treating worker exit as successful output. + +`memorySnapshot`, `memoryChunk` and `memoryMask` persist the pooled allocation +data used by the bottom-right HUD. Each chunk has a stable cookie, heap/type, +capacity, used bytes, mapped/active flags and occupancy masks. Snapshots are +taken at capture boundaries and at the first Present at least 500 ms after the +previous one. Their actual spacing, scan time, missing observations and +truncation are recorded. No Present means no periodic allocator snapshot; +there is no additional flush or synchronous GPU readback to obtain one. These +pooled totals exclude dedicated allocations and are not whole-process RAM or +total GPU memory. The bottom-left frame-time HUD layout is unchanged. +The runtime retains at most 128 snapshots, 256 chunks and 4096 mask words per +snapshot. Adjacent snapshots also produce chunk changes; missing chunks in a +truncated snapshot remain uncertain instead of being reported as freed. +Frame-latency waits and sampler-pool fence waits are separate kinds. Their +pending check means the signal was incomplete when observed; it can become +ready before the thread actually sleeps. Swapchain image acquisition is an +inclusive CPU/driver/acquire span, not a pure GPU timer. + +CPU sampling uses one worker that suspends the main thread and CS separately, +never together. Main sampling keeps its configured interval; CS defaults to +10 ms. All symbol resolution, stack interpretation and report writing happen +after resuming the target. `cpuStream kind=main` and `kind=cs` report separate +sample counts, errors, missed/dropped records, structural-chain quality and +suspension time. Failure to sample CS does not discard the main stream. The +HUD says when CPU sampling is running instead of retaining the baseline +waiting message. +Actual sample count and elapsed sampling time establish the achieved rate. +Scheduling misses count late wake-ups, not every interval extended by sample +processing. A zero miss count does not prove the nominal rate was achieved. + +`frameOverlap` associates event spans with each intersecting half-open Present +interval. Samples and memory snapshots also carry frame references. A phase +boundary whose start is not a successful Present is marked truncated; trace +overflow, phase gaps and out-of-window evidence stay explicitly unmatched. +This is temporal association, not proof that every overlapping operation +caused a long frame. + +A separate 25 ms observer reads the device window's foreground/minimized state, +the Present heartbeat and the host's safe game-frame counter. It does not call +the device or hold its reference. Its bounded history remains useful when +Presents pause. Reports preserve raw observations, background exclusions and +candidate freezes; rendering work and pixel equality remain unknown. +Freeze candidates require at least 250 ms without game-frame progress and +distinguish a continuing Present heartbeat from a pause in Presents. +All instrumented phases retain their measured overhead, including the memory and +context collection during the baseline phase. Baseline still excludes CPU +thread suspension; it is not a completely uninstrumented run. + The candidate is validated with native D3D9/Vulkan fixtures, symbol fixtures, repeated serial measurement-overhead benchmarks, and the repository pipeline. Native HUD captures verify the renderer with fixture text. Game appearance, actual Grove Street frame times and indoor/outdoor behavior require a separate manual game run after deployment is authorized. +The measurement-coverage benchmark runner uses already-built binaries and +records their hashes before and after each independent process: + +```powershell +pwsh -NoProfile -File tests/measurement-coverage-benchmark.ps1 -Mode Backend ` + -Baseline -Candidate ` + -Probe -OutputDirectory +pwsh -NoProfile -File tests/measurement-coverage-benchmark.ps1 -Mode Runtime ` + -Candidate -Probe ` + -OutputDirectory +``` + +Backend mode compares inactive old/new DLLs, inactive/active collection on the +same candidate, and inactive/inactive A/A. Runtime mode compares the existing +main sampling/audit flow with the full new telemetry, CS and context coverage, +plus an A/A control. Timed phase rows and whole-capture sample/event counters +have separate scopes. Inspect each process and A/A dispersion before claiming +a measurable change; report rendering/symbol resolution is outside the timed +frame phases. Whole-capture work/Present means and maxima, the completion +callback, the observed CPU-finishing gap and post-completion report wait are +recorded separately; these overlapping wall-time measures are not additive. +Cycle normalization reads `sampledPresents` from the same source report, and +zero thread-CPU accounting values remain in the paired data. Run these serially +without builds or other GPU tests in parallel. + +For a version comparison of bounded event storage, build the same native fixture +source against old and new `BridgeMeasurementRuntime.cpp`, freeze both executables, +and use `tests/tail-mapping-benchmark.py --kind runtime` with the same backend DLL. +It runs fresh-process AB/BA and A/A, records file hashes and rejects failed work or +incomplete candidate captures. The isolated storage cases disable CPU sampling and +journal attribution intentionally; full F8 phase overhead is measured separately +with both versions' coverage enabled. Report completion time is separate from the +resource-work/Present/GPU-drain timing. Start-Present and completion-interrupt costs +are also recorded; `totalMainNs` and `totalMainThreadCycles` include both callbacks +and the resource-work window. The high-volume old control is expected to +truncate; its omissions are retained in the output rather than hidden. +The default native runtime test build includes a compile-only synchronization +seam to force the empty-queue/stop interleaving. Shipping DLLs do not compile it. +Freeze timing executables with `tests/measurement-runtime-test.ps1 +-StorageTestSync:$false` for both runtime versions, using matching fixture and +shared header inputs; do not benchmark the synchronized correctness build. + +Texture mapping policy comparisons use `tests/tail-mapping-benchmark.py --kind +mapping` with one frozen `mapping-budget-probe.exe` and two frozen backend DLLs. +The default six workloads cover below-budget reuse, pressured hot reuse, cold +sequential input, reuse beyond the budget, create-to-first-use, and unused new +resources competing with a hot set. Each uses the same explicit 100 MiB budget +and 512 operations, with six alternating A/B pairs and three A/A pairs. + +`totalNs` and `mainThreadCycles` include measured resource release and the final +GPU drain. `releaseNs` is a part of that total. Loop quantiles and +`operationMaxUnmapBurst` describe the individual update/draw/Present operations; +`maxUnmapBurst` includes final cleanup as well. `cleanupUnmapCalls` and +`cleanupUnmapNs` identify the final release/drain cost, so deferred cleanup cannot +be mistaken for a saving. Actual main-thread OS map/unmap calls are observed at +the backend's existing import boundary, without active telemetry in the timed pass. + +A separate untimed pass repeats the same resource lifecycle and GPU pixel check +to measure logical allocator residency through existing telemetry records. These +are logical source-allocation bytes, not total GPU memory or summed OS view sizes. +`mappedEndBytes` is before final release; `mappedAfterReleaseBytes` must be zero. +The timing pass pins only its main thread to the lowest permitted CPU, which is +reported in `affinityScope`; correctness tests use normal affinity. Run all timing +processes serially, and assess the measured A/A spread with each control workload. + StateRing diagnostics use a separate on-demand F10 capture. With `[ProperShadersStateRing] Enable=1`, normal play no longer takes full StateRing device snapshots. Press its configured key once to collect about one second of @@ -100,3 +270,37 @@ Admission stops at the deadline even if another Present is delayed. A guard record completed after that deadline is discarded; an empty dump can therefore mean that no complete guard record fit inside the window, rather than no state changes occurring. +# Mapping retirement and CS sequence correlation + +The optional telemetry stream also records `mapping-retire` (12), `cs-chunk` +(13), `command-allocate` (14), `command-begin` (15), and `command-reset` (16). +No interface layout or vtable changes. Command spans time the actual Vulkan +entry point, including its return status. CS chunks identify their queue +sequence; flag `0x800` means high priority, whose unsynchronized sequence can be +zero. Flag `0x1000` identifies secondary command buffers. CS chunk completion +includes reference cleanup after sequence publication; clip intervals to each +wait before attributing time. Filtered, unfinished or dropped chunks cannot be +inferred from a CPU sample. + +`mapping-retire` is enqueue-to-completion latency, not time blocked on the main +thread. Its `0x400` flag also marks logical ownership removal in `unmap` records; +the separate OS unmap span runs on the worker that actually makes the call. +Queued retirements own in-flight recorder tokens so the capture tail cannot +appear empty while cleanup is still queued. Pending mapped bytes remain charged +to the texture budget. Pending backing files are drained before committing a +replacement file. Live texture eviction, Vulkan staging and copies are unchanged. + +Final-release work uses one device-owned worker only when the mapped working set +is at least 8 MiB or a burst is already queued. The queue has at most 128 entries, +128 MiB of mapped views and 128 MiB of backing files; these are separate limits +on already-owned resources. Worker startup failure and oversized retirements +fall back to synchronous cleanup. Small create/release loops remain synchronous. + +Run `python tools/analyze-cs-waits.py REPORT --output RESULT.json` to join waits +to CS chunks and nested command-buffer spans. It uses clipped interval unions, +preserves unexplained time, and outputs the source hash and capture metadata. +The duration fields are wall time, not CPU cycle measurements or pure GPU time. +For mapping A/B, `releaseSubmitNs` measures the calling thread and `releaseNs` +waits for the last real backing-file close. This completion barrier and the GPU +drain remain inside `totalNs`; the small barrier resource is created after the +timed work loop, so it cannot change allocation reuse during that loop. diff --git a/src/bridge/legacy/BridgeD3D9.cpp b/src/bridge/legacy/BridgeD3D9.cpp index 5c148e5..ade14fd 100644 --- a/src/bridge/legacy/BridgeD3D9.cpp +++ b/src/bridge/legacy/BridgeD3D9.cpp @@ -5996,7 +5996,7 @@ static void STDMETHODCALLTYPE OnUnwrappedDeviceEvent(void* data, break; case D3D9BridgeEvent::PresentAfter: ++g_cpuHotspotPresents; - if (BridgeMeasurement::RuntimeEnabled()) BridgeMeasurement::RuntimePresent(device, result); + if (BridgeMeasurement::RuntimeEnabled()) BridgeMeasurement::RuntimePresent(device, result, present ? present->Window : nullptr); else PollProperShadersStateAttribution(g_unwrappedStateAttributionTriggerWasDown, true); for (auto& plugin : g_plugins) SafePluginCall("OnPresentAfter", plugin.onPresentAfter, device, result); @@ -7557,15 +7557,16 @@ static void LoadBridgeConfig() measurement.timing.attributionMs = g_properShadersStateAttributionDurationMs; measurement.timing.slowFrameMs = std::clamp(GetPrivateProfileIntA("Measurement", "SlowFrameMs", 50, iniPath), 1, 1000); measurement.intervalMs = g_cpuHotspotIntervalMs; + measurement.csIntervalMs = std::clamp(GetPrivateProfileIntA("Measurement", "CsIntervalMs", 10, iniPath), 1, 100); measurement.gameDirectory = g_gameDir; measurement.stopEvent = g_bridgeStopEvent; measurement.startWorker = &StartMeasurementWorker; measurement.log = +[](const char* message) { Log("measurement: {}", message); }; measurement.readFrame = &ReadGtasaFrameCounter; measurement.appendCpuReport = &AppendUnifiedCpuAdapterReport; BridgeMeasurement::ConfigureRuntime(measurement); - Log("measurement: enabled={} HUD={} F8/F7=combined baselineMs={} cpuMs={} stateMs={} samplingIntervalMs={}", + Log("measurement: enabled={} HUD={} F8/F7=combined baselineMs={} cpuMs={} stateMs={} samplingIntervalMs={} csIntervalMs={}", measurement.enabled, measurement.hud, measurement.timing.baselineMs, measurement.timing.cpuMs, - measurement.timing.attributionMs, measurement.intervalMs); + measurement.timing.attributionMs, measurement.intervalMs, measurement.csIntervalMs); int useDxvk = GetPrivateProfileIntA("Backend", "UseDxvkBackend", 0, iniPath); char backendRel[MAX_PATH]{}; @@ -8138,7 +8139,7 @@ class BridgeDirect3DDevice9 final : public IDirect3DDevice9 OnCallsitePresent(); OnProperShadersEffectProfilePresent(); if (BridgeMeasurement::RuntimeEnabled()) { - BridgeDiagnostics::BestEffort([&] { BridgeMeasurement::RuntimePresent(m_inner, hr); }); + BridgeDiagnostics::BestEffort([&] { BridgeMeasurement::RuntimePresent(m_inner, hr, hDestWindowOverride); }); } else PollProperShadersStateAttribution(m_stateAttributionTriggerWasDown, true); ResetTrackedState(); for (auto& plugin : g_plugins) { diff --git a/src/bridge/legacy/BridgeMeasurement.h b/src/bridge/legacy/BridgeMeasurement.h index 1b07a21..40c0902 100644 --- a/src/bridge/legacy/BridgeMeasurement.h +++ b/src/bridge/legacy/BridgeMeasurement.h @@ -33,9 +33,11 @@ struct FrameStats { std::uint64_t presents = 0, failedPresents = 0, invalidTimes = 0; std::uint64_t startNs = 0, endNs = 0, lastPresentNs = 0, lastObservedNs = 0; std::uint64_t totalIntervalNs = 0, maxIntervalNs = 0; - void Begin(std::uint64_t now) { + bool lastIsSuccessfulPresent = false; + void Begin(std::uint64_t now, bool startIsSuccessfulPresent = false) { *this = {}; startNs = endNs = lastPresentNs = lastObservedNs = now; + lastIsSuccessfulPresent = startIsSuccessfulPresent; } bool Observe(std::uint64_t now, bool success) { if (now < lastObservedNs) { ++invalidTimes; return false; } @@ -45,6 +47,7 @@ struct FrameStats { totalIntervalNs += interval; maxIntervalNs = std::max(maxIntervalNs, interval); lastPresentNs = now; + lastIsSuccessfulPresent = true; ++presents; return true; } @@ -57,15 +60,16 @@ struct FrameStats { struct FrameRecord { std::uint64_t beginNs = 0, endNs = 0, frameId = 0; Phase phase = Phase::Ready; + bool beginIsSuccessfulPresent = false; }; class FrameTrace { public: static constexpr std::size_t Capacity = 4096; void Clear() { m_count = m_dropped = 0; } - void Add(std::uint64_t begin, std::uint64_t end, std::uint64_t id, Phase phase) { + void Add(std::uint64_t begin, std::uint64_t end, std::uint64_t id, Phase phase, bool beginKnown = true) { if (end <= begin) return; if (m_count == Capacity) { ++m_dropped; return; } - m_rows[m_count++] = {begin, end, id, phase}; + m_rows[m_count++] = {begin, end, id, phase, beginKnown}; } const FrameRecord* Find(std::uint64_t time) const { const auto first = m_rows.begin(), last = first + m_count; @@ -79,6 +83,21 @@ class FrameTrace { const auto* row = Find(time); return row && row->endNs - row->beginNs > thresholdNs; } + // Intersect half-open span [begin,end) against each retained interval. + // The returned duration omits phase gaps, out-of-range and dropped frames. + template + std::uint64_t ForEachOverlap(std::uint64_t begin, std::uint64_t end, F callback) const { + if (end <= begin) return 0; + const auto first = m_rows.begin(), last = first + m_count; + auto it = std::lower_bound(first, last, begin, + [](const FrameRecord& row, std::uint64_t value) { return row.endNs <= value; }); + std::uint64_t covered = 0; + for (; it != last && it->beginNs < end; ++it) { + const auto a = (std::max)(begin, it->beginNs), b = (std::min)(end, it->endNs); + if (b > a) { callback(*it, a, b); covered += b - a; } + } + return covered; + } const FrameRecord* data() const { return m_rows.data(); } std::size_t size() const { return m_count; } std::uint64_t dropped() const { return m_dropped; } @@ -96,7 +115,7 @@ class Controller { explicit Controller(Options options = {}) : m_options(options) { } void Configure(Options options) { if (!active()) m_options = options; } void SetAvailable(bool cpu, bool attribution) { m_cpuAvailable = cpu; m_attributionAvailable = attribution; } - bool Key(bool f7, bool f8, std::uint64_t now) { + bool Key(bool f7, bool f8, std::uint64_t now, bool startIsSuccessfulPresent = false) { const bool down = f7 || f8; const bool edge = down && !m_keyDown; m_keyDown = down; @@ -108,7 +127,7 @@ class Controller { m_frame = 0; m_cpuDone = m_cpuFailed = false; m_unavailable = (!m_cpuAvailable ? 2u : 0u) | (!m_attributionAvailable ? 4u : 0u); - Enter(Phase::Baseline, now); + Enter(Phase::Baseline, now, startIsSuccessfulPresent); return true; } void SuppressKeys(bool f7, bool f8) { m_keyDown = f7 || f8; } @@ -120,23 +139,24 @@ class Controller { } if (m_phase == Phase::CpuFinishing) { if (!m_cpuDone) return Action::None; - return BeginAttribution(now); + return BeginAttribution(now, success); } const unsigned index = phaseIndex(); const auto before = m_stats[index].lastPresentNs; + const bool beforeKnown = m_stats[index].lastIsSuccessfulPresent; if (m_stats[index].Observe(now, success)) - m_trace.Add(before, now, ++m_frame, m_phase); + m_trace.Add(before, now, ++m_frame, m_phase, beforeKnown); if (m_stats[index].elapsedNs() < std::uint64_t(durationMs()) * 1'000'000ull) return Action::None; m_stats[index].Finish(now); if (m_phase == Phase::Baseline) { - if (!m_cpuAvailable) return BeginAttribution(now); - Enter(Phase::Cpu, now); + if (!m_cpuAvailable) return BeginAttribution(now, success); + Enter(Phase::Cpu, now, success); return Action::StartCpu; } if (m_phase == Phase::Cpu) { m_phase = Phase::CpuFinishing; - if (m_cpuDone) return BeginAttribution(now); + if (m_cpuDone) return BeginAttribution(now, success); return Action::None; } m_phase = Phase::Complete; @@ -163,13 +183,13 @@ class Controller { return m_phase == Phase::Baseline ? 0u : (m_phase == Phase::Attribution ? 2u : 1u); } private: - void Enter(Phase phase, std::uint64_t now) { + void Enter(Phase phase, std::uint64_t now, bool startIsSuccessfulPresent) { m_phase = phase; - m_stats[phaseIndex()].Begin(now); + m_stats[phaseIndex()].Begin(now, startIsSuccessfulPresent); } - Action BeginAttribution(std::uint64_t now) { + Action BeginAttribution(std::uint64_t now, bool startIsSuccessfulPresent) { if (!m_attributionAvailable) { m_phase = Phase::Complete; return Action::FinishAttribution; } - Enter(Phase::Attribution, now); + Enter(Phase::Attribution, now, startIsSuccessfulPresent); return Action::StartAttribution; } Options m_options; diff --git a/src/bridge/legacy/BridgeMeasurementRuntime.cpp b/src/bridge/legacy/BridgeMeasurementRuntime.cpp index 63f9990..b8466b3 100644 --- a/src/bridge/legacy/BridgeMeasurementRuntime.cpp +++ b/src/bridge/legacy/BridgeMeasurementRuntime.cpp @@ -3,6 +3,7 @@ #include "ProperShadersStateJournal.h" #include "../../../backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h" #include "../../../backend/dxvk/src/d3d9/d3d9_bridge_thread_stats.h" +#include "../../../backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h" #include #include @@ -13,11 +14,16 @@ #include #include #include +#include "MeasurementEventStorage.h" namespace BridgeMeasurement { namespace { +using detail::MeasurementEventStorage; constexpr std::size_t MaxSamples = 16384; constexpr std::size_t StackBytes = 4096; +constexpr std::size_t MaxTelemetryEvents = 65536, MaxMemorySnapshots = 128; +constexpr UINT MemoryChunkCapacity = 256, MemoryMaskCapacity = 4096; +constexpr std::uint64_t SnapshotCadenceNs = 500'000'000ull; struct Range { std::uintptr_t begin, end; }; struct RawSample { std::uint64_t timeNs = 0, suspendedNs = 0; @@ -33,20 +39,56 @@ struct CpuFunction { struct CpuResult { std::uint64_t id = 0, elapsedNs = 0, missed = 0, dropped = 0, suspendedNs = 0; std::uint64_t maxSuspendedNs = 0, cycles = 0, presents = 0, startedNs = 0; + std::uint64_t scheduleMissed = 0, maxScheduleLatenessNs = 0; std::uint64_t validChains = 0, partialChains = 0, invalidChains = 0; std::vector samples; std::vector modules; std::vector functions; std::unordered_map symbols; std::string error; + const char* csError = "not-initialized"; + DWORD thread = 0; + bool available = false; + std::unique_ptr cs; }; struct CpuJob { RuntimeConfig config; std::string directory; - DWORD thread = 0; + DWORD thread = 0, csThread = 0; std::uint64_t id = 0; std::atomic cancel{false}; }; +struct ContextPoint { + std::uint64_t timeNs = 0, presents = 0; + std::uint64_t frameUnchangedNs = 0; + std::uint32_t frame = 0; + bool windowKnown = false, foregroundKnown = false, foreground = false, minimized = false; + bool frameKnown = false, unchanged = false, presentGap = false; + bool presentsAdvanced = false; +}; +struct ContextJob { + RuntimeConfig config; + std::uint64_t id = 0; + std::atomic window{nullptr}; + std::atomic cancel{false}, done{false}, failed{false}; + std::mutex mutex; + std::vector points; + std::uint64_t dropped = 0; +}; +struct MemoryObservation { + D3D9BridgeMemorySnapshot snapshot{}; + std::vector chunks; + std::vector masks; + const char* boundary = "periodic"; +}; +struct TelemetryCapture { + bool available = false, active = false; + const char* error = ""; + std::uint64_t id = 0, lastSnapshotNs = 0, runtimeDropped = 0, invalidEvents = 0, snapshotSkipped = 0, snapshotFailures = 0; + D3D9BridgeTelemetryRead read{}; + std::shared_ptr storage; + std::vector memory; +}; struct ReportJob { RuntimeConfig config; std::string path; @@ -56,6 +98,8 @@ struct ReportJob { D3D9BridgeAuditSnapshot audit{}; std::array threadBegin{}, threadEnd{}; std::shared_ptr cpu; + std::shared_ptr context; + std::shared_ptr telemetry; Phase finalPhase = Phase::Complete; }; @@ -70,15 +114,20 @@ Controller g_controller; IDirect3DDevice9* g_device = nullptr; // borrowed; cleared before final release ID3D9BridgeDiagnostics* g_diagnostics = nullptr; // likewise never retains device ID3D9BridgeThreadStats* g_threadStats = nullptr; // borrowed, same device lifetime +ID3D9BridgeTelemetry* g_telemetry = nullptr; // borrowed only under device callback/lifecycle +HWND g_deviceWindow = nullptr; std::array g_threadBegin{}, g_threadEnd{}; int g_threadPhase = -1; DWORD g_owner = 0; std::shared_ptr g_cpuJob; std::shared_ptr g_cpuResult; +std::shared_ptr g_context; +std::shared_ptr g_telemetryCapture; bool g_cpuReady = false, g_journalStarted = false, g_auditStarted = false; bool g_reportFailed = false, g_reportStarted = false, g_reportInterrupted = false; bool g_journalDrainPending = false; std::uint64_t g_nextCapture = 0, g_capture = 0, g_lastHudNs = 0; +std::uint64_t g_auditCapture = 0; std::uint32_t g_unavailable = 0; FrameStats g_live; bool g_liveStarted = false; @@ -95,6 +144,52 @@ std::uint64_t NowNs() { const auto t = std::uint64_t(ticks.QuadPart), f = std::uint64_t(frequency); return (t / f) * 1'000'000'000ull + ((t % f) * 1'000'000'000ull) / f; } +DWORD WINAPI ContextWorker(void* opaque) { + WorkerExit activity; + std::unique_ptr> holder(static_cast*>(opaque)); + const auto job = *holder; + struct Done { std::shared_ptr job; ~Done() { job->done.store(true, std::memory_order_release); } } done{job}; + try { + std::uint32_t lastFrame = 0; + bool haveFrame = false; + auto lastPresents = g_successfulPresents.load(std::memory_order_relaxed); + auto lastPresentNs = NowNs(); + auto frameChangedNs = lastPresentNs, presentsAtFrameChange = lastPresents; + while (!job->cancel.load(std::memory_order_relaxed) && + !(job->config.stopEvent && WaitForSingleObject(job->config.stopEvent, 0) == WAIT_OBJECT_0)) { + ContextPoint point; + point.timeNs = NowNs(); point.presents = g_successfulPresents.load(std::memory_order_relaxed); + if (point.presents != lastPresents) { lastPresents = point.presents; lastPresentNs = point.timeNs; } + point.presentGap = point.timeNs >= lastPresentNs && point.timeNs - lastPresentNs >= 250'000'000ull; + const auto window = job->window.load(std::memory_order_relaxed); + DWORD pid = 0; + if (window && IsWindow(window) && GetWindowThreadProcessId(window, &pid) && pid == GetCurrentProcessId()) { + point.windowKnown = true; + point.minimized = IsIconic(window) != FALSE; + const HWND foreground = GetForegroundWindow(); + point.foregroundKnown = foreground != nullptr; + point.foreground = foreground && GetAncestor(foreground, GA_ROOTOWNER) == GetAncestor(window, GA_ROOTOWNER); + } + if (job->config.readFrame) point.frameKnown = job->config.readFrame(point.frame); + point.unchanged = point.frameKnown && haveFrame && point.frame == lastFrame; + if (point.frameKnown) { + if (!point.unchanged) { frameChangedNs = point.timeNs; presentsAtFrameChange = point.presents; } + point.frameUnchangedNs = point.timeNs >= frameChangedNs ? point.timeNs - frameChangedNs : 0; + point.presentsAdvanced = point.presents > presentsAtFrameChange; + lastFrame = point.frame; haveFrame = true; + } else haveFrame = false; + { + std::lock_guard lock(job->mutex); + if (job->points.size() < 4096) job->points.push_back(point); + else ++job->dropped; + } + if (job->config.stopEvent) { + if (WaitForSingleObject(job->config.stopEvent, 25) != WAIT_TIMEOUT) break; + } else Sleep(25); + } + } catch (...) { job->failed.store(true, std::memory_order_release); } + return 0; +} void CopyText(char* out, std::size_t size, const std::string& text) { if (!size) return; const auto count = (std::min)(size - 1, text.size()); @@ -193,6 +288,130 @@ bool Stopping(const CpuJob& job) { return job.cancel.load(std::memory_order_relaxed) || (job.config.stopEvent && WaitForSingleObject(job.config.stopEvent, 0) == WAIT_OBJECT_0); } +void DrainTelemetryLocked(bool finalDrain) { + if (!g_telemetryCapture || !g_telemetryCapture->available || !g_telemetry) return; + auto& capture = *g_telemetryCapture; + try { + std::array buffer{}; + for (unsigned batch = 0; batch < (finalDrain ? 257u : 4u); ++batch) { + D3D9BridgeTelemetryRead read{}; + read.StructSize = sizeof(read); read.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + if (FAILED(g_telemetry->ReadEvents(capture.id, &read, buffer.data(), UINT(buffer.size()))) || + read.CaptureId != capture.id || read.Written > buffer.size()) { + capture.error = "read-events-failed"; g_unavailable |= RuntimeTelemetryUnavailable; break; + } + capture.read = read; + for (UINT i = 0; i < read.Written; ++i) { + if (buffer[i].CaptureId != capture.id) { ++capture.invalidEvents; g_unavailable |= RuntimeTelemetryUnavailable; } + else if (capture.storage) capture.storage->Push(buffer[i]); + else ++capture.runtimeDropped; + } + if (!read.Remaining || !read.Written) break; + } + } catch (...) { capture.error = "read-events-exception"; g_unavailable |= RuntimeTelemetryUnavailable; } +} +void SnapshotMemoryLocked(const char* boundary, std::uint64_t now) { + if (!g_telemetryCapture || !g_telemetryCapture->active || !g_telemetry) return; + auto& capture = *g_telemetryCapture; + capture.lastSnapshotNs = now; + if (capture.memory.size() >= MaxMemorySnapshots) { ++capture.snapshotSkipped; return; } + try { + MemoryObservation observation; + observation.boundary = boundary; + observation.chunks.resize(MemoryChunkCapacity); + observation.masks.resize(MemoryMaskCapacity); + observation.snapshot.StructSize = sizeof(observation.snapshot); + observation.snapshot.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + if (FAILED(g_telemetry->GetMemorySnapshot(capture.id, &observation.snapshot, + observation.chunks.data(), UINT(observation.chunks.size()), observation.masks.data(), UINT(observation.masks.size()))) || + observation.snapshot.CaptureId != capture.id || observation.snapshot.CopiedChunks > observation.chunks.size() || + observation.snapshot.CopiedMasks > observation.masks.size()) { + ++capture.snapshotFailures; g_unavailable |= RuntimeTelemetryUnavailable; return; + } + observation.chunks.resize(observation.snapshot.CopiedChunks); + observation.masks.resize(observation.snapshot.CopiedMasks); + capture.memory.push_back(std::move(observation)); + } catch (...) { ++capture.snapshotFailures; g_unavailable |= RuntimeTelemetryUnavailable; } +} +template +bool Launch(const RuntimeConfig&, LPTHREAD_START_ROUTINE, const std::shared_ptr&); +DWORD WINAPI EventStorageWorker(void* opaque) { + WorkerExit activity; + std::unique_ptr> holder(static_cast*>(opaque)); + (*holder)->Run(); + return 0; +} +void BeginTelemetryLocked(std::uint64_t now) { + // Reserve bounded storage before StartCapture: allocation failure can never + // leave an active backend recorder with no owning runtime capture. + g_telemetryCapture.reset(); + try { + g_telemetryCapture = std::make_shared(); + auto& capture = *g_telemetryCapture; + capture.id = g_capture; + if (!g_config.telemetry) { capture.error = "disabled"; return; } + if (!g_telemetry) { capture.error = "interface-unavailable"; g_unavailable |= RuntimeTelemetryUnavailable; return; } + capture.storage = std::make_shared(); + capture.storage->stop = g_config.stopEvent; + capture.memory.reserve(MaxMemorySnapshots); + } catch (...) { + g_unavailable |= RuntimeTelemetryUnavailable; + if (g_telemetryCapture) g_telemetryCapture->error = "storage-allocation-failed"; + CopyText(g_error, sizeof(g_error), "cannot allocate event storage"); + return; + } + auto& capture = *g_telemetryCapture; + D3D9BridgeTelemetryConfig config{}; + config.StructSize = sizeof(config); config.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + config.CaptureId = g_capture; config.MaxEvents = UINT(MaxTelemetryEvents); + config.MinDurationNs = 50'000; config.MinBytes = 65'536; + try { + if (FAILED(g_telemetry->StartCapture(&config))) { + capture.error = "start-failed"; g_unavailable |= RuntimeTelemetryUnavailable; return; + } + capture.available = capture.active = true; + capture.storage->ready.store(false, std::memory_order_release); + if (!Launch(g_config, EventStorageWorker, capture.storage)) { + capture.storage->errors.fetch_or(MeasurementEventStorage::Worker); + capture.storage->ready = true; + capture.storage->abort = true; + capture.error = "storage-worker-failed"; g_unavailable |= RuntimeTelemetryUnavailable; + } + SnapshotMemoryLocked("begin", now); + } catch (...) { + capture.error = "start-exception"; g_unavailable |= RuntimeTelemetryUnavailable; + try { g_telemetry->StopCapture(capture.id); } catch (...) { } + capture.active = false; + } +} +void PollTelemetryLocked(std::uint64_t now) { + if (!g_controller.active() || !g_telemetryCapture || !g_telemetryCapture->active) return; + DrainTelemetryLocked(false); + const auto last = g_telemetryCapture->lastSnapshotNs; + if (now >= last && now - last >= SnapshotCadenceNs) { + SnapshotMemoryLocked("periodic", now); + DrainTelemetryLocked(false); + } +} +void EndTelemetryLocked(std::uint64_t now, const char* boundary) { + if (!g_telemetryCapture || !g_telemetryCapture->active || !g_telemetry) return; + // Snapshot errors are contained so StopCapture is attempted on every exit. + SnapshotMemoryLocked(boundary, now); + bool stopped = false; + try { stopped = SUCCEEDED(g_telemetry->StopCapture(g_telemetryCapture->id)); } catch (...) { } + if (!stopped) { + g_telemetryCapture->error = "stop-failed"; g_unavailable |= RuntimeTelemetryUnavailable; + } + g_telemetryCapture->active = !stopped; + DrainTelemetryLocked(true); + const auto& read = g_telemetryCapture->read; + // An in-flight span during capture is normal. After stop, however, its + // missing completion and any backend loss make telemetry evidence partial. + if (read.Dropped || read.LateDropped || read.InFlight || read.InFlightAtStop || read.Remaining || + g_telemetryCapture->runtimeDropped) + g_unavailable |= RuntimeTelemetryUnavailable; + if (g_telemetryCapture->storage) g_telemetryCapture->storage->sealed.store(true, std::memory_order_release); +} void ResolveCpu(CpuResult& result) { MeasurementSymbolResolver resolver; for (const auto& module : result.modules) { @@ -296,28 +515,50 @@ DWORD WINAPI CpuWorker(void* opaque) { std::unique_ptr> holder(static_cast*>(opaque)); const auto job = *holder; std::shared_ptr result; - HANDLE thread = nullptr; + HANDLE mainThread = nullptr, csThread = nullptr; try { - result = std::make_shared(); result->id = job->id; + result = std::make_shared(); result->id = job->id; result->thread = job->thread; result->samples.reserve(MaxSamples); + if (!job->config.csCpu) result->csError = "disabled"; + else if (!job->csThread || job->csThread == job->thread || job->csThread == GetCurrentThreadId()) + result->csError = "missing-or-invalid-cs-thread"; + else try { + result->cs = std::make_unique(); + result->cs->id = job->id; result->cs->thread = job->csThread; + result->cs->samples.reserve(MaxSamples); + result->csError = ""; + } catch (...) { result->cs.reset(); result->csError = "cs-allocation-failed"; } std::vector ranges; CollectModules(result->modules, ranges); - thread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, job->thread); - if (!thread || GetProcessIdOfThread(thread) != GetCurrentProcessId()) throw std::runtime_error("cannot open capture owner thread"); - ULONG64 startCycles = 0, endCycles = 0; - QueryThreadCycleTime(thread, &startCycles); + if (result->cs) try { result->cs->modules = result->modules; } + catch (...) { result->cs.reset(); result->csError = "cs-allocation-failed"; } + auto* cs = result->cs.get(); + mainThread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, job->thread); + if (!mainThread || GetProcessIdOfThread(mainThread) != GetCurrentProcessId() || job->thread == GetCurrentThreadId()) + throw std::runtime_error("cannot open capture owner thread"); + result->available = true; + if (cs) { + csThread = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, job->csThread); + if (!csThread || GetProcessIdOfThread(csThread) != GetCurrentProcessId()) { + if (csThread) CloseHandle(csThread); + csThread = nullptr; result->csError = "cannot-open-cs-thread"; + } else cs->available = true; + } + ULONG64 mainBegin = 0, mainEnd = 0, csBegin = 0, csEnd = 0; + QueryThreadCycleTime(mainThread, &mainBegin); + if (csThread) QueryThreadCycleTime(csThread, &csBegin); const auto startPresents = g_successfulPresents.load(std::memory_order_relaxed); + // Initialize the QPC frequency and all storage before either target is suspended. result->startedNs = NowNs(); - Jitter jitter(DWORD(job->id) ^ job->thread); + if (cs) cs->startedNs = result->startedNs; + Jitter mainJitter(DWORD(job->id) ^ job->thread); + Jitter csJitter(DWORD(job->id) ^ job->csThread ^ 0x85ebca6bu); std::array stack{}; - while (!Stopping(*job) && NowNs() - result->startedNs < std::uint64_t(job->config.timing.cpuMs) * 1'000'000ull) { - const DWORD delay = jitter.Next(job->config.intervalMs); - if (job->config.stopEvent) { - if (WaitForSingleObject(job->config.stopEvent, delay) != WAIT_TIMEOUT) break; - } else Sleep(delay); - if (Stopping(*job)) break; + auto nextMain = result->startedNs + std::uint64_t(mainJitter.Next(job->config.intervalMs)) * 1'000'000ull; + auto nextCs = result->startedNs + std::uint64_t(csJitter.Next(job->config.csIntervalMs)) * 1'000'000ull; + auto sampleThread = [&](HANDLE thread, CpuResult& target) { const auto pauseStart = NowNs(); - if (SuspendThread(thread) == DWORD(-1)) { ++result->missed; continue; } + if (SuspendThread(thread) == DWORD(-1)) { ++target.missed; return; } CONTEXT context{}; context.ContextFlags = CONTEXT_CONTROL; const bool got = GetThreadContext(thread, &context) != FALSE; RawSample sample; sample.timeNs = NowNs(); @@ -331,11 +572,11 @@ DWORD WINAPI CpuWorker(void* opaque) { const auto resumed = ResumeThread(thread); const auto pauseEnd = NowNs(); sample.suspendedNs = pauseEnd >= pauseStart ? pauseEnd - pauseStart : 0; - result->suspendedNs += sample.suspendedNs; - result->maxSuspendedNs = (std::max)(result->maxSuspendedNs, sample.suspendedNs); - if (!got || !sample.ip || resumed == DWORD(-1)) { ++result->missed; continue; } - // All parsing and aggregation happens after ResumeThread. The structural - // chain has its own quality; candidate executable words stay heuristic. + target.suspendedNs += sample.suspendedNs; + target.maxSuspendedNs = (std::max)(target.maxSuspendedNs, sample.suspendedNs); + if (!got || !sample.ip || resumed == DWORD(-1)) { ++target.missed; return; } + // The only suspension region is above. No parsing, allocation, logging, + // device call or runtime lock occurs until the target has been resumed. sample.chain = WalkCapturedFrames(sp, stack.data(), copied, bp, [&](std::uintptr_t address) { return Executable(ranges, address); }); unsigned candidates = 0; @@ -343,30 +584,68 @@ DWORD WINAPI CpuWorker(void* opaque) { const auto address = std::uintptr_t(stack[i]); if (address != sample.ip && Executable(ranges, address)) sample.candidates[candidates++] = address; } - if (sample.chain.quality == ChainQuality::FrameChain) ++result->validChains; - else if (sample.chain.quality == ChainQuality::Partial || sample.chain.quality == ChainQuality::Truncated) ++result->partialChains; - else ++result->invalidChains; - if (result->samples.size() < MaxSamples) result->samples.push_back(sample); - else ++result->dropped; + if (sample.chain.quality == ChainQuality::FrameChain) ++target.validChains; + else if (sample.chain.quality == ChainQuality::Partial || sample.chain.quality == ChainQuality::Truncated) ++target.partialChains; + else ++target.invalidChains; + if (target.samples.size() < MaxSamples) target.samples.push_back(sample); + else ++target.dropped; + }; + const auto deadline = result->startedNs + std::uint64_t(job->config.timing.cpuMs) * 1'000'000ull; + while (!Stopping(*job)) { + const auto now = NowNs(); + if (now >= deadline) break; + if (now >= nextMain) { + const auto late = now - nextMain; + result->maxScheduleLatenessNs = (std::max)(result->maxScheduleLatenessNs, late); + result->scheduleMissed += late / (std::uint64_t((std::max)(1u, job->config.intervalMs)) * 1'000'000ull); + sampleThread(mainThread, *result); + nextMain = NowNs() + std::uint64_t(mainJitter.Next(job->config.intervalMs)) * 1'000'000ull; + } + if (csThread && !Stopping(*job) && NowNs() >= nextCs) { + const auto late = NowNs() - nextCs; + cs->maxScheduleLatenessNs = (std::max)(cs->maxScheduleLatenessNs, late); + cs->scheduleMissed += late / (std::uint64_t((std::max)(1u, job->config.csIntervalMs)) * 1'000'000ull); + sampleThread(csThread, *cs); + nextCs = NowNs() + std::uint64_t(csJitter.Next(job->config.csIntervalMs)) * 1'000'000ull; + } + const auto after = NowNs(); + const auto due = csThread ? (std::min)(nextMain, nextCs) : nextMain; + const DWORD delay = due > after ? DWORD((std::min)(10, (due - after + 999'999ull) / 1'000'000ull)) : 0; + if (job->config.stopEvent) { + if (WaitForSingleObject(job->config.stopEvent, delay) != WAIT_TIMEOUT) break; + } else Sleep(delay); } result->elapsedNs = NowNs() - result->startedNs; + if (cs) cs->elapsedNs = result->elapsedNs; result->presents = g_successfulPresents.load(std::memory_order_relaxed) - startPresents; - QueryThreadCycleTime(thread, &endCycles); - result->cycles = endCycles >= startCycles ? endCycles - startCycles : 0; - CloseHandle(thread); thread = nullptr; + if (cs) cs->presents = result->presents; + QueryThreadCycleTime(mainThread, &mainEnd); + result->cycles = mainEnd >= mainBegin ? mainEnd - mainBegin : 0; + if (csThread) { + QueryThreadCycleTime(csThread, &csEnd); + cs->cycles = csEnd >= csBegin ? csEnd - csBegin : 0; + } + CloseHandle(mainThread); mainThread = nullptr; + if (csThread) { CloseHandle(csThread); csThread = nullptr; } if (Stopping(*job)) throw std::runtime_error("CPU capture interrupted"); if (result->samples.empty()) throw std::runtime_error("no valid CPU samples"); + if (cs && cs->available && cs->samples.empty()) { cs->available = false; result->csError = "no-valid-cs-samples"; } ResolveCpu(*result); if (Stopping(*job)) throw std::runtime_error("CPU capture interrupted"); WriteCpuLog(*job, *result); + if (Stopping(*job)) throw std::runtime_error("CPU capture interrupted"); + if (cs && cs->available) try { ResolveCpu(*cs); } + catch (...) { cs->available = false; result->csError = "cs-symbol-resolution-failed"; } } catch (const std::exception& e) { if (result) result->error = e.what(); } catch (...) { if (result) result->error = "CPU capture failed"; } - if (thread) CloseHandle(thread); + if (mainThread) CloseHandle(mainThread); + if (csThread) CloseHandle(csThread); { std::lock_guard lock(g_mutex); if (g_cpuJob == job && g_capture == job->id) { g_cpuResult = result; g_cpuReady = true; + if (job->config.csCpu && (!result || !result->cs || !result->cs->available)) g_unavailable |= RuntimeCsCpuUnavailable; if (!result) CopyText(g_error, sizeof(g_error), "CPU result allocation failed"); else if (!result->error.empty() && g_controller.active()) CopyText(g_error, sizeof(g_error), result->error); } @@ -381,13 +660,43 @@ const char* AuditName(unsigned i) { "stateBlockBeginRecord", "stateBlockEndRecord", "stateBlockCapture", "stateBlockApply"}; return i < std::size(names) ? names[i] : "unknown"; } +const char* TelemetryKindName(UINT kind) { + switch (kind) { + case D3D9_BRIDGE_TELEMETRY_ALLOC: return "alloc"; + case D3D9_BRIDGE_TELEMETRY_FREE: return "free"; + case D3D9_BRIDGE_TELEMETRY_MAP: return "map"; + case D3D9_BRIDGE_TELEMETRY_UNMAP: return "unmap"; + case D3D9_BRIDGE_TELEMETRY_CS_WAIT: return "cs-wait"; + case D3D9_BRIDGE_TELEMETRY_RESOURCE_WAIT: return "resource-wait"; + case D3D9_BRIDGE_TELEMETRY_GPU_WAIT: return "gpu-wait"; + case D3D9_BRIDGE_TELEMETRY_SUBMISSION_WAIT: return "submission-wait"; + case D3D9_BRIDGE_TELEMETRY_FRAME_LATENCY_WAIT: return "frame-latency-wait"; + case D3D9_BRIDGE_TELEMETRY_PRESENT_ACQUIRE: return "present-acquire"; + case D3D9_BRIDGE_TELEMETRY_SAMPLER_POOL_WAIT: return "sampler-pool-wait"; + case D3D9_BRIDGE_TELEMETRY_MAPPING_RETIRE: return "mapping-retire"; + case D3D9_BRIDGE_TELEMETRY_CS_CHUNK: return "cs-chunk"; + case D3D9_BRIDGE_TELEMETRY_COMMAND_ALLOCATE: return "command-allocate"; + case D3D9_BRIDGE_TELEMETRY_COMMAND_BEGIN: return "command-begin"; + case D3D9_BRIDGE_TELEMETRY_COMMAND_RESET: return "command-reset"; + default: return "unknown"; + } +} void FillSnapshotLocked(RuntimeSnapshot& snapshot) { snapshot = {}; snapshot.phase = g_reportFailed ? Phase::Error : g_reportInterrupted ? Phase::Interrupted : g_controller.phase(); if (g_journalDrainPending && snapshot.phase == Phase::Complete) snapshot.phase = Phase::AttributionFinishing; snapshot.captureId = g_capture; snapshot.unavailableMask = g_unavailable | g_controller.unavailableMask(); - for (unsigned i = 0; i < 3; ++i) snapshot.phaseStats[i] = g_controller.stats(i); + for (unsigned i = 0; i < 3; ++i) { + snapshot.phaseStats[i] = g_controller.stats(i); + const auto& a = g_threadBegin[i]; const auto& b = g_threadEnd[i]; + if (a.AvailableMask == 7 && b.AvailableMask == 7 && a.MainThreadId == b.MainThreadId && + a.CsThreadId == b.CsThreadId && b.MainCpu100ns >= a.MainCpu100ns && b.CsCpu100ns >= a.CsCpu100ns) { + snapshot.threadActivityMask |= 1u << i; + snapshot.phaseMainCpuNs[i] = (b.MainCpu100ns - a.MainCpu100ns) * 100; + snapshot.phaseCsCpuNs[i] = (b.CsCpu100ns - a.CsCpu100ns) * 100; + } + } snapshot.frameTraceDropped = g_controller.trace().dropped(); snapshot.workerBusy = g_workers.load(std::memory_order_acquire) != 0 || g_journalDrainPending; snapshot.hudRequested = g_config.hud; @@ -395,6 +704,27 @@ void FillSnapshotLocked(RuntimeSnapshot& snapshot) { if (g_cpuResult) { snapshot.cpuSamples = g_cpuResult->samples.size(); snapshot.cpuMissed = g_cpuResult->missed; snapshot.cpuDropped = g_cpuResult->dropped; snapshot.suspendedNs = g_cpuResult->suspendedNs; + snapshot.mainThreadCycles = g_cpuResult->cycles; + if (g_cpuResult->cs) { + snapshot.csSamples = g_cpuResult->cs->samples.size(); snapshot.csMissed = g_cpuResult->cs->missed; + snapshot.csDropped = g_cpuResult->cs->dropped; snapshot.csSuspendedNs = g_cpuResult->cs->suspendedNs; + snapshot.csThreadCycles = g_cpuResult->cs->cycles; + } + } + if (g_telemetryCapture) { + snapshot.telemetryFiltered = g_telemetryCapture->read.Filtered; + snapshot.telemetryDropped = g_telemetryCapture->read.Dropped + g_telemetryCapture->runtimeDropped; + if (g_telemetryCapture->storage) { + const auto& storage = *g_telemetryCapture->storage; + snapshot.telemetryReportFinished = storage.reportFinished.load(std::memory_order_acquire); + snapshot.telemetryStorageErrors = storage.errors.load(); + const auto written = storage.written.load(); + snapshot.telemetryEvents = snapshot.telemetryReportFinished ? storage.finalizedReported.load() : written; + snapshot.telemetryUnread = snapshot.telemetryReportFinished ? written - snapshot.telemetryEvents : 0; + snapshot.telemetryDropped += storage.omitted.load() + snapshot.telemetryUnread; + } + if (g_telemetryCapture->storage && g_telemetryCapture->storage->errors.load()) snapshot.unavailableMask |= RuntimeTelemetryUnavailable; + snapshot.memorySnapshots = g_telemetryCapture->memory.size(); } CopyText(snapshot.reportPath, sizeof(snapshot.reportPath), g_reportPath); CopyText(snapshot.lastError, sizeof(snapshot.lastError), g_error); @@ -416,40 +746,58 @@ void SyncThreadPhaseLocked() { if (next >= 0) g_threadBegin[unsigned(next)] = sample; g_threadPhase = next; } +void StopAuditSafelyLocked() { + if (!g_diagnostics || !g_auditStarted) return; + bool stopped = false; + // A thrown COM call may have stopped the audit before throwing. One retry + // permits an idempotent S_FALSE result and never blocks recorder cleanup. + for (unsigned attempt = 0; attempt < 2 && !stopped; ++attempt) { + try { stopped = SUCCEEDED(g_diagnostics->StopAudit(g_auditCapture)); } + catch (...) { g_unavailable |= RuntimeBackendUnavailable; } + } + if (!stopped) g_unavailable |= RuntimeBackendUnavailable; + else { g_auditStarted = false; g_auditCapture = 0; } + try { ReadAuditLocked(); } catch (...) { g_unavailable |= RuntimeBackendUnavailable; } +} void StopAttributionLocked() { if (g_journalStarted) { // Present/Reset can already hold the device lock or be inside an admitted // transaction. Never drain it synchronously from that calling thread. - JournalAttributionCloseAdmission(); - JournalAttributionGetSummary(&g_journal); - g_journalDrainPending = g_journal.inFlight != 0; + try { + JournalAttributionCloseAdmission(); + if (!JournalAttributionGetSummary(&g_journal)) { + g_unavailable |= RuntimeJournalUnavailable; g_journalDrainPending = true; + } else g_journalDrainPending = g_journal.inFlight != 0; + } catch (...) { + g_unavailable |= RuntimeJournalUnavailable; g_journalDrainPending = true; + } g_journalStarted = false; if (!g_journalDrainPending && !g_journal.available) g_unavailable |= RuntimeJournalUnavailable; } - if (g_diagnostics && g_auditStarted) { - g_diagnostics->StopAudit(g_capture); - ReadAuditLocked(); - g_auditStarted = false; - } + StopAuditSafelyLocked(); } -void WriteCombinedReport(const ReportJob& job) { +void WriteCombinedReport(ReportJob& job) { std::filesystem::create_directories(std::filesystem::path(job.path).parent_path()); FILE* file = nullptr; if (fopen_s(&file, job.path.c_str(), "w") || !file) throw std::runtime_error("cannot write combined report"); struct FileClose { FILE* file; ~FileClose() { std::fclose(file); } } close{file}; const auto& s = job.snapshot; - const char* status = s.phase == Phase::Complete ? (s.unavailableMask ? "complete-partial" : "complete") : - s.phase == Phase::Interrupted ? "interrupted" : "error"; - std::fprintf(file, "# measurement captureId=%llu status=%s pid=%lu clock=qpc-nanoseconds unavailableMask=%u\n", - s.captureId, status, GetCurrentProcessId(), s.unavailableMask); + const auto writeHeader = [&](const char* status) { + // Fixed-width fields permit finalizing storage read errors after bounded + // report iteration, without buffering the report or claiming success early. + std::fprintf(file, "# measurement captureId=%llu status=%-16s pid=%lu clock=qpc-nanoseconds unavailableMask=%010u\n", + s.captureId, status, GetCurrentProcessId(), s.unavailableMask); + }; + writeHeader("pending"); std::fprintf(file, "hud requested=%u integrationAvailable=%u baselineKeepsSameHudConfiguration=1\n", unsigned(s.hudRequested), unsigned(s.hudIntegrationAvailable)); std::fprintf(file, "limitations=sampling-is-statistical;frame-chain-is-structural;candidate-callers-are-heuristic;inclusive-timings-are-not-additive\n"); const char* phases[] = {"baseline", "cpu", "attribution"}; for (unsigned i = 0; i < 3; ++i) { const auto& stats = s.phaseStats[i]; - std::fprintf(file, "phase=%s observer=%s elapsedMs=%.6f successfulPresents=%llu failedPresents=%llu fps=%.6f averageIntervalMs=%.6f maxIntervalMs=%.6f invalidTimes=%llu\n", - phases[i], i == 0 ? "baseline" : "instrumented", double(stats.elapsedNs()) / 1e6, - stats.presents, stats.failedPresents, stats.fps(), stats.averageIntervalNs() / 1e6, + std::fprintf(file, "phase=%s observer=%s coverageActive=%u contextPolling=%u cpuSuspendSampling=%u elapsedMs=%.6f successfulPresents=%llu failedPresents=%llu fps=%.6f averageIntervalMs=%.6f maxIntervalMs=%.6f invalidTimes=%llu\n", + phases[i], i == 0 ? "baseline" : "instrumented", + unsigned(job.telemetry && job.telemetry->available), unsigned(job.context != nullptr), unsigned(i == 1 && job.config.cpu), + double(stats.elapsedNs()) / 1e6, stats.presents, stats.failedPresents, stats.fps(), stats.averageIntervalNs() / 1e6, double(stats.maxIntervalNs) / 1e6, stats.invalidTimes); const auto& a = job.threadBegin[i]; const auto& b = job.threadEnd[i]; const bool available = a.AvailableMask == 7 && b.AvailableMask == 7 @@ -517,12 +865,134 @@ void WriteCombinedReport(const ReportJob& job) { std::fprintf(file, "frameTrace count=%zu dropped=%llu slowThresholdMs=%u\n", job.trace.size(), job.trace.dropped(), job.config.timing.slowFrameMs); for (std::size_t i = 0; i < job.trace.size(); ++i) { const auto& row = job.trace.data()[i]; - std::fprintf(file, "frame id=%llu phase=%u beginNs=%llu endNs=%llu intervalMs=%.6f slow=%u\n", + std::fprintf(file, "frame id=%llu phase=%u beginNs=%llu endNs=%llu intervalMs=%.6f slow=%u beginSuccessfulPresent=%u boundaryTruncated=%u\n", row.frameId, unsigned(row.phase), row.beginNs, row.endNs, double(row.endNs - row.beginNs) / 1e6, - unsigned(row.endNs - row.beginNs > std::uint64_t(job.config.timing.slowFrameMs) * 1'000'000ull)); + unsigned(row.endNs - row.beginNs > std::uint64_t(job.config.timing.slowFrameMs) * 1'000'000ull), + unsigned(row.beginIsSuccessfulPresent), unsigned(!row.beginIsSuccessfulPresent)); } + if (job.telemetry) { + const auto& telemetry = *job.telemetry; + const auto& read = telemetry.read; + std::fprintf(file, "telemetry available=%u active=%u captureId=%llu source=completed-spans minDurationNs=50000 minBytes=65536 backendCapacity=%u hostCapacity=%zu recorded=%zu backendDropped=%llu hostDropped=%llu invalidCaptureEvents=%llu filtered=%llu started=%llu inFlight=%llu inFlightAtStop=%llu lateDroppedAtRead=%llu remaining=%u error=%s\n", + unsigned(telemetry.available), unsigned(telemetry.active), telemetry.id, read.Capacity, MeasurementEventStorage::Capacity, + size_t(telemetry.storage ? telemetry.storage->written.load() : 0), read.Dropped, (telemetry.runtimeDropped + (telemetry.storage ? telemetry.storage->omitted.load() : 0)), telemetry.invalidEvents, read.Filtered, read.Started, read.InFlight, + read.InFlightAtStop, read.LateDropped, read.Remaining, Quote(telemetry.error).c_str()); + std::fprintf(file, "telemetrySemantics=completed-only;inclusive-spans-may-nest;not-additive;open-at-stop-is-not-zero;overlaps-half-open;trace-gaps-and-dropped-frames-unmatched\n"); + std::fprintf(file, "presentWaitSemantics=frame-latency-signal-was-incomplete-before-wait-may-race;present-acquire-is-inclusive-CPU-driver-and-possible-waits;sampler-pool-wait-is-submit-fence-pressure;none-is-pure-GPU-execution\n"); + std::fprintf(file, "csWorkSemantics=cs-chunk-identity-is-queue-sequence;zero-is-unsynchronized-high-priority;chunk-includes-reference-release-after-sequence-publication;driver-spans-are-inclusive-wall-time;join-by-capture-thread-queue-sequence-and-overlap;filtered-or-open-spans-remain-unobserved\n"); + std::fprintf(file, "retirementSemantics=mapping-retire-is-enqueue-to-completion-latency-not-main-thread-blocked-time;deferred-logical-unmap-releases-owner;os-unmap-on-worker-records-actual-call;pending-bytes-still-count-against-mapped-budget;deferredFlag=0x%X highPriorityFlag=0x%X secondaryFlag=0x%X\n", + D3D9_BRIDGE_TELEMETRY_DEFERRED,D3D9_BRIDGE_TELEMETRY_HIGH_PRIORITY,D3D9_BRIDGE_TELEMETRY_SECONDARY); + std::fprintf(file, "telemetryFlags fileBacked=0x%X failed=0x%X allocatorOperation=0x%X deviceMemory=0x%X osMapping=0x%X deviceIdle=0x%X cacheHit=0x%X cacheReturn=0x%X cacheFill=0x%X cacheEvict=0x%X allocatorOperationDoesNotImplyPhysicalFree=1 submissionWaitIsGpuCompletion=0\n", + D3D9_BRIDGE_TELEMETRY_FILE_BACKED, D3D9_BRIDGE_TELEMETRY_FAILED, D3D9_BRIDGE_TELEMETRY_RESOURCE, + D3D9_BRIDGE_TELEMETRY_DEVICE_MEMORY, D3D9_BRIDGE_TELEMETRY_OS_MAPPING, D3D9_BRIDGE_TELEMETRY_DEVICE_IDLE, + D3D9_BRIDGE_TELEMETRY_CACHE_HIT, D3D9_BRIDGE_TELEMETRY_CACHE_RETURN, + D3D9_BRIDGE_TELEMETRY_CACHE_FILL, D3D9_BRIDGE_TELEMETRY_CACHE_EVICT); + std::uint64_t unmatchedSpans = 0, partialSpans = 0; + if (telemetry.storage) telemetry.storage->ForEach([&](std::size_t index, const D3D9BridgeTelemetryEvent& event) { + std::fprintf(file, "telemetryEvent index=%zu captureId=%llu kind=%u kindName=%s beginNs=%llu endNs=%llu thread=%u identity=0x%llX bytes=%llu result=%d flags=0x%X\n", + index, event.CaptureId, event.Kind, TelemetryKindName(event.Kind), event.BeginNs, event.EndNs, event.ThreadId, event.Identity, + event.Bytes, event.Result, event.Flags); + unsigned overlaps = 0; + std::uint64_t boundaryTruncatedNs = 0; + const auto covered = job.trace.ForEachOverlap(event.BeginNs, event.EndNs, + [&](const FrameRecord& row, std::uint64_t begin, std::uint64_t end) { + ++overlaps; + if (!row.beginIsSuccessfulPresent) boundaryTruncatedNs += end - begin; + const bool slow = row.beginIsSuccessfulPresent && row.endNs - row.beginNs > std::uint64_t(job.config.timing.slowFrameMs) * 1'000'000ull; + std::fprintf(file, "frameOverlap event=%zu frame=%llu phase=%u beginNs=%llu endNs=%llu overlapNs=%llu class=%s\n", + index, row.frameId, unsigned(row.phase), begin, end, end - begin, + !row.beginIsSuccessfulPresent ? "boundary-truncated" : slow ? "slow" : "ordinary"); + }); + const auto duration = event.EndNs >= event.BeginNs ? event.EndNs - event.BeginNs : 0; + const auto unobserved = duration >= covered ? duration - covered : 0; + if (!overlaps) ++unmatchedSpans; + if (unobserved) ++partialSpans; + std::fprintf(file, "eventCoverage event=%zu overlapFrames=%u coveredNs=%llu boundaryTruncatedNs=%llu unmatchedNs=%llu invalidInterval=%u\n", + index, overlaps, covered, boundaryTruncatedNs, unobserved, unsigned(event.EndNs < event.BeginNs)); + }); + if (telemetry.storage) { + auto& storage = *telemetry.storage; + storage.CheckTerminalAccounting(); + const auto received = storage.received.load(), accepted = storage.accepted.load(); + const auto written = storage.written.load(), reported = storage.reported.load(); + const auto omitted = storage.omitted.load(); + const bool conserved = received == accepted && accepted == written && written == reported && !omitted; + const bool complete = conserved && !storage.errors.load() && !read.Dropped && !read.LateDropped && !read.InFlight && !read.InFlightAtStop && !read.Remaining && !telemetry.active && !telemetry.invalidEvents && !telemetry.error[0]; + if (!complete) job.snapshot.unavailableMask |= RuntimeTelemetryUnavailable; + std::fprintf(file, "telemetryStorage complete=%u stagingBytes=%zu fileLimitBytes=%llu received=%llu accepted=%llu written=%llu reported=%llu omitted=%llu unread=%llu errors=0x%X\n", + unsigned(complete), (MeasurementEventStorage::Capacity + 2 * MeasurementEventStorage::BlockEvents) * sizeof(D3D9BridgeTelemetryEvent), MeasurementEventStorage::FileLimit, + received, accepted, written, reported, omitted, written >= reported ? written - reported : 0, storage.errors.load()); + } + std::fprintf(file, "eventCoverageSummary unmatchedSpans=%llu partiallyUnmatchedSpans=%llu frameTraceDropped=%llu\n", + unmatchedSpans, partialSpans, job.trace.dropped()); + std::fprintf(file, "memoryHistory source=present-and-boundaries cadenceMs=500 maxSnapshots=%zu chunkCapacity=%u maskCapacity=%u snapshots=%zu skipped=%llu failures=%llu totals=pooled-hud-chunks-only\n", + MaxMemorySnapshots, MemoryChunkCapacity, MemoryMaskCapacity, telemetry.memory.size(), telemetry.snapshotSkipped, telemetry.snapshotFailures); + std::uint64_t previousNs = 0; + for (std::size_t index = 0; index < telemetry.memory.size(); ++index) { + const auto& observation = telemetry.memory[index]; const auto& memory = observation.snapshot; + const auto* frame = job.trace.Find(memory.TimestampNs); + const auto interval = previousNs && memory.TimestampNs >= previousNs ? memory.TimestampNs - previousNs : 0; + previousNs = memory.TimestampNs; + std::fprintf(file, "memorySnapshot index=%zu boundary=%s timeNs=%llu intervalNs=%llu cadenceGap=%u scanNs=%llu frame=%llu capacity=%llu used=%llu totalChunks=%u copiedChunks=%u totalMasks=%u copiedMasks=%u flags=0x%X\n", + index, observation.boundary, memory.TimestampNs, interval, unsigned(interval > SnapshotCadenceNs + 100'000'000ull), + memory.ScanDurationNs, frame ? frame->frameId : 0, memory.TotalCapacity, memory.TotalUsed, memory.TotalChunks, + memory.CopiedChunks, memory.TotalMasks, memory.CopiedMasks, memory.Flags); + for (std::size_t chunkIndex = 0; chunkIndex < observation.chunks.size(); ++chunkIndex) { + const auto& chunk = observation.chunks[chunkIndex]; + std::fprintf(file, "memoryChunk snapshot=%zu index=%zu cookie=0x%llX heap=%u type=%u capacity=%llu used=%llu mapped=%u active=%u pages=%u maskOffset=%u maskCount=%u\n", + index, chunkIndex, chunk.Cookie, chunk.Heap, chunk.Type, chunk.Capacity, chunk.Used, chunk.Mapped, + chunk.Active, chunk.PageCount, chunk.MaskOffset, chunk.MaskCount); + if (chunk.MaskOffset <= observation.masks.size() && chunk.MaskCount <= observation.masks.size() - chunk.MaskOffset) + for (UINT m = 0; m < chunk.MaskCount; ++m) + std::fprintf(file, "memoryMask snapshot=%zu chunk=%zu index=%u bits=0x%08X\n", + index, chunkIndex, m, observation.masks[chunk.MaskOffset + m]); + } + } + for (std::size_t index = 1; index < telemetry.memory.size(); ++index) { + const auto& before = telemetry.memory[index - 1]; const auto& after = telemetry.memory[index]; + std::map previous, current; + for (const auto& chunk : before.chunks) previous[chunk.Cookie] = &chunk; + for (const auto& chunk : after.chunks) current[chunk.Cookie] = &chunk; + const bool beforeComplete = !(before.snapshot.Flags & D3D9_BRIDGE_MEMORY_CHUNKS_TRUNCATED); + const bool afterComplete = !(after.snapshot.Flags & D3D9_BRIDGE_MEMORY_CHUNKS_TRUNCATED); + const auto maskValid = [](const MemoryObservation& sample, const D3D9BridgeMemoryChunk& chunk) { + return chunk.MaskOffset <= sample.masks.size() && chunk.MaskCount <= sample.masks.size() - chunk.MaskOffset && + (chunk.PageCount == 0 || chunk.MaskCount != 0); + }; + for (const auto& [cookie, chunk] : current) { + const auto prior = previous.find(cookie); + if (prior == previous.end()) { + std::fprintf(file, "memoryChange from=%zu to=%zu cookie=0x%llX change=%s used=%llu\n", + index - 1, index, cookie, beforeComplete ? "new" : "previous-presence-unknown", chunk->Used); + continue; + } + const auto& old = *prior->second; + const char* masks = "unknown"; + if (maskValid(before, old) && maskValid(after, *chunk)) { + const bool same = old.PageCount == chunk->PageCount && old.MaskCount == chunk->MaskCount && + std::equal(before.masks.begin() + old.MaskOffset, before.masks.begin() + old.MaskOffset + old.MaskCount, + after.masks.begin() + chunk->MaskOffset); + masks = same ? "unchanged" : "changed"; + } + const auto delta = chunk->Used >= old.Used ? chunk->Used - old.Used : old.Used - chunk->Used; + if (delta || old.Capacity != chunk->Capacity || old.Mapped != chunk->Mapped || old.Active != chunk->Active || + old.Heap != chunk->Heap || old.Type != chunk->Type || std::strcmp(masks, "unchanged") != 0) + std::fprintf(file, "memoryChange from=%zu to=%zu cookie=0x%llX change=observed usedBefore=%llu usedAfter=%llu usedDirection=%s usedDelta=%llu capacityBefore=%llu capacityAfter=%llu mappedBefore=%u mappedAfter=%u activeBefore=%u activeAfter=%u mask=%s\n", + index - 1, index, cookie, old.Used, chunk->Used, chunk->Used >= old.Used ? "increase-or-same" : "decrease", + delta, old.Capacity, chunk->Capacity, old.Mapped, chunk->Mapped, old.Active, chunk->Active, masks); + } + for (const auto& [cookie, chunk] : previous) if (!current.count(cookie)) + std::fprintf(file, "memoryChange from=%zu to=%zu cookie=0x%llX change=%s previousUsed=%llu\n", + index - 1, index, cookie, afterComplete ? "removed" : "current-presence-unknown", chunk->Used); + } + std::fprintf(file, "memoryChangeSemantics=adjacent-snapshots;cookie-identity-may-be-reused;truncated-presence-is-unknown;dedicated-allocations-excluded\n"); + } else std::fprintf(file, "telemetry available=0 error=not-started\n"); if (job.cpu) { const auto& cpu = *job.cpu; + std::fprintf(file, "cpuStream kind=main available=%u thread=%lu intervalMs=%u samples=%zu missed=%llu scheduleMissed=%llu maxScheduleLatenessNs=%llu dropped=%llu suspendedNs=%llu maxSuspendedNs=%llu validFrameChains=%llu partialFrameChains=%llu invalidFrameChains=%llu error=%s\n", + unsigned(cpu.available), cpu.thread, job.config.intervalMs, cpu.samples.size(), cpu.missed, + cpu.scheduleMissed, cpu.maxScheduleLatenessNs, cpu.dropped, cpu.suspendedNs, cpu.maxSuspendedNs, + cpu.validChains, cpu.partialChains, cpu.invalidChains, Quote(cpu.error).c_str()); std::fprintf(file, "cpu captureId=%llu samples=%zu missed=%llu dropped=%llu suspendedNs=%llu maxSuspendedNs=%llu samplingElapsedNs=%llu sampledPresents=%llu validFrameChains=%llu partialFrameChains=%llu invalidFrameChains=%llu\n", cpu.id, cpu.samples.size(), cpu.missed, cpu.dropped, cpu.suspendedNs, cpu.maxSuspendedNs, cpu.elapsedNs, cpu.presents, cpu.validChains, cpu.partialChains, cpu.invalidChains); @@ -538,17 +1008,18 @@ void WriteCombinedReport(const ReportJob& job) { std::uint64_t slow = 0, ordinary = 0, unmatched = 0; for (const auto& sample : cpu.samples) { const auto* frame = job.trace.Find(sample.timeNs); - const bool isSlow = frame && frame->endNs - frame->beginNs > std::uint64_t(job.config.timing.slowFrameMs) * 1'000'000ull; - if (!frame) ++unmatched; else if (isSlow) ++slow; else ++ordinary; + const bool completeFrame = frame && frame->beginIsSuccessfulPresent; + const bool isSlow = completeFrame && frame->endNs - frame->beginNs > std::uint64_t(job.config.timing.slowFrameMs) * 1'000'000ull; + if (!completeFrame) ++unmatched; else if (isSlow) ++slow; else ++ordinary; auto found = cpu.symbols.find(sample.ip); - if (frame && found != cpu.symbols.end()) { + if (completeFrame && found != cpu.symbols.end()) { const auto& symbol = found->second; auto& counts = byClass[{symbol.moduleBase, sample.ip - symbol.offset}]; if (isSlow) ++counts.first; else ++counts.second; } - std::fprintf(file, "sample timeNs=%llu frame=%llu phase=%u class=%s ip=0x%llX suspendedNs=%llu chainQuality=%u chainCount=%u\n", + std::fprintf(file, "sample stream=main timeNs=%llu frame=%llu phase=%u class=%s ip=0x%llX suspendedNs=%llu chainQuality=%u chainCount=%u\n", sample.timeNs, frame ? frame->frameId : 0, frame ? unsigned(frame->phase) : 0, - !frame ? "unmatched" : isSlow ? "slow" : "ordinary", std::uint64_t(sample.ip), sample.suspendedNs, + !frame ? "unmatched" : !completeFrame ? "boundary-truncated" : isSlow ? "slow" : "ordinary", std::uint64_t(sample.ip), sample.suspendedNs, unsigned(sample.chain.quality), sample.chain.count); for (unsigned i = 0; i < sample.chain.count; ++i) { const auto address = sample.chain.addresses[i]; const auto* module = FindModule(cpu.modules, address); @@ -567,16 +1038,134 @@ void WriteCombinedReport(const ReportJob& job) { for (const auto& [identity, counts] : byClass) std::fprintf(file, "functionByFrameClass base=0x%llX functionStart=0x%llX slowSamples=%llu ordinarySamples=%llu\n", std::uint64_t(identity.first), std::uint64_t(identity.second), counts.first, counts.second); + if (cpu.cs) { + const auto& cs = *cpu.cs; + std::fprintf(file, "cpuStream kind=cs available=%u thread=%lu intervalMs=%u samples=%zu missed=%llu scheduleMissed=%llu maxScheduleLatenessNs=%llu dropped=%llu suspendedNs=%llu maxSuspendedNs=%llu validFrameChains=%llu partialFrameChains=%llu invalidFrameChains=%llu error=%s\n", + unsigned(cs.available), cs.thread, job.config.csIntervalMs, cs.samples.size(), cs.missed, cs.scheduleMissed, + cs.maxScheduleLatenessNs, cs.dropped, + cs.suspendedNs, cs.maxSuspendedNs, cs.validChains, cs.partialChains, cs.invalidChains, Quote(cpu.csError).c_str()); + std::uint64_t csSlow = 0, csOrdinary = 0, csUnmatched = 0, csBoundary = 0; + std::map, std::pair> csByClass; + for (const auto& sample : cs.samples) { + const auto* frame = job.trace.Find(sample.timeNs); + const bool isSlow = frame && frame->beginIsSuccessfulPresent && frame->endNs - frame->beginNs > std::uint64_t(job.config.timing.slowFrameMs) * 1'000'000ull; + if (!frame) ++csUnmatched; + else if (!frame->beginIsSuccessfulPresent) ++csBoundary; + else { + if (isSlow) ++csSlow; else ++csOrdinary; + const auto symbol = cs.symbols.find(sample.ip); + if (symbol != cs.symbols.end()) { + auto& counts = csByClass[{symbol->second.moduleBase, sample.ip - symbol->second.offset}]; + if (isSlow) ++counts.first; else ++counts.second; + } + } + std::fprintf(file, "sample stream=cs timeNs=%llu frame=%llu phase=%u class=%s ip=0x%llX suspendedNs=%llu chainQuality=%u chainCount=%u\n", + sample.timeNs, frame ? frame->frameId : 0, frame ? unsigned(frame->phase) : 0, + !frame ? "unmatched" : !frame->beginIsSuccessfulPresent ? "boundary-truncated" : isSlow ? "slow" : "ordinary", std::uint64_t(sample.ip), sample.suspendedNs, + unsigned(sample.chain.quality), sample.chain.count); + for (unsigned i = 0; i < sample.chain.count; ++i) { + const auto address = sample.chain.addresses[i]; const auto* module = FindModule(cs.modules, address); + std::fprintf(file, " caller stream=cs kind=structural-frame-chain quality=%u index=%u address=0x%llX base=0x%llX rva=0x%llX path=%s\n", + unsigned(sample.chain.quality), i, std::uint64_t(address), module ? std::uint64_t(module->base) : 0, + module ? std::uint64_t(address - module->base) : 0, Quote(module ? module->path : "private").c_str()); + } + for (unsigned i = 0; i < sample.candidates.size(); ++i) if (sample.candidates[i]) { + const auto address = sample.candidates[i]; const auto* module = FindModule(cs.modules, address); + std::fprintf(file, " caller stream=cs kind=heuristic-candidate index=%u address=0x%llX base=0x%llX rva=0x%llX path=%s\n", + i, std::uint64_t(address), module ? std::uint64_t(module->base) : 0, + module ? std::uint64_t(address - module->base) : 0, Quote(module ? module->path : "private").c_str()); + } + } + std::fprintf(file, "sampleCoverage stream=cs slow=%llu ordinary=%llu unmatched=%llu boundaryTruncated=%llu recorded=%zu dropped=%llu\n", + csSlow, csOrdinary, csUnmatched, csBoundary, cs.samples.size(), cs.dropped); + for (const auto& [identity, counts] : csByClass) + std::fprintf(file, "functionByFrameClass stream=cs base=0x%llX functionStart=0x%llX slowSamples=%llu ordinarySamples=%llu\n", + std::uint64_t(identity.first), std::uint64_t(identity.second), counts.first, counts.second); + for (const auto& function : cs.functions) + std::fprintf(file, "function stream=cs base=0x%llX rva=0x%X samples=%llu quality=%s name=%s path=%s\n", + std::uint64_t(function.symbol.moduleBase), function.symbol.rva - function.symbol.offset, function.samples, + SymbolQualityName(function.symbol.quality), Quote(function.name).c_str(), Quote(function.symbol.modulePath).c_str()); + } else std::fprintf(file, "cpuStream kind=cs available=0 reason=%s\n", cpu.csError); + } else { + std::fprintf(file, "cpuStream kind=main available=0 reason=no-completed-result\n"); + std::fprintf(file, "cpuStream kind=cs available=0 reason=no-completed-result\n"); + } + if (job.context) { + std::lock_guard lock(job.context->mutex); + std::fprintf(file, "contextSummary available=%u source=window-poll;safe-game-frame-callback cadenceMs=25 count=%zu dropped=%llu completed=%u failed=%u renderWork=unknown pixelEvidence=none\n", + unsigned(!job.context->failed.load(std::memory_order_acquire)), job.context->points.size(), job.context->dropped, + unsigned(job.context->done.load(std::memory_order_acquire)), unsigned(job.context->failed.load(std::memory_order_acquire))); + std::uint64_t excluded = 0, candidates = 0, gaps = 0; + for (const auto& point : job.context->points) { + const auto* frame = job.trace.Find(point.timeNs); + const bool candidate = point.frameKnown && point.frameUnchangedNs >= 250'000'000ull; + const bool exclude = (point.windowKnown && point.minimized) || + (point.foregroundKnown && !point.foreground) || candidate; + excluded += exclude; candidates += candidate; gaps += point.presentGap; + std::fprintf(file, "context kind=window timeNs=%llu known=%u foregroundKnown=%u foreground=%u minimized=%u frame=%llu excludeSteadyState=%u\n", + point.timeNs, unsigned(point.windowKnown), unsigned(point.foregroundKnown), unsigned(point.foreground), unsigned(point.minimized), + frame ? frame->frameId : 0, unsigned(exclude)); + std::fprintf(file, "context kind=gameFrame timeNs=%llu known=%u value=%u unchanged=%u unchangedNs=%llu frame=%llu presents=%llu\n", + point.timeNs, unsigned(point.frameKnown), point.frame, unsigned(point.unchanged), point.frameUnchangedNs, + frame ? frame->frameId : 0, point.presents); + if (candidate) + std::fprintf(file, "reuseCandidate confidence=heuristic source=unchanged-game-frame timeNs=%llu unchangedNs=%llu presentsAdvancedSinceFrameChange=%u presentGap=%u frame=%llu renderWork=unknown pixelEvidence=none\n", + point.timeNs, point.frameUnchangedNs, unsigned(point.presentsAdvanced), unsigned(point.presentGap), frame ? frame->frameId : 0); + if (point.presentGap) + std::fprintf(file, "context kind=presentGap timeNs=%llu thresholdMs=250 presents=%llu frame=%llu\n", + point.timeNs, point.presents, frame ? frame->frameId : 0); + } + std::fprintf(file, "contextCounts excludedPolls=%llu candidatePolls=%llu presentGapPolls=%llu candidateThresholdMs=250 unit=poll scope=observed-only\n", + excluded, candidates, gaps); + const auto writeRanges = [&](const char* kind, auto predicate) { + bool open = false; std::uint64_t begin = 0, last = 0, count = 0; + for (const auto& point : job.context->points) { + if (predicate(point)) { + if (!open) { begin = point.timeNs; count = 0; open = true; } + last = point.timeNs; ++count; + } else if (open) { + std::fprintf(file, "contextRange kind=%s beginObservedNs=%llu endObservedNs=%llu polls=%llu interpolation=none\n", kind, begin, last, count); + open = false; + } + } + if (open) std::fprintf(file, "contextRange kind=%s beginObservedNs=%llu endObservedNs=%llu polls=%llu interpolation=none\n", kind, begin, last, count); + }; + writeRanges("excluded-steady-state", [](const ContextPoint& point) { + return (point.windowKnown && point.minimized) || (point.foregroundKnown && !point.foreground) || + (point.frameKnown && point.frameUnchangedNs >= 250'000'000ull); + }); + writeRanges("frame-stall-candidate", [](const ContextPoint& point) { + return point.frameKnown && point.frameUnchangedNs >= 250'000'000ull; + }); + } else std::fprintf(file, "contextSummary available=0 reason=%s renderWork=unknown pixelEvidence=none\n", + job.config.context ? "worker-unavailable" : "disabled"); + if (job.telemetry && job.telemetry->storage && job.telemetry->storage->errors.load()) { + job.snapshot.unavailableMask |= RuntimeTelemetryUnavailable; + CopyText(job.snapshot.lastError, sizeof(job.snapshot.lastError), "event storage incomplete"); } if (s.lastError[0]) std::fprintf(file, "error=%s\n", Quote(s.lastError).c_str()); - if (std::fflush(file) || std::ferror(file)) throw std::runtime_error("combined report write failed"); + std::fprintf(file, "reportEnd captureId=%llu completed=1\n", s.captureId); + if (std::fseek(file, 0, SEEK_SET)) throw std::runtime_error("cannot finalize combined report header"); + writeHeader(s.phase == Phase::Complete ? (s.unavailableMask ? "complete-partial" : "complete") : + s.phase == Phase::Interrupted ? "interrupted" : "error"); + if (std::fseek(file, 0, SEEK_END) || std::fflush(file) || std::ferror(file)) throw std::runtime_error("combined report write failed"); } DWORD WINAPI ReportWorker(void* opaque) { WorkerExit activity; std::unique_ptr> holder(static_cast*>(opaque)); const auto job = *holder; std::string error; + bool reportWritten = false; try { + if (job->context) { + const auto deadline = NowNs() + 200'000'000ull; + while (!job->context->done.load(std::memory_order_acquire) && NowNs() < deadline) Sleep(1); + if (!job->context->done.load(std::memory_order_acquire) || job->context->failed.load(std::memory_order_acquire)) { + job->snapshot.unavailableMask |= RuntimeContextUnavailable; + std::lock_guard lock(g_mutex); + if (g_capture == job->snapshot.captureId) g_unavailable |= RuntimeContextUnavailable; + } + } // This wait is outside Runtime/device/owner locks. A missing transaction // end yields an explicitly incomplete report after one second or shutdown, // rather than a stuck game or falsely complete attribution. @@ -608,11 +1197,33 @@ DWORD WINAPI ReportWorker(void* opaque) { } } } + if (job->telemetry && job->telemetry->storage) { + auto& storage = *job->telemetry->storage; + storage.ClaimReader(); + while (!storage.ready.load(std::memory_order_acquire)) Sleep(1); + if (storage.errors.load()) job->snapshot.unavailableMask |= RuntimeTelemetryUnavailable; + } WriteCombinedReport(*job); + reportWritten = true; + if (job->telemetry && job->telemetry->storage) + job->telemetry->storage->finalizedReported.store(job->telemetry->storage->reported.load()); if (job->journal.captureStarted && !job->journal.inFlight) JournalAttributionDump(); Log(job->config, "capture=" + std::to_string(job->snapshot.captureId) + " report=" + job->path); } catch (const std::exception& e) { error = e.what(); } catch (...) { error = "combined report failed"; } + if (job->telemetry && job->telemetry->storage) { + auto& storage = *job->telemetry->storage; + if (!reportWritten) storage.errors.fetch_or(MeasurementEventStorage::Report); + storage.reportFinished.store(true, std::memory_order_release); + storage.consumed.store(true, std::memory_order_release); + if (storage.errors.load() || (job->snapshot.unavailableMask & RuntimeTelemetryUnavailable)) { + std::lock_guard lock(g_mutex); + if (g_capture == job->snapshot.captureId) { + g_unavailable |= RuntimeTelemetryUnavailable; + if (storage.errors.load()) CopyText(g_error, sizeof(g_error), "event storage incomplete"); + } + } + } if (!error.empty()) { std::lock_guard lock(g_mutex); if (g_capture == job->snapshot.captureId) { g_reportFailed = true; CopyText(g_error, sizeof(g_error), error); } @@ -624,22 +1235,37 @@ bool Launch(const RuntimeConfig& config, LPTHREAD_START_ROUTINE worker, const st auto* data = new (std::nothrow) std::shared_ptr(job); if (!data) return false; g_workers.fetch_add(1, std::memory_order_acq_rel); - const HANDLE handle = config.startWorker ? config.startWorker(worker, data) : nullptr; + HANDLE handle = nullptr; + try { handle = config.startWorker ? config.startWorker(worker, data) : nullptr; } + catch (...) { g_workers.fetch_sub(1, std::memory_order_release); delete data; return false; } if (handle) return true; g_workers.fetch_sub(1, std::memory_order_release); delete data; return false; } void StartReportLocked() { if (g_reportStarted || !g_capture) return; g_reportStarted = true; - SyncThreadPhaseLocked(); - auto job = std::make_shared(); - job->config = g_config; job->path = g_reportPath; - FillSnapshotLocked(job->snapshot); - job->finalPhase = g_controller.phase(); - job->trace = g_controller.trace(); job->journal = g_journal; job->audit = g_audit; job->cpu = g_cpuResult; - job->threadBegin = g_threadBegin; job->threadEnd = g_threadEnd; - if (!Launch(g_config, ReportWorker, job)) { - g_reportFailed = true; CopyText(g_error, sizeof(g_error), "cannot start combined-report worker"); + if (g_context) g_context->cancel.store(true, std::memory_order_relaxed); + try { + SyncThreadPhaseLocked(); + auto job = std::make_shared(); + job->config = g_config; job->path = g_reportPath; + FillSnapshotLocked(job->snapshot); + job->finalPhase = g_controller.phase(); + job->trace = g_controller.trace(); job->journal = g_journal; job->audit = g_audit; job->cpu = g_cpuResult; + job->context = g_context; + job->telemetry = g_telemetryCapture; + job->threadBegin = g_threadBegin; job->threadEnd = g_threadEnd; + if (!Launch(g_config, ReportWorker, job)) { + g_reportFailed = true; std::snprintf(g_error, sizeof(g_error), "cannot start combined-report worker"); + } + } catch (...) { + g_reportFailed = true; std::snprintf(g_error, sizeof(g_error), "cannot allocate combined-report job"); + } + if (g_reportFailed && g_telemetryCapture && g_telemetryCapture->storage) { + auto& storage = *g_telemetryCapture->storage; + storage.errors.fetch_or(MeasurementEventStorage::Report); + storage.reportFinished.store(true, std::memory_order_release); + storage.abort = true; } } void PublishHudLocked(std::uint64_t now, bool force = false) { @@ -662,7 +1288,7 @@ void PublishHudLocked(std::uint64_t now, bool force = false) { std::snprintf(hud.Lines[0], sizeof(hud.Lines[0]), "RenderStack #%llu: %s", g_capture, PhaseName(phase)); std::snprintf(hud.Lines[1], sizeof(hud.Lines[1]), "F8/F7: baseline -> CPU -> state | %llu / %llu ms", hud.ElapsedMs, hud.DurationMs); std::snprintf(hud.Lines[2], sizeof(hud.Lines[2]), "Live: %.1f FPS | mean %.2f ms | max %.2f ms", g_liveFps, g_liveMeanMs, g_liveMaxMs); - std::snprintf(hud.Lines[3], sizeof(hud.Lines[3]), "Phase FPS: base %.1f | CPU %.1f* | state %.1f* (*instrumented)", + std::snprintf(hud.Lines[3], sizeof(hud.Lines[3]), "Phase FPS: base %.1f* | CPU %.1f* | state %.1f* (*capture)", g_controller.stats(0).fps(), g_controller.stats(1).fps(), g_controller.stats(2).fps()); if (g_cpuResult) { std::snprintf(hud.Lines[4], sizeof(hud.Lines[4]), "CPU: %zu samples, %llu missed, %llu dropped | suspended %.2f ms", @@ -673,7 +1299,13 @@ void PublishHudLocked(std::uint64_t now, bool force = false) { g_cpuResult->samples.empty() ? 0 : 100.0 * double(top.samples) / double(g_cpuResult->samples.size()), top.name.c_str(), SymbolQualityName(top.symbol.quality)); } - } else std::snprintf(hud.Lines[4], sizeof(hud.Lines[4]), "CPU: waiting | no sampling during baseline"); + } else if (phase == Phase::Cpu) + std::snprintf(hud.Lines[4], sizeof(hud.Lines[4]), "CPU: sampling in progress | results after sampling"); + else if (phase == Phase::CpuFinishing) + std::snprintf(hud.Lines[4], sizeof(hud.Lines[4]), "CPU: processing samples | results pending"); + else if (phase == Phase::Ready || phase == Phase::Baseline) + std::snprintf(hud.Lines[4], sizeof(hud.Lines[4]), "CPU: waiting | no sampling during baseline"); + else std::snprintf(hud.Lines[4], sizeof(hud.Lines[4]), "CPU: no completed sampling result"); std::snprintf(hud.Lines[6], sizeof(hud.Lines[6]), "State: %llu transactions | API %llu calls, %llu redundant | missing=0x%X", g_journal.transactions, g_audit.Total.Calls, g_audit.Total.RedundantCalls, g_unavailable | g_controller.unavailableMask()); if (g_error[0]) std::snprintf(hud.Lines[7], sizeof(hud.Lines[7]), "Status: %s", g_error); @@ -683,21 +1315,29 @@ void PublishHudLocked(std::uint64_t now, bool force = false) { } void InterruptLocked(const char* reason, bool releaseDevice) { if (g_cpuJob) g_cpuJob->cancel.store(true, std::memory_order_relaxed); + if (g_context) g_context->cancel.store(true, std::memory_order_relaxed); const bool wasActive = g_controller.active(); if (wasActive) { g_controller.Interrupt(); CopyText(g_error, sizeof(g_error), reason ? reason : "interrupted"); - StopAttributionLocked(); StartReportLocked(); + StopAttributionLocked(); EndTelemetryLocked(NowNs(), "interrupted"); StartReportLocked(); } PublishHudLocked(NowNs(), true); if (releaseDevice) { + StopAuditSafelyLocked(); + if (g_telemetry && g_telemetryCapture && g_telemetryCapture->active) { + // An earlier stop failure must not be allowed to retain a recorder on a + // device being destroyed. Do not mutate a report already handed off. + try { g_telemetry->StopCapture(g_telemetryCapture->id); } catch (...) { } + } if (g_diagnostics) { D3D9BridgeHudSnapshot hidden{}; hidden.StructSize = sizeof(hidden); hidden.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; g_diagnostics->PublishHud(&hidden); g_diagnostics->SetCaptureOwner(FALSE); } - g_diagnostics = nullptr; g_threadStats = nullptr; g_device = nullptr; g_owner = 0; - } + g_diagnostics = nullptr; g_threadStats = nullptr; g_telemetry = nullptr; g_device = nullptr; g_deviceWindow = nullptr; g_owner = 0; + g_auditStarted = false; g_auditCapture = 0; + } else g_deviceWindow = nullptr; // Reset may replace the presentation window. } } // namespace @@ -717,10 +1357,11 @@ void ConfigureRuntime(const RuntimeConfig& config) { } g_config = config; g_directory = config.gameDirectory ? config.gameDirectory : "."; g_controller = Controller(config.timing); - g_device = nullptr; g_diagnostics = nullptr; g_threadStats = nullptr; g_owner = 0; g_capture = 0; + g_device = nullptr; g_diagnostics = nullptr; g_threadStats = nullptr; g_telemetry = nullptr; g_deviceWindow = nullptr; g_owner = 0; g_capture = 0; g_threadBegin = {}; g_threadEnd = {}; g_threadPhase = -1; - g_cpuJob.reset(); g_cpuResult.reset(); g_cpuReady = false; + g_cpuJob.reset(); g_cpuResult.reset(); g_context.reset(); g_telemetryCapture.reset(); g_cpuReady = false; g_journalStarted = g_auditStarted = g_reportFailed = g_reportStarted = g_reportInterrupted = false; + g_auditCapture = 0; g_journalDrainPending = false; g_journal = {}; g_audit = {}; g_unavailable = 0; g_lastHudNs = 0; g_reportPath[0] = g_error[0] = 0; g_liveStarted = false; @@ -729,6 +1370,23 @@ void ConfigureRuntime(const RuntimeConfig& config) { } bool RuntimeEnabled() noexcept { return g_enabled.load(std::memory_order_acquire); } +static HWND RuntimeDeviceWindow(IDirect3DDevice9* device) { + HWND window = nullptr; + IDirect3DSwapChain9* swapChain = nullptr; + D3DPRESENT_PARAMETERS parameters{}; + bool presentationParametersKnown = false; + if (SUCCEEDED(device->GetSwapChain(0, &swapChain)) && swapChain) { + if (SUCCEEDED(swapChain->GetPresentParameters(¶meters))) { + window = parameters.hDeviceWindow; presentationParametersKnown = true; + } + swapChain->Release(); + } + if (presentationParametersKnown && !window) { + D3DDEVICE_CREATION_PARAMETERS creation{}; + if (SUCCEEDED(device->GetCreationParameters(&creation))) window = creation.hFocusWindow; + } + return window; +} void RuntimePrepareDevice(IDirect3DDevice9* device) { if (!RuntimeEnabled() || !device) return; std::lock_guard lock(g_mutex); @@ -745,10 +1403,16 @@ void RuntimePrepareDevice(IDirect3DDevice9* device) { g_threadStats = threadStats; threadStats->Release(); } + ID3D9BridgeTelemetry* telemetry = nullptr; + if (SUCCEEDED(device->QueryInterface(IID_D3D9BridgeTelemetry, reinterpret_cast(&telemetry)))) { + g_telemetry = telemetry; + telemetry->Release(); + } + g_deviceWindow = RuntimeDeviceWindow(device); g_device = device; // publish borrowed state only after successful setup } -void RuntimePresent(IDirect3DDevice9* device, HRESULT result) { +void RuntimePresent(IDirect3DDevice9* device, HRESULT result, HWND windowOverride) { if (!RuntimeEnabled() || !device) return; RuntimePrepareDevice(device); std::lock_guard lock(g_mutex); @@ -762,6 +1426,7 @@ void RuntimePresent(IDirect3DDevice9* device, HRESULT result) { if (!g_owner) g_owner = GetCurrentThreadId(); g_controller.SetAvailable(g_config.cpu, g_config.journal || g_diagnostics != nullptr); if (g_owner != GetCurrentThreadId()) return; + if (g_context) g_context->window.store(windowOverride ? windowOverride : g_deviceWindow, std::memory_order_relaxed); const bool success = result == S_OK; if (success) g_successfulPresents.fetch_add(1, std::memory_order_relaxed); if (!g_liveStarted) { g_live.Begin(now); g_liveStarted = true; } @@ -775,24 +1440,48 @@ void RuntimePresent(IDirect3DDevice9* device, HRESULT result) { const auto key = [&](int vk) { return g_config.keyDown ? g_config.keyDown(vk) : (GetAsyncKeyState(vk) & 0x8000) != 0; }; const bool f7 = key(VK_F7), f8 = key(VK_F8); if ((!g_workers.load(std::memory_order_acquire) && !g_journalDrainPending) || g_controller.active()) { - const bool newCapture = g_controller.Key(f7, f8, now); + const bool newCapture = g_controller.Key(f7, f8, now, success); if (newCapture) { + if (!g_deviceWindow) g_deviceWindow = RuntimeDeviceWindow(device); + StopAuditSafelyLocked(); + if (g_telemetry && g_telemetryCapture && g_telemetryCapture->active) { + try { g_telemetry->StopCapture(g_telemetryCapture->id); } catch (...) { } + } g_capture = ++g_nextCapture; g_cpuJob.reset(); g_cpuResult.reset(); g_cpuReady = false; g_reportStarted = g_reportFailed = g_reportInterrupted = false; g_error[0] = 0; + g_reportPath[0] = 0; + const auto written = std::snprintf(g_reportPath, sizeof(g_reportPath), + "%s\\scripts\\BridgeD3D9.measurement-%lu-%llu.log", g_directory.empty() ? "." : g_directory.c_str(), + GetCurrentProcessId(), g_capture); + if (written < 0 || std::size_t(written) >= sizeof(g_reportPath)) { + g_reportPath[0] = 0; g_controller.Interrupt(); g_reportFailed = true; + std::snprintf(g_error, sizeof(g_error), "measurement report path too long"); + PublishHudLocked(now, true); return; + } g_journal = {}; g_audit = {}; g_threadBegin = {}; g_threadEnd = {}; g_threadPhase = -1; SyncThreadPhaseLocked(); g_unavailable = g_diagnostics ? 0 : RuntimeBackendUnavailable; + if (g_auditStarted) g_unavailable |= RuntimeBackendUnavailable; if (!g_config.journal) g_unavailable |= RuntimeJournalUnavailable; - const auto path = (std::filesystem::path(g_directory) / "scripts" / - ("BridgeD3D9.measurement-" + std::to_string(GetCurrentProcessId()) + "-" + std::to_string(g_capture) + ".log")).string(); - CopyText(g_reportPath, sizeof(g_reportPath), path); + BeginTelemetryLocked(now); + g_context.reset(); + if (g_config.context) try { + g_context = std::make_shared(); + g_context->config = g_config; g_context->id = g_capture; + g_context->points.reserve(4096); + g_context->window.store(windowOverride ? windowOverride : g_deviceWindow, std::memory_order_relaxed); + if (!Launch(g_config, ContextWorker, g_context)) { + g_context.reset(); g_unavailable |= RuntimeContextUnavailable; + } + } catch (...) { g_context.reset(); g_unavailable |= RuntimeContextUnavailable; } Log(g_config, "capture=" + std::to_string(g_capture) + " started baseline -> CPU -> attribution"); PublishHudLocked(now, true); return; } } else g_controller.SuppressKeys(f7, f8); + PollTelemetryLocked(now); if (g_cpuReady && (g_controller.phase() == Phase::Cpu || g_controller.phase() == Phase::CpuFinishing)) { g_controller.CpuFinished(g_cpuResult && g_cpuResult->error.empty()); g_cpuReady = false; @@ -801,24 +1490,42 @@ void RuntimePresent(IDirect3DDevice9* device, HRESULT result) { const auto action = g_controller.Present(now, success); SyncThreadPhaseLocked(); if (action == Action::StartCpu) { - auto job = std::make_shared(); job->config = g_config; job->directory = g_directory; - job->thread = g_owner; job->id = g_capture; g_cpuJob = job; - if (!g_config.cpu) { - g_unavailable |= RuntimeCpuUnavailable; g_controller.CpuFinished(true); - } else if (!Launch(g_config, CpuWorker, job)) { - CopyText(g_error, sizeof(g_error), "cannot start CPU sampler worker"); g_controller.CpuFinished(false); + bool started = false; + try { + auto job = std::make_shared(); job->config = g_config; job->directory = g_directory; + job->thread = g_owner; job->id = g_capture; job->csThread = g_threadBegin[1].CsThreadId; + g_cpuJob = job; + started = Launch(g_config, CpuWorker, job); + } catch (...) { } + if (!started) { + g_cpuJob.reset(); + g_unavailable |= RuntimeCpuUnavailable; + if (g_config.csCpu) g_unavailable |= RuntimeCsCpuUnavailable; + std::snprintf(g_error, sizeof(g_error), "cannot start CPU sampler worker"); + g_controller.CpuFinished(false); + if (g_controller.Present(now, success) == Action::ReportError) { + SyncThreadPhaseLocked(); + StopAttributionLocked(); EndTelemetryLocked(now, "error"); StartReportLocked(); + } } } else if (action == Action::StartAttribution) { if (g_config.journal) { JournalAttributionSetCaptureIdentity(g_capture); JournalAttributionStartCapture(); g_journalStarted = true; } - if (g_diagnostics) { - const auto started = g_diagnostics->StartAudit(g_capture, g_config.timing.attributionMs); - if (SUCCEEDED(started)) { g_auditStarted = true; ReadAuditLocked(); } - else g_unavailable |= RuntimeBackendUnavailable; + if (g_diagnostics && !g_auditStarted) { + // StartAudit may have changed backend state before an exception crosses + // the private COM boundary; retain its identity before making the call. + g_auditStarted = true; g_auditCapture = g_capture; + try { + const auto started = g_diagnostics->StartAudit(g_capture, g_config.timing.attributionMs); + if (SUCCEEDED(started)) ReadAuditLocked(); + else { g_unavailable |= RuntimeBackendUnavailable; StopAuditSafelyLocked(); } + } catch (...) { g_unavailable |= RuntimeBackendUnavailable; StopAuditSafelyLocked(); } + } else if (g_auditStarted) { + g_unavailable |= RuntimeBackendUnavailable; } } else if (action == Action::FinishAttribution || action == Action::ReportError) { - StopAttributionLocked(); StartReportLocked(); + StopAttributionLocked(); EndTelemetryLocked(now, action == Action::ReportError ? "error" : "end"); StartReportLocked(); } PublishHudLocked(now, action != Action::None); } diff --git a/src/bridge/legacy/BridgeMeasurementRuntime.h b/src/bridge/legacy/BridgeMeasurementRuntime.h index 60cb261..50e59b3 100644 --- a/src/bridge/legacy/BridgeMeasurementRuntime.h +++ b/src/bridge/legacy/BridgeMeasurementRuntime.h @@ -32,8 +32,9 @@ struct RuntimeCpuReport { struct RuntimeConfig { bool enabled = false, hud = true, journal = true, cpu = true; + bool telemetry = true, csCpu = true, context = true; Options timing{}; - std::uint32_t intervalMs = 5; + std::uint32_t intervalMs = 5, csIntervalMs = 10; const char* gameDirectory = nullptr; HANDLE stopEvent = nullptr; // The host owns returned thread handles and must join them before unload or @@ -49,6 +50,9 @@ inline constexpr std::uint32_t RuntimeCpuUnavailable = 2; inline constexpr std::uint32_t RuntimeAttributionUnavailable = 4; inline constexpr std::uint32_t RuntimeBackendUnavailable = 8; inline constexpr std::uint32_t RuntimeJournalUnavailable = 16; +inline constexpr std::uint32_t RuntimeTelemetryUnavailable = 32; +inline constexpr std::uint32_t RuntimeCsCpuUnavailable = 64; +inline constexpr std::uint32_t RuntimeContextUnavailable = 128; struct RuntimeSnapshot { Phase phase = Phase::Ready; std::uint64_t captureId = 0; @@ -56,9 +60,22 @@ struct RuntimeSnapshot { FrameStats phaseStats[3]{}; std::uint64_t cpuSamples = 0, cpuMissed = 0, cpuDropped = 0; std::uint64_t suspendedNs = 0, frameTraceDropped = 0; + std::uint64_t csSamples = 0, csMissed = 0, csDropped = 0, csSuspendedNs = 0; + std::uint64_t mainThreadCycles = 0, csThreadCycles = 0; + // Before report completion, events counts full binary records already spooled; + // queued records are pending, not retained or dropped. After report completion, + // events counts records in the finalized report. Dropped then includes unread + // or unfinalized binary records, in addition to backend and storage omissions. + std::uint64_t telemetryEvents = 0, telemetryFiltered = 0, telemetryDropped = 0; + std::uint64_t phaseMainCpuNs[3]{}, phaseCsCpuNs[3]{}, memorySnapshots = 0; + std::uint32_t threadActivityMask = 0; bool workerBusy = false; bool hudRequested = false, hudIntegrationAvailable = false; char reportPath[MAX_PATH]{}, lastError[128]{}; + // Appended private snapshot fields preserve the earlier fixture layout. + std::uint64_t telemetryUnread = 0; + bool telemetryReportFinished = false; + std::uint32_t telemetryStorageErrors = 0; }; void ConfigureRuntime(const RuntimeConfig&); @@ -66,7 +83,7 @@ bool RuntimeEnabled() noexcept; // Acquire combined audit ownership before the backend can poll a first F7. // The first real Present selects the capture thread, not device creation. void RuntimePrepareDevice(IDirect3DDevice9* rawDevice); -void RuntimePresent(IDirect3DDevice9* rawDevice, HRESULT); +void RuntimePresent(IDirect3DDevice9* rawDevice, HRESULT, HWND windowOverride = nullptr); void RuntimeInterrupt(IDirect3DDevice9* rawDevice, const char* reason, bool releaseDevice = false); void RuntimeShutdown(); bool RuntimeGetSnapshot(RuntimeSnapshot*); diff --git a/src/bridge/legacy/MeasurementEventStorage.h b/src/bridge/legacy/MeasurementEventStorage.h new file mode 100644 index 0000000..8cbd723 --- /dev/null +++ b/src/bridge/legacy/MeasurementEventStorage.h @@ -0,0 +1,137 @@ +#pragma once +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#include +#include +#include +#include +#include "../../../backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h" + +namespace BridgeMeasurement::detail { + +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) +struct MeasurementEventStorage; +void MeasurementStorageEmptyHeadSnapshot(MeasurementEventStorage&) noexcept; +#endif + +// Capture-private SPSC staging. The device callback only copies into the ring; +// its managed worker owns all file creation, writes and cleanup. +struct MeasurementEventStorage { + using Event = D3D9BridgeTelemetryEvent; + static constexpr size_t Capacity = 32768; + static constexpr size_t BlockEvents = 256; + static constexpr UINT64 FileLimit = 128ull * 1024 * 1024; + static_assert((Capacity + 2 * BlockEvents) * sizeof(Event) <= 2 * 1024 * 1024); + enum Error : unsigned { Open = 1, Write = 2, Read = 4, Cap = 8, Queue = 16, Worker = 32, Cancelled = 64, Report = 128, Accounting = 256 }; + std::unique_ptr ring{new Event[Capacity]}; + std::atomic head{0}, tail{0}, received{0}, accepted{0}, written{0}, omitted{0}, reported{0}; + std::atomic finalizedReported{0}; + std::atomic reportFinished{false}; + std::atomic errors{0}; + std::atomic sealed{false}, ready{true}, consumed{false}, abort{false}; + // 0: no reader yet; 1: report owns reading; 2: shutdown won ownership. + // Shutdown and the report must exchange ownership before touching the handle. + std::atomic readerOwner{0}; + bool ClaimReader() noexcept { + unsigned expected = 0; + if (readerOwner.compare_exchange_strong(expected, 1)) return true; + return expected == 1; + } + HANDLE file = INVALID_HANDLE_VALUE; // worker publishes before ready; reader finishes before consumed + HANDLE stop = nullptr; + + void Push(const Event& event) noexcept { + received.fetch_add(1, std::memory_order_relaxed); + if (errors.load() & Worker) { omitted.fetch_add(1); return; } + const auto h = head.load(std::memory_order_relaxed); + if (h - tail.load(std::memory_order_acquire) == Capacity) { + errors.fetch_or(Queue); omitted.fetch_add(1); return; + } + ring[h % Capacity] = event; + head.store(h + 1, std::memory_order_release); + accepted.fetch_add(1, std::memory_order_relaxed); + } + void CheckTerminalAccounting() noexcept { + // A stopped producer has published all counters before seal/abort. Keep raw + // omissions distinct from unread records: flag discrepancies, never count a + // missing or duplicated record a second time to make the totals agree. + const auto r = received.load(), a = accepted.load(), w = written.load(); + if (a > r || w > a || w > r || omitted.load() != r - w || + head.load(std::memory_order_acquire) != tail.load() || reported.load() > w) + errors.fetch_or(Accounting); + } + void Run() noexcept { + wchar_t directory[MAX_PATH]{}, path[MAX_PATH]{}; + const auto pathLength = GetTempPathW(MAX_PATH, directory); + if (!pathLength || pathLength >= MAX_PATH || !GetTempFileNameW(directory, L"F8e", 0, path)) errors.fetch_or(Open); + else { + file = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, + nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, nullptr); + if (file == INVALID_HANDLE_VALUE) { DeleteFileW(path); errors.fetch_or(Open); } + } + std::array block; + for (;;) { + const auto t = tail.load(std::memory_order_relaxed); + const auto h = head.load(std::memory_order_acquire); + const auto count = (std::min)(UINT64(BlockEvents), h - t); + if (!count) { +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) + MeasurementStorageEmptyHeadSnapshot(*this); +#endif + if (sealed.load(std::memory_order_acquire) || abort.load(std::memory_order_acquire)) { + // The producer may have published its final batch after the empty + // snapshot above. Only a head observed AFTER stop can prove drained. + if (head.load(std::memory_order_acquire) == t) break; + continue; + } + Sleep(1); continue; + } + for (UINT64 i = 0; i < count; ++i) block[size_t(i)] = ring[size_t((t + i) % Capacity)]; + tail.store(t + count, std::memory_order_release); + const DWORD bytes = DWORD(count * sizeof(Event)); + if (abort.load()) { errors.fetch_or(Cancelled); omitted.fetch_add(count); } + else if (errors.load() & (Open | Write | Cap)) omitted.fetch_add(count); + else if ((written.load() + count) * sizeof(Event) > FileLimit) { errors.fetch_or(Cap); omitted.fetch_add(count); } + else { + DWORD actual = 0; + const bool ok = WriteFile(file, block.data(), bytes, &actual, nullptr) != FALSE; + const auto complete = actual / sizeof(Event); + written.fetch_add(complete); + if (!ok || actual != bytes) { errors.fetch_or(Write); omitted.fetch_add(count - complete); } + } + } + CheckTerminalAccounting(); + ready.store(true, std::memory_order_release); + while (!consumed.load(std::memory_order_acquire) && !abort.load()) { + if (stop && WaitForSingleObject(stop, 0) != WAIT_TIMEOUT) { + unsigned expected = 0; + if (readerOwner.compare_exchange_strong(expected, 2)) { errors.fetch_or(Cancelled); break; } + } + Sleep(1); + } + if (file != INVALID_HANDLE_VALUE) { CloseHandle(file); file = INVALID_HANDLE_VALUE; } + } + template void ForEach(F callback) { + const bool owned = ClaimReader(); + while (!ready.load(std::memory_order_acquire)) Sleep(1); + if (!owned) { errors.fetch_or(Cancelled); return; } + if (file == INVALID_HANDLE_VALUE) return; + LARGE_INTEGER offset{}; + if (!SetFilePointerEx(file, offset, nullptr, FILE_BEGIN)) { errors.fetch_or(Read); return; } + std::array block; + const auto total = written.load(); + for (UINT64 index = 0; index < total;) { + const auto count = (std::min)(UINT64(BlockEvents), total - index); + DWORD actual = 0; + if (!ReadFile(file, block.data(), DWORD(count * sizeof(Event)), &actual, nullptr) || actual != count * sizeof(Event)) { + errors.fetch_or(Read); break; + } + for (UINT64 i = 0; i < count; ++i) { callback(size_t(index + i), block[size_t(i)]); reported.fetch_add(1); } + index += count; + } + } +}; + +} // namespace BridgeMeasurement::detail diff --git a/tests/cs-wait-analysis-test.py b/tests/cs-wait-analysis-test.py new file mode 100644 index 0000000..5ee4e14 --- /dev/null +++ b/tests/cs-wait-analysis-test.py @@ -0,0 +1,23 @@ +import importlib.util,pathlib,unittest +p=pathlib.Path(__file__).resolve().parents[1]/'tools/analyze-cs-waits.py' +spec=importlib.util.spec_from_file_location('analysis',p);analysis=importlib.util.module_from_spec(spec);spec.loader.exec_module(analysis) + +def event(kind,start,end,thread=2,identity=4,flags=0): + return dict(kind=kind,beginNs=start,endNs=end,thread=thread,identity=identity,flags=flags,captureId=1) + +class Correlation(unittest.TestCase): + def test_nested_spans_are_unioned_and_clipped(self): + rows=[event(5,100,200,thread=1),event(13,90,160),event(13,160,210),event(14,80,140),event(15,130,170)] + r=analysis.analyze(rows)[0] + self.assertEqual(r['csObservedNs'],100);self.assertEqual(r['driverWithinCsNs'],70);self.assertEqual(r['unobservedNs'],0) + def test_unrelated_threads_and_future_sequences_do_not_explain_wait(self): + rows=[event(5,100,200,thread=1),event(13,100,200,identity=8),event(15,100,200,thread=3)] + r=analysis.analyze(rows)[0];self.assertEqual(r['csObservedNs'],0);self.assertEqual(r['driverWithinCsNs'],0);self.assertEqual(r['unobservedNs'],100) + def test_high_priority_can_delay_an_ordered_target_without_same_sequence(self): + rows=[event(5,100,200,thread=1),event(13,100,150,identity=0,flags=2048),event(13,150,200)] + r=analysis.analyze(rows)[0];self.assertEqual(r['csObservedNs'],100);self.assertEqual(r['highPriorityNs'],50) + def test_missing_spans_remain_unknown(self): + r=analysis.analyze([event(5,100,200,thread=1)])[0] + self.assertEqual(r['unobservedNs'],100);self.assertFalse(r['targetObserved']) + +if __name__=='__main__':unittest.main() diff --git a/tests/lib/vulkan-dispatch-observer.h b/tests/lib/vulkan-dispatch-observer.h new file mode 100644 index 0000000..5bd231a --- /dev/null +++ b/tests/lib/vulkan-dispatch-observer.h @@ -0,0 +1,69 @@ +#pragma once +#define VK_NO_PROTOTYPES +#include +#include +#include +#include + +// Test-only interception of the real loader boundary. Every call reaches +// Vulkan. Timed benchmark runs do not install this observer. +namespace command_observer { +inline PFN_vkGetInstanceProcAddr instanceProc; +inline PFN_vkGetDeviceProcAddr deviceProc; +inline PFN_vkAllocateCommandBuffers allocate; +inline PFN_vkBeginCommandBuffer begin; +inline PFN_vkResetCommandPool reset; +inline PFN_vkDestroyCommandPool destroy; +inline decltype(&GetProcAddress) getProc; +inline std::mutex mutex; +struct Pool { unsigned allocated=0; std::unordered_set used; }; +inline std::unordered_map pools; +inline std::unordered_map owners; +inline unsigned calls=0, buffers=0, batches=0, resets=0, destroyed=0, maxSpare=0, errors=0; +inline void (*beforeBegin)(VkCommandBuffer) = nullptr; +inline void (*beforeAllocate)() = nullptr; +inline VKAPI_ATTR VkResult VKAPI_CALL Allocate(VkDevice d,const VkCommandBufferAllocateInfo* info,VkCommandBuffer* out) { + if(beforeAllocate)beforeAllocate(); + auto result=allocate(d,info,out); + if(result==VK_SUCCESS && info->level==VK_COMMAND_BUFFER_LEVEL_PRIMARY) { + std::lock_guard lock(mutex);++calls;buffers+=info->commandBufferCount;batches+=info->commandBufferCount>1; + auto& pool=pools[info->commandPool];pool.allocated+=info->commandBufferCount; + for(unsigned i=0;icommandBufferCount;++i)owners[out[i]]=info->commandPool; + } + return result; +} +inline VKAPI_ATTR VkResult VKAPI_CALL Begin(VkCommandBuffer buffer,const VkCommandBufferBeginInfo* info) { + if(beforeBegin)beforeBegin(buffer); + auto result=begin(buffer,info); + std::lock_guard lock(mutex); + auto it=owners.find(buffer);if(it!=owners.end()){auto& pool=pools[it->second];pool.used.insert(buffer);maxSpare=std::max(maxSpare,pool.allocated-unsigned(pool.used.size()));} + errors+=result!=VK_SUCCESS;return result; +} +inline VKAPI_ATTR VkResult VKAPI_CALL Reset(VkDevice d,VkCommandPool pool,VkCommandPoolResetFlags flags) { + auto result=reset(d,pool,flags);std::lock_guard lock(mutex);++resets;errors+=result!=VK_SUCCESS;return result; +} +inline VKAPI_ATTR void VKAPI_CALL Destroy(VkDevice d,VkCommandPool pool,const VkAllocationCallbacks* callbacks) { + destroy(d,pool,callbacks);std::lock_guard lock(mutex);++destroyed; + for(auto i=owners.begin();i!=owners.end();)if(i->second==pool)i=owners.erase(i);else ++i; + pools.erase(pool); +} +inline VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL DeviceProc(VkDevice d,const char* name) { + auto p=deviceProc(d,name); + if(!std::strcmp(name,"vkAllocateCommandBuffers")){allocate=reinterpret_cast(p);return reinterpret_cast(&Allocate);} + if(!std::strcmp(name,"vkBeginCommandBuffer")){begin=reinterpret_cast(p);return reinterpret_cast(&Begin);} + if(!std::strcmp(name,"vkResetCommandPool")){reset=reinterpret_cast(p);return reinterpret_cast(&Reset);} + if(!std::strcmp(name,"vkDestroyCommandPool")){destroy=reinterpret_cast(p);return reinterpret_cast(&Destroy);} + return p; +} +inline VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL InstanceProc(VkInstance i,const char* name) { + auto p=instanceProc(i,name); + if(!std::strcmp(name,"vkGetDeviceProcAddr")){deviceProc=reinterpret_cast(p);return reinterpret_cast(&DeviceProc);}return p; +} +inline FARPROC WINAPI GetProc(HMODULE m,LPCSTR name) { + auto p=getProc(m,name); + if(reinterpret_cast(name)>65535 && !std::strcmp(name,"vkGetInstanceProcAddr")) { + instanceProc=reinterpret_cast(p);return reinterpret_cast(&InstanceProc); + } + return p; +} +} diff --git a/tests/lockrect-pool-probe.cpp b/tests/lockrect-pool-probe.cpp new file mode 100644 index 0000000..f47f27d --- /dev/null +++ b/tests/lockrect-pool-probe.cpp @@ -0,0 +1,88 @@ +#define main UploadMappingLegacyMain +#include "upload-mapping-probe.cpp" +#undef main +#include "lib/vulkan-dispatch-observer.h" +#include "../backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h" +#include + +static std::atomic delayBegin{false}; +static void DelayBegin(VkCommandBuffer) {if(delayBegin.exchange(false))Sleep(25);} +static void DelayAllocate() {if(delayBegin.exchange(false))Sleep(25);} +static void WaitCorrelation(IDirect3DDevice9* d,bool allocation) { + IDirect3DSurface9 *rt=nullptr,*copy=nullptr;CHECK(d->GetRenderTarget(0,&rt)==S_OK); + CHECK(d->CreateOffscreenPlainSurface(32,32,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,©,nullptr)==S_OK);Flush(d); + ID3D9BridgeTelemetry* t=nullptr;CHECK(d->QueryInterface(IID_D3D9BridgeTelemetry,reinterpret_cast(&t))==S_OK);if(!t)return; + D3D9BridgeTelemetryConfig c{};c.StructSize=sizeof(c);c.Version=1;c.CaptureId=1;c.MaxEvents=65536;CHECK(t->StartCapture(&c)==S_OK); + if(allocation)command_observer::beforeAllocate=&DelayAllocate;else command_observer::beforeBegin=&DelayBegin;delayBegin=true; + CHECK(d->Clear(0,nullptr,D3DCLEAR_TARGET,0xff987654,1,0)==S_OK);CHECK(d->GetRenderTargetData(rt,copy)==S_OK); + auto lockBegin=Milliseconds();D3DLOCKED_RECT a{};CHECK(copy->LockRect(&a,nullptr,D3DLOCK_READONLY)==S_OK);auto lockEnd=Milliseconds(); + CHECK(a.pBits && *static_cast(a.pBits)==0xff987654);CHECK(copy->UnlockRect()==S_OK);CHECK(!delayBegin.load()); + Flush(d);command_observer::beforeBegin=nullptr;command_observer::beforeAllocate=nullptr;CHECK(t->StopCapture(1)==S_OK); + D3D9BridgeTelemetryRead r{};r.StructSize=sizeof(r);r.Version=1;std::vector events(65536); + CHECK(t->ReadEvents(1,&r,events.data(),unsigned(events.size()))==S_OK);CHECK(!r.Dropped && !r.LateDropped && !r.Remaining);events.resize(r.Written); + bool correlated=false,driver=false,chunk=false;unsigned waits=0;UINT64 overlap=0; + for(const auto& e:events){ + std::printf("telemetryEvent captureId=1 kind=%u beginNs=%llu endNs=%llu threadId=%u identity=%llu bytes=%llu result=%d flags=%u\n",e.Kind,e.BeginNs,e.EndNs,e.ThreadId,e.Identity,e.Bytes,e.Result,e.Flags); + if(e.Kind==D3D9_BRIDGE_TELEMETRY_CS_WAIT)++waits; + if(e.Kind!=(allocation?D3D9_BRIDGE_TELEMETRY_COMMAND_ALLOCATE:D3D9_BRIDGE_TELEMETRY_COMMAND_BEGIN) || e.EndNs-e.BeginNs<20000000)continue; + driver=true;CHECK(e.Result==0 && e.ThreadId!=GetCurrentThreadId()); + for(const auto& work:events)if(work.Kind==D3D9_BRIDGE_TELEMETRY_CS_CHUNK && work.ThreadId==e.ThreadId && work.BeginNs<=e.BeginNs && work.EndNs>=e.EndNs){ + chunk=true; + for(const auto& wait:events)if(wait.Kind==D3D9_BRIDGE_TELEMETRY_CS_WAIT && wait.ThreadId==GetCurrentThreadId() && !(wait.Flags&D3D9_BRIDGE_TELEMETRY_HIGH_PRIORITY) && !(work.Flags&D3D9_BRIDGE_TELEMETRY_HIGH_PRIORITY) && work.Identity && work.Identity<=wait.Identity){ + auto lo=std::max(e.BeginNs,wait.BeginNs),hi=std::min(e.EndNs,wait.EndNs);if(hi>lo){overlap=std::max(overlap,hi-lo);correlated|=hi-lo>=10000000;} + } + } + } + CHECK(driver && chunk && correlated && waits>0); + std::printf("controlled-driver-delay lockMs=%.6f overlapNs=%llu driver=%u chunk=%u correlated=%u\n",lockEnd-lockBegin,overlap,unsigned(driver),unsigned(chunk),unsigned(correlated)); + t->Release();copy->Release();rt->Release();Flush(d); +} + +static void Work(IDirect3DDevice9* d,unsigned iterations,bool timed,bool upload) { + const unsigned before=failures; + IDirect3DTexture9* src[8]{};IDirect3DTexture9* gpu[8]{}; + IDirect3DSurface9 *rt=nullptr,*copy=nullptr; + CHECK(d->GetRenderTarget(0,&rt)==S_OK); + CHECK(d->CreateOffscreenPlainSurface(32,32,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,©,nullptr)==S_OK); + std::vector locks;locks.reserve(iterations); + ULONG64 c0=0,c1=0;QueryThreadCycleTime(GetCurrentThread(),&c0); + auto start=Milliseconds(); + if(upload)for(unsigned j=0;j<8;++j){CHECK(d->CreateTexture(256,256,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&src[j],nullptr)==S_OK);CHECK(d->CreateTexture(256,256,1,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&gpu[j],nullptr)==S_OK);} + for(unsigned i=0;iUpdateTexture(src[j],gpu[j])==S_OK);} + const auto color=Color(0,i%6); + CHECK(d->Clear(0,nullptr,D3DCLEAR_TARGET,color,1,0)==S_OK); + CHECK(d->GetRenderTargetData(rt,copy)==S_OK); + // A later GPU write must not change the result of the queued readback. + CHECK(d->Clear(0,nullptr,D3DCLEAR_TARGET,color^0x0000ffff,1,0)==S_OK); + D3DLOCKED_RECT a{};auto begin=Milliseconds(); + CHECK(copy->LockRect(&a,nullptr,D3DLOCK_READONLY)==S_OK);locks.push_back(Milliseconds()-begin); + bool exact=a.pBits!=nullptr; + if(a.pBits){for(unsigned y=0;y<32;++y)for(unsigned x=0;x<32;++x)exact &= reinterpret_cast(static_cast(a.pBits)+y*a.Pitch)[x]==color;CHECK(copy->UnlockRect()==S_OK);} + CHECK(exact); + } + for(auto p:src)if(p)p->Release();for(auto p:gpu)if(p)p->Release();copy->Release();rt->Release();Flush(d); + const auto total=Milliseconds()-start;QueryThreadCycleTime(GetCurrentThread(),&c1); + if(timed){std::sort(locks.begin(),locks.end());double sum=0;for(auto n:locks)sum+=n; + std::printf("{\"type\":\"lockrect-pool-benchmark\",\"workload\":\"%s\",\"iterations\":%u,\"valid\":%s,\"totalMs\":%.6f,\"mainThreadCycles\":%llu,\"lockSumMs\":%.6f,\"lockP95Ms\":%.6f,\"lockMaxMs\":%.6f,\"observer\":false}\n",upload?"upload-readback":"readback",iterations,failures==before?"true":"false",total,static_cast(c1-c0),sum,locks[(locks.size()-1)*95/100],locks.back());} +} +int main(int argc,char** argv) { + std::setvbuf(stdout,nullptr,_IONBF,0);if(argc<3)return 2; + std::string mode=argv[2];bool timed=mode=="bench";bool upload=argc<4 || std::strcmp(argv[3],"readback"); + unsigned iterations=argc>4?std::strtoul(argv[4],nullptr,10):48; + if(!iterations || iterations>4096 || (mode!="observe" && mode!="regression" && mode!="correlation" && mode!="correlation-allocate" && !timed))return 2; + SetEnvironmentVariableA("DXVK_CONFIG","d3d9.textureMemory=100;d3d9.presentInterval=0;d3d9.gtaSaCompat=False;dxvk.hud="); + SetEnvironmentVariableA("DXVK_CONFIG_FILE","lockrect-no-external.conf");SetEnvironmentVariableA("DXVK_LOG_LEVEL","error");SetEnvironmentVariableA("DXVK_HUD","0"); + auto lib=LoadLibraryA(argv[1]);CHECK(lib);if(!lib)return 1; + { + ImportHook hook;if(!timed){CHECK(hook.install(lib,"GetProcAddress",reinterpret_cast(&command_observer::GetProc)));command_observer::getProc=reinterpret_cast(hook.original);} + using Factory=IDirect3D9* (WINAPI*)(UINT);auto make=reinterpret_cast(GetProcAddress(lib,"Direct3DCreate9"));CHECK(make);auto factory=make(D3D_SDK_VERSION);CHECK(factory); + HWND w=CreateWindowA("STATIC","lockrect-pool",WS_POPUP,0,0,64,64,nullptr,nullptr,GetModuleHandleA(nullptr),nullptr);CHECK(w); + D3DPRESENT_PARAMETERS pp{};pp.BackBufferWidth=pp.BackBufferHeight=32;pp.BackBufferFormat=D3DFMT_A8R8G8B8;pp.BackBufferCount=1;pp.SwapEffect=D3DSWAPEFFECT_DISCARD;pp.Windowed=TRUE;pp.hDeviceWindow=w;pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9* d=nullptr;CHECK(factory->CreateDevice(0,D3DDEVTYPE_HAL,w,D3DCREATE_HARDWARE_VERTEXPROCESSING,&pp,&d)==S_OK); + if(d){if(timed){Work(d,16,false,upload);BenchmarkThreadAffinity affinity;CHECK(affinity.valid());Work(d,iterations,true,upload);}else if(mode=="correlation" || mode=="correlation-allocate"){WaitCorrelation(d,mode=="correlation-allocate");CHECK(d->Reset(&pp)==S_OK);}else{Work(d,iterations,false,true);CHECK(d->Reset(&pp)==S_OK);Work(d,12,false,false);}CHECK(d->Release()==0);} + factory->Release();DestroyWindow(w); + if(!timed){using namespace command_observer;std::printf("driver primaryCalls=%u buffers=%u batches=%u resets=%u destroyed=%u maxSpare=%u errors=%u livePools=%u\n",calls,buffers,batches,resets,destroyed,maxSpare,errors,unsigned(pools.size()));CHECK(calls>0 && resets>0 && destroyed>0);CHECK(errors==0 && pools.empty() && owners.empty());} + } + FreeLibrary(lib);std::printf("checks=%u failures=%u\n",checks,failures);return failures?1:0; +} diff --git a/tests/lockrect-pool-test.ps1 b/tests/lockrect-pool-test.ps1 new file mode 100644 index 0000000..c226386 --- /dev/null +++ b/tests/lockrect-pool-test.ps1 @@ -0,0 +1,26 @@ +param([string]$Backend,[string]$OutputDirectory,[string]$LlvmMingwBin, + [ValidateSet('observe','regression','correlation','correlation-allocate','bench')][string]$Mode='regression', + [ValidateSet('upload-readback','readback')][string]$Workload='upload-readback', + [int]$Iterations=128,[string]$Probe,[switch]$BuildOnly) +$ErrorActionPreference='Stop' +$root=Split-Path -Parent $PSScriptRoot +if(!$Backend){$Backend=Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll'} +if(!$OutputDirectory){$OutputDirectory=Join-Path $root 'out/tests/lockrect-pool'} +New-Item -ItemType Directory -Force -Path $OutputDirectory|Out-Null +$OutputDirectory=(Resolve-Path -LiteralPath $OutputDirectory).Path +if(!$Probe){ + . (Join-Path $root 'tools/lib/process-runner.ps1') + . (Join-Path $root 'tools/lib/toolchain-discovery.ps1') + $toolchain=Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin + $Probe=Join-Path $OutputDirectory 'lockrect-pool-probe.exe' + & $toolchain.LlvmMingw.CompilerPath '-std=c++17' '-O2' '-static' ('-I'+(Join-Path $root 'backend/dxvk/include/vulkan/include')) (Join-Path $PSScriptRoot 'lockrect-pool-probe.cpp') '-o' $Probe '-luuid' '-luser32' + if($LASTEXITCODE){throw "LockRect probe compilation failed ($LASTEXITCODE)"} +} +if($BuildOnly){return} +$env:DXVK_LOG_PATH=$OutputDirectory +$stdout=Join-Path $OutputDirectory "$Mode-$Workload.stdout.log" +$stderr=Join-Path $OutputDirectory "$Mode-$Workload.stderr.log" +$p=Start-Process -FilePath (Resolve-Path -LiteralPath $Probe).Path -ArgumentList @(('"'+(Resolve-Path -LiteralPath $Backend).Path+'"'),$Mode,$Workload,$Iterations) -WorkingDirectory $OutputDirectory -WindowStyle Hidden -PassThru -RedirectStandardOutput $stdout -RedirectStandardError $stderr +try{if(!$p.WaitForExit(120000)){$p.Kill();$p.WaitForExit();$result=124}else{$result=$p.ExitCode}} +finally{Get-Content -LiteralPath $stdout;Get-Content -LiteralPath $stderr;$p.Dispose()} +exit $result diff --git a/tests/mapping-budget-probe.cpp b/tests/mapping-budget-probe.cpp new file mode 100644 index 0000000..4347013 --- /dev/null +++ b/tests/mapping-budget-probe.cpp @@ -0,0 +1,538 @@ +// Reuse the existing real upload/readback checks, rather than another pixel model. +#define main UploadMappingLegacyMain +#include "upload-mapping-probe.cpp" +#undef main +#include "../backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h" +#include +#include +#include +#include +#include +#include +#include + +namespace budget_probe { +using U64 = unsigned long long; +static U64 frequency; +static U64 Now() { LARGE_INTEGER q; QueryPerformanceCounter(&q); return U64(q.QuadPart); } +static U64 Ns(U64 ticks) { return ticks / frequency * 1000000000ull + ticks % frequency * 1000000000ull / frequency; } +struct OsCounters { U64 maps=0, unmaps=0, mapTicks=0, unmapTicks=0, burst=0, burstTicks=0, maxBurst=0, maxBurstTicks=0; }; +static OsCounters os; +static bool counting; +static std::atomic inspectRelease{false}; +static std::atomic watchRetirement{false}, heldRetirement{false}; +static HANDLE retirementReached, retirementGate, retirementFinished; +static std::atomic retiredOffThread{0}; +struct ReleaseView { U64 bytes=0, pages=0, resident=0, queryNs=0, unmapNs=0; }; +static std::vector releaseViews; +static std::atomic releaseQueryErrors{0}; +static std::mutex observationMutex; +// Track a real file-close barrier so asynchronous cleanup remains inside the +// lifetime timer. The sentinel is released last, on the same pool as textures. +using CloseMapping=decltype(&CloseHandle); +static CloseMapping originalClose; +static std::atomic barrierFile{nullptr}; +static HANDLE barrierEvent; +static bool findSentinelFile; +static HANDLE sentinelFile; +static std::atomic countWorker{false}; +static std::atomic workerCalls{0},workerTicks{0}; +static BOOL WINAPI CloseHook(HANDLE h){auto result=originalClose(h);if(result && h==barrierFile.load())SetEvent(barrierEvent);return result;} +using WorkingSetQuery = BOOL (WINAPI*)(HANDLE,PVOID,DWORD); +static ReleaseView InspectView(LPCVOID p) { + ReleaseView result;const auto begin=Now(); + static auto query=reinterpret_cast(GetProcAddress(GetModuleHandleA("kernel32.dll"),"K32QueryWorkingSetEx")); + MEMORY_BASIC_INFORMATION info{}; + if(!query || VirtualQuery(p,&info,sizeof(info))!=sizeof(info) || info.AllocationBase!=p){++releaseQueryErrors;return result;} + result.bytes=info.RegionSize;result.pages=(info.RegionSize+4095)/4096; + std::vector entries(result.pages); + for(size_t i=0;i(static_cast(p))+i*4096; + if(!query(GetCurrentProcess(),entries.data(),DWORD(entries.size()*sizeof(entries[0]))))++releaseQueryErrors; + else for(const auto& page:entries)result.resident+=page.VirtualAttributes.Valid; + result.queryNs=Ns(Now()-begin);return result; +} +using SetMemory = void* (__cdecl*)(void*, int, size_t); +static SetMemory originalSet; +static U64 zeroBytes; +static void* __cdecl SetHook(void* p, int value, size_t bytes) { + if (GetCurrentThreadId()==ownerThread && counting && value==0 && bytes>=65536) { + MEMORY_BASIC_INFORMATION info{}; + if (VirtualQuery(p,&info,sizeof(info))==sizeof(info) && info.Type==MEM_MAPPED) + zeroBytes+=bytes; + } + return originalSet(p,value,bytes); +} +static U64 nextCapture; +using CreateMapping = decltype(&CreateFileMappingA); +static CreateMapping originalCreateMapping; +static bool failNextCreateMapping; +static bool failNextMap; +using CreateWorker=decltype(&CreateThread); +static CreateWorker originalCreateWorker; +static bool failNextWorker; +static HANDLE WINAPI CreateWorkerHook(LPSECURITY_ATTRIBUTES s,SIZE_T n,LPTHREAD_START_ROUTINE p,LPVOID a,DWORD flags,LPDWORD id){ + if(GetCurrentThreadId()==ownerThread && failNextWorker){failNextWorker=false;SetLastError(ERROR_NOT_ENOUGH_MEMORY);return nullptr;} + return originalCreateWorker(s,n,p,a,flags,id); +} +static HANDLE WINAPI CreateMappingHook(HANDLE f,LPSECURITY_ATTRIBUTES s,DWORD p,DWORD h,DWORD l,LPCSTR n) { + if(GetCurrentThreadId()==ownerThread && failNextCreateMapping){failNextCreateMapping=false;SetLastError(ERROR_NOT_ENOUGH_MEMORY);return nullptr;} + return originalCreateMapping(f,s,p,h,l,n); +} +static LPVOID WINAPI MapHook(HANDLE f,DWORD a,DWORD h,DWORD l,SIZE_T n) { + if(GetCurrentThreadId()==ownerThread && failNextMap){failNextMap=false;SetLastError(ERROR_NOT_ENOUGH_MEMORY);return nullptr;} + const auto begin=Now(); auto p=originalMap(f,a,h,l,n); const auto end=Now(); + if(p && GetCurrentThreadId()==ownerThread && findSentinelFile)sentinelFile=f; + if(GetCurrentThreadId()==ownerThread && counting && p) { ++os.maps; os.mapTicks+=end-begin; } + return p; +} +static BOOL WINAPI UnmapHook(LPCVOID p) { + bool held=false; + if(watchRetirement.load() && GetCurrentThreadId()!=ownerThread){++retiredOffThread;bool expected=false;if(heldRetirement.compare_exchange_strong(expected,true)){held=true;SetEvent(retirementReached);WaitForSingleObject(retirementGate,5000);}} + const bool inspect=inspectRelease.load(); + ReleaseView view;if(inspect)view=InspectView(p); + const auto begin=Now(); auto r=originalUnmap(p); const auto end=Now(); + if(inspect){view.unmapNs=Ns(end-begin);std::lock_guard lock(observationMutex);releaseViews.push_back(view);} + if(GetCurrentThreadId()==ownerThread && counting && r) { + ++os.unmaps; os.unmapTicks+=end-begin; ++os.burst; os.burstTicks+=end-begin; + os.maxBurst=std::max(os.maxBurst,os.burst); os.maxBurstTicks=std::max(os.maxBurstTicks,os.burstTicks); + } + if(r && GetCurrentThreadId()!=ownerThread && countWorker.load()){++workerCalls;workerTicks+=end-begin;} + if(held)SetEvent(retirementFinished); + return r; +} +static void Operation() { os.burst=os.burstTicks=0; } +// The observation pass consumes completed outer allocator spans. OS events are +// nested and must not contribute a second copy of logical mapped bytes. +struct Residency { + ID3D9BridgeTelemetry* t=nullptr; U64 capture=0, mapped=0, peak=0; bool valid=true; + bool requireSyncNesting=false;unsigned deferredLogicalUnmaps=0; + struct Entry { U64 bytes; unsigned refs; }; + std::unordered_map live; + std::vector events; + explicit Residency(IDirect3DDevice9* d):events(65536) { CHECK(d->QueryInterface(IID_D3D9BridgeTelemetry,reinterpret_cast(&t))==S_OK); } + void Start() { + capture=++nextCapture; mapped=peak=0; valid=true; live.clear(); + D3D9BridgeTelemetryConfig c{}; c.StructSize=sizeof(c); c.Version=1; c.CaptureId=capture; c.MaxEvents=65536; + CHECK(t && t->StartCapture(&c)==S_OK); + } + void Poll() { + if(!t)return; + D3D9BridgeTelemetryRead r{};r.StructSize=sizeof(r);r.Version=1; + auto hr=t->ReadEvents(capture,&r,events.data(),unsigned(events.size())); + if(hr!=S_OK || r.Dropped || r.Filtered || r.LateDropped || r.Remaining)valid=false; + std::sort(events.begin(),events.begin()+r.Written,[](const auto& a,const auto& b){return a.EndNs=e.EndNs;}CHECK(contained);} + }else deferredLogicalUnmaps+=bool(e.Flags&D3D9_BRIDGE_TELEMETRY_DEFERRED); + } + if(!(e.Flags&D3D9_BRIDGE_TELEMETRY_FILE_BACKED) || (e.Flags&D3D9_BRIDGE_TELEMETRY_OS_MAPPING))continue; + if(e.Flags&D3D9_BRIDGE_TELEMETRY_FAILED){valid=false;continue;} + if(e.Kind==D3D9_BRIDGE_TELEMETRY_MAP) { + auto& a=live[e.Identity];a.bytes=e.Bytes;if(!a.refs++)mapped+=e.Bytes;peak=std::max(peak,mapped); + } else if(e.Kind==D3D9_BRIDGE_TELEMETRY_UNMAP) { + auto p=live.find(e.Identity);if(p==live.end() || !p->second.refs){valid=false;continue;} + if(!--p->second.refs){mapped-=p->second.bytes;live.erase(p);} + } + } + } + void Stop() { Poll(); + const auto deadline=GetTickCount64()+10000; + for(;;){D3D9BridgeTelemetryRead pending{};pending.StructSize=sizeof(pending);pending.Version=1; + CHECK(t->ReadEvents(capture,&pending,nullptr,0)==S_OK);if(!pending.InFlight)break; + if(GetTickCount64()>=deadline){CHECK(false);break;}SwitchToThread();Poll();} + CHECK(t->StopCapture(capture)==S_OK);Poll(); + D3D9BridgeTelemetryRead r{};r.StructSize=sizeof(r);r.Version=1; + if(t->ReadEvents(capture,&r,nullptr,0)!=S_OK || r.InFlight || r.InFlightAtStop || r.LateDropped || r.Dropped)valid=false; + CHECK(valid); } + ~Residency(){if(t)t->Release();} +}; +static IDirect3DTexture9* Create(IDirect3DDevice9* d,unsigned w=512,unsigned h=512,unsigned levels=1,D3DPOOL pool=D3DPOOL_SYSTEMMEM,void* data=nullptr) { + IDirect3DTexture9* p=nullptr;HANDLE initial=data; + CHECK(d->CreateTexture(w,h,levels,0,D3DFMT_A8R8G8B8,pool,&p,data?&initial:nullptr)==S_OK);return p; +} +static void Access(IDirect3DTexture9* p,DWORD value,bool write=true,unsigned level=0) { + D3DLOCKED_RECT r{};CHECK(p && p->LockRect(level,&r,nullptr,write?0:D3DLOCK_READONLY)==S_OK); + if(r.pBits){if(write)*static_cast(r.pBits)=value;else CHECK(*static_cast(r.pBits)==value);CHECK(p->UnlockRect(level)==S_OK);} +} +static void Release(std::vector& list) { for(auto p:list)if(p)p->Release();list.clear(); } +static void Retirement(IDirect3DDevice9* d,unsigned budget) { + if(!budget)return; + retirementReached=CreateEventA(nullptr,TRUE,FALSE,nullptr);retirementGate=CreateEventA(nullptr,TRUE,FALSE,nullptr); + retirementFinished=CreateEventA(nullptr,TRUE,FALSE,nullptr); + auto keeper=Create(d,8,8);Access(keeper,0xff998877); + std::vector workingSet;for(unsigned i=0;i<7;++i)workingSet.push_back(Create(d)); + auto p=Create(d,512,512);Fill(p,2);D3DLOCKED_RECT a{};CHECK(p->LockRect(0,&a,nullptr,D3DLOCK_READONLY)==S_OK);auto old=a.pBits;CHECK(p->UnlockRect(0)==S_OK); + heldRetirement=false;retiredOffThread=0;watchRetirement=true;p->Release(); + const auto reached=WaitForSingleObject(retirementReached,500);CHECK(reached==WAIT_OBJECT_0); + if(reached==WAIT_OBJECT_0){MEMORY_BASIC_INFORMATION info{};CHECK(VirtualQuery(old,&info,sizeof(info))==sizeof(info) && info.State==MEM_COMMIT);Access(keeper,0xff998877,false); + // The retired view may alias a recycled file range. It is unreachable by + // the application; unmapping it must not invalidate the new mapping. + auto replacement=Create(d,512,512);Fill(replacement,3);SetEvent(retirementGate); + auto gpu=Create(d,512,512,1,D3DPOOL_DEFAULT);CHECK(d->UpdateTexture(replacement,gpu)==S_OK);Pixels(d,gpu,3);gpu->Release();replacement->Release(); + } else SetEvent(retirementGate); + if(reached==WAIT_OBJECT_0)CHECK(WaitForSingleObject(retirementFinished,5000)==WAIT_OBJECT_0); + watchRetirement=false;Release(workingSet);keeper->Release();Flush(d); + CloseHandle(retirementGate);CloseHandle(retirementReached);CloseHandle(retirementFinished); + std::printf("retirement offThread=%u held=%u\n",retiredOffThread.load(),unsigned(heldRetirement.load())); +} +static void RetirementFailure(IDirect3DDevice9* d) { + Residency r(d);r.Start();r.requireSyncNesting=true; + std::vector workingSet;for(unsigned i=0;i<7;++i)workingSet.push_back(Create(d)); + auto p=Create(d);Fill(p,3);failNextWorker=true;os={};counting=true;p->Release();counting=false; + CHECK(!failNextWorker);CHECK(os.unmaps==1);failNextWorker=false; + Release(workingSet); + p=Create(d);Fill(p,4);auto gpu=Create(d,512,512,1,D3DPOOL_DEFAULT);CHECK(d->UpdateTexture(p,gpu)==S_OK);Pixels(d,gpu,4);gpu->Release();p->Release();Flush(d);r.Stop();CHECK(r.deferredLogicalUnmaps==0); +} +static void RetirementPressure(IDirect3DDevice9* d,bool retry) { + Residency r(d);r.Start(); + retirementReached=CreateEventA(nullptr,TRUE,FALSE,nullptr);retirementGate=CreateEventA(nullptr,TRUE,FALSE,nullptr);retirementFinished=CreateEventA(nullptr,TRUE,FALSE,nullptr); + std::vector hot;for(unsigned i=0;i<7;++i){auto p=Create(d);Access(p,0xff000000|i);hot.push_back(p);} + auto victim=Create(d);Fill(victim,2);heldRetirement=false;watchRetirement=true;victim->Release(); + const auto reached=WaitForSingleObject(retirementReached,500);CHECK(reached==WAIT_OBJECT_0); + if(reached==WAIT_OBJECT_0){ + D3D9BridgeTelemetryRead pending{};pending.StructSize=sizeof(pending);pending.Version=1;CHECK(r.t->ReadEvents(r.capture,&pending,nullptr,0)==S_OK);CHECK(pending.InFlight>0); + // Pending view + new two-MiB view crosses the ten-MiB budget. The pending + // view must be drained before any still useful hot mapping is evicted. + failNextMap=retry; + std::thread unblock([]{Sleep(60);SetEvent(retirementGate);});auto begin=Now();auto incoming=Create(d,1024,512);auto elapsed=Ns(Now()-begin);unblock.join();CHECK(!failNextMap);failNextMap=false; + CHECK(elapsed>=30000000);CHECK(WaitForSingleObject(retirementFinished,5000)==WAIT_OBJECT_0); + os={};counting=true;for(unsigned i=0;iUpdateTexture(incoming,gpu)==S_OK);Pixels(d,gpu,3);gpu->Release();incoming->Release();} + }else SetEvent(retirementGate); + watchRetirement=false;Release(hot);r.Stop();CHECK(r.mapped==0);Flush(d); + CloseHandle(retirementGate);CloseHandle(retirementReached);CloseHandle(retirementFinished); +} +static void RetirementTeardown(IDirect3DDevice9*& d,bool capacity) { + retirementReached=CreateEventA(nullptr,TRUE,FALSE,nullptr);retirementGate=CreateEventA(nullptr,TRUE,FALSE,nullptr);retirementFinished=CreateEventA(nullptr,TRUE,FALSE,nullptr); + std::vector textures;const unsigned count=capacity?128:8; + for(unsigned i=0;iRelease();textures[0]=nullptr; + CHECK(WaitForSingleObject(retirementReached,500)==WAIT_OBJECT_0); + std::atomic released{1},beforeSignal{0}; + std::thread unblock([&]{Sleep(60);beforeSignal=released.load();SetEvent(retirementGate);}); + auto begin=Now();for(unsigned i=1;iRelease();++released;}auto releaseEnd=Now(); + CHECK(d->Release()==0);auto destroyed=Now();d=nullptr;unblock.join(); + CHECK(WaitForSingleObject(retirementFinished,5000)==WAIT_OBJECT_0);watchRetirement=false; + if(capacity){CHECK(beforeSignal>1 && beforeSignal=30000000);} + else {CHECK(beforeSignal==count);CHECK(Ns(destroyed-begin)>=30000000);} + std::printf("retirement-teardown capacity=%u releasedBeforeSignal=%u total=%u\n",unsigned(capacity),beforeSignal.load(),count); + CloseHandle(retirementGate);CloseHandle(retirementReached);CloseHandle(retirementFinished); +} +static void BudgetCheck(Residency& r,unsigned budget) {r.Poll();if(budget)CHECK(r.mapped<=U64(budget)*1048576);} + +static void Edge(IDirect3DDevice9* d,unsigned budget) { + Residency r(d);r.Start();std::vector sources; + // Every 512-square allocation is 1 MiB + 64 bytes. A final skinny + // texture fills the exact logical remainder, including padding. + if(!budget || budget>200){auto p=Create(d);Access(p,0xff123456);p->Release();r.Stop();CHECK(r.mapped==0);return;} + for(unsigned i=0;i1) { + auto incoming=Create(d);sources.push_back(incoming);Access(incoming,0xffabcdef);r.Poll(); + CHECK(r.mapped<=U64(budget)*1048576);CHECK(r.mapped>U64(budget)*1048576*3/4); + os={};counting=true;Access(sources[1],0xff000001,false);counting=false; + std::printf("edge stage=survivor maps=%llu mapped=%llu\n",os.maps,r.mapped);CHECK(os.maps==0); + Fill(exact,3);auto gpu=Create(d,16,16384-budget,1,D3DPOOL_DEFAULT);CHECK(d->UpdateTexture(exact,gpu)==S_OK);Pixels(d,gpu,3);gpu->Release(); + } + Release(sources);r.Stop();CHECK(r.mapped==0);Flush(d); +} +static void LockedAndPages(IDirect3DDevice9* d,unsigned budget) { + Residency r(d);r.Start(); + const unsigned side=budget==1?1024:512; + auto old=Create(d,side,side,0);Fill(old,1); + D3DLOCKED_RECT a{},b{};CHECK(old->LockRect(0,&a,nullptr,0)==S_OK);CHECK(old->LockRect(1,&b,nullptr,0)==S_OK); + r.Poll();const auto pinned=r.mapped; + std::vector pressure; + for(unsigned i=0;i<(budget?budget+3:3);++i){auto p=Create(d);pressure.push_back(p);Access(p,i);*static_cast(a.pBits)=Color(0,1);*static_cast(b.pBits)=Color(1,1);r.Poll();CHECK(r.mapped>=pinned);} + CHECK(old->UnlockRect(0)==S_OK);r.Poll();CHECK(r.mapped>=pinned);*static_cast(b.pBits)=Color(1,1);CHECK(old->UnlockRect(1)==S_OK);BudgetCheck(r,budget); + auto gpu=Create(d,side,side,0,D3DPOOL_DEFAULT);CHECK(d->UpdateTexture(old,gpu)==S_OK);Pixels(d,gpu,1);gpu->Release();old->Release();Release(pressure);r.Poll();CHECK(r.mapped==0); + auto x=Create(d,8,8), y=Create(d,8,8);D3DLOCKED_RECT lx{},ly{}; + CHECK(x->LockRect(0,&lx,nullptr,0)==S_OK);CHECK(y->LockRect(0,&ly,nullptr,0)==S_OK); + MEMORY_BASIC_INFORMATION mx{},my{};CHECK(VirtualQuery(lx.pBits,&mx,sizeof(mx))==sizeof(mx));CHECK(VirtualQuery(ly.pBits,&my,sizeof(my))==sizeof(my)); + if(budget)CHECK(mx.AllocationBase==my.AllocationBase); + *static_cast(lx.pBits)=0xff112233;*static_cast(ly.pBits)=0xff223344; + CHECK(x->UnlockRect(0)==S_OK);CHECK(y->UnlockRect(0)==S_OK); + os={};counting=true;x->Release();counting=false;if(budget)CHECK(os.unmaps==0);Access(y,0xff223344,false);y->Release(); + auto cross=Create(d,257,128);Fill(cross,2);gpu=Create(d,257,128,1,D3DPOOL_DEFAULT);CHECK(d->UpdateTexture(cross,gpu)==S_OK);Pixels(d,gpu,2);gpu->Release();cross->Release(); + if(budget) { + std::vector allPinned;std::vector locks; + for(unsigned i=0;iLockRect(0,&a,nullptr,0)==S_OK);allPinned.push_back(p);locks.push_back(a);} + r.Poll();CHECK(r.mapped>U64(budget)*1048576); + // Every mapped allocation is pinned; a normal reclamation entry must + // return with these pointers intact, even though the budget is exceeded. + for(unsigned i=0;i(locks[i].pBits)=0xffa00000|i; + for(unsigned i=0;i(locks[i].pBits)==(0xffa00000|i));CHECK(allPinned[i]->UnlockRect(0)==S_OK);r.Poll();} + BudgetCheck(r,budget);Release(allPinned); + } + r.Stop();CHECK(r.mapped==0);Flush(d); +} +static void Draw(IDirect3DDevice9* d,IDirect3DTexture9* texture); +static void SolidPixels(IDirect3DDevice9* d,IDirect3DTexture9* texture,DWORD expected) { + Draw(d,texture);IDirect3DSurface9 *rt=nullptr,*copy=nullptr; + CHECK(d->GetRenderTarget(0,&rt)==S_OK);CHECK(d->CreateOffscreenPlainSurface(32,32,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,©,nullptr)==S_OK);CHECK(d->GetRenderTargetData(rt,copy)==S_OK); + D3DLOCKED_RECT a{};CHECK(copy->LockRect(&a,nullptr,D3DLOCK_READONLY)==S_OK);if(a.pBits){CHECK(*reinterpret_cast(static_cast(a.pBits)+8*a.Pitch+32)==expected);CHECK(copy->UnlockRect()==S_OK);}copy->Release();rt->Release(); +} +static void RowStride(IDirect3DDevice9* d) { + IDirect3DTexture9 *p=nullptr,*gpu=nullptr; + CHECK(d->CreateTexture(3,7,1,0,D3DFMT_R5G6B5,D3DPOOL_SYSTEMMEM,&p,nullptr)==S_OK); + if(!p)return; + D3DLOCKED_RECT a{};CHECK(p->LockRect(0,&a,nullptr,0)==S_OK); + if(a.pBits){CHECK(a.Pitch>=6);for(unsigned y=0;y<7;++y){auto row=reinterpret_cast(static_cast(a.pBits)+y*a.Pitch);std::fill_n(row,3,0xf81f);if(a.Pitch>=8)row[3]=0x07e0;}CHECK(p->UnlockRect(0)==S_OK);} + CHECK(p->LockRect(0,&a,nullptr,D3DLOCK_READONLY)==S_OK); + if(a.pBits){for(unsigned y=0;y<7;++y)for(unsigned x=0;x<3;++x)CHECK(reinterpret_cast(static_cast(a.pBits)+y*a.Pitch)[x]==0xf81f);CHECK(p->UnlockRect(0)==S_OK);} + CHECK(d->CreateTexture(3,7,1,0,D3DFMT_R5G6B5,D3DPOOL_DEFAULT,&gpu,nullptr)==S_OK);CHECK(d->UpdateTexture(p,gpu)==S_OK);Draw(d,gpu); + IDirect3DSurface9 *rt=nullptr,*copy=nullptr;CHECK(d->GetRenderTarget(0,&rt)==S_OK);CHECK(d->CreateOffscreenPlainSurface(32,32,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,©,nullptr)==S_OK);CHECK(d->GetRenderTargetData(rt,copy)==S_OK);a={};CHECK(copy->LockRect(&a,nullptr,D3DLOCK_READONLY)==S_OK);if(a.pBits){CHECK(*reinterpret_cast(static_cast(a.pBits)+8*a.Pitch+32)==0xffff00ff);CHECK(copy->UnlockRect()==S_OK);}copy->Release();rt->Release();gpu->Release();p->Release(); +} +static void Initializer(IDirect3DDevice9* d,unsigned budget) { + RowStride(d); + Residency r(d);r.Start(); + for(auto pool:{D3DPOOL_SYSTEMMEM,D3DPOOL_MANAGED,D3DPOOL_SCRATCH}) { + os={};counting=true;auto p=Create(d,256,256,0,pool);counting=false; + std::printf("initializer pool=%u createMaps=%llu createUnmaps=%llu\n",unsigned(pool),os.maps,os.unmaps); + if(budget){CHECK(os.maps>=1);CHECK(os.unmaps==0);} + os={};counting=true; + for(unsigned level=0;levelGetLevelCount();++level){D3DLOCKED_RECT a{};CHECK(p->LockRect(level,&a,nullptr,D3DLOCK_READONLY)==S_OK);D3DSURFACE_DESC desc{};p->GetLevelDesc(level,&desc);if(a.pBits){for(unsigned y=0;y(static_cast(a.pBits)+a.Pitch*y)[x]==0);CHECK(p->UnlockRect(level)==S_OK);}} + counting=false;if(budget)CHECK(os.maps==0); + Fill(p,2);if(pool!=D3DPOOL_SCRATCH){auto gpu=Create(d,256,256,0,D3DPOOL_DEFAULT);if(pool==D3DPOOL_MANAGED)Pixels(d,p,2);else{CHECK(d->UpdateTexture(p,gpu)==S_OK);Pixels(d,gpu,2);}gpu->Release();}p->Release();r.Poll();CHECK(r.mapped==0); + } + IDirect3DCubeTexture9* cube=nullptr;os={};counting=true; + CHECK(d->CreateCubeTexture(64,0,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&cube,nullptr)==S_OK);counting=false; + if(budget)CHECK(os.maps>=1 && !os.unmaps);os={};counting=true; + if(cube){for(unsigned face=0;face<6;++face)for(unsigned mip=0;mipGetLevelCount();++mip){D3DSURFACE_DESC desc{};CHECK(cube->GetLevelDesc(mip,&desc)==S_OK);D3DLOCKED_RECT a{};CHECK(cube->LockRect(D3DCUBEMAP_FACES(face),mip,&a,nullptr,D3DLOCK_READONLY)==S_OK);if(a.pBits){for(unsigned y=0;y(static_cast(a.pBits)+y*a.Pitch)[x]==0);CHECK(cube->UnlockRect(D3DCUBEMAP_FACES(face),mip)==S_OK);}}counting=false;if(budget)CHECK(!os.maps);cube->Release();}counting=false;r.Poll();CHECK(r.mapped==0); + IDirect3DVolumeTexture9* volume=nullptr; + CHECK(d->CreateVolumeTexture(64,33,7,0,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&volume,nullptr)==S_OK); + if(volume){for(unsigned mip=0;mipGetLevelCount();++mip){D3DVOLUME_DESC desc{};CHECK(volume->GetLevelDesc(mip,&desc)==S_OK);D3DLOCKED_BOX a{};CHECK(volume->LockBox(mip,&a,nullptr,D3DLOCK_READONLY)==S_OK);bool zero=a.pBits!=nullptr;if(a.pBits){for(unsigned z=0;z(static_cast(a.pBits)+z*a.SlicePitch+y*a.RowPitch)[x]==0;CHECK(volume->UnlockBox(mip)==S_OK);}CHECK(zero);}volume->Release();}r.Poll();CHECK(r.mapped==0); + std::vector data(17*19,Color(0,4));auto p=Create(d,17,19,1,D3DPOOL_SYSTEMMEM,data.data());Access(p,Color(0,4),false);auto gpu=Create(d,17,19,1,D3DPOOL_DEFAULT);CHECK(d->UpdateTexture(p,gpu)==S_OK);Pixels(d,gpu,4);gpu->Release();p->Release(); + HANDLE initial=nullptr; + IDirect3DSurface9 *surface=nullptr,*gpuSurface=nullptr;initial=data.data(); + CHECK(d->CreateOffscreenPlainSurface(17,19,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&surface,&initial)==S_OK); + if(surface){D3DLOCKED_RECT a{};CHECK(surface->LockRect(&a,nullptr,D3DLOCK_READONLY)==S_OK);if(a.pBits){for(unsigned y=0;y<19;++y)for(unsigned x=0;x<17;++x)CHECK(reinterpret_cast(static_cast(a.pBits)+y*a.Pitch)[x]==Color(0,4));CHECK(surface->UnlockRect()==S_OK);} + gpu=Create(d,17,19,1,D3DPOOL_DEFAULT);CHECK(gpu->GetSurfaceLevel(0,&gpuSurface)==S_OK);CHECK(d->UpdateSurface(surface,nullptr,gpuSurface,nullptr)==S_OK);Pixels(d,gpu,4);gpuSurface->Release();gpu->Release();surface->Release();} + // Create-only and old locked source: no untracked initialized mappings. + auto old=Create(d,256,256);D3DLOCKED_RECT locked{};CHECK(old->LockRect(0,&locked,nullptr,0)==S_OK); + std::vector unused; + for(unsigned i=0;i<(budget?budget+8:8);++i){unused.push_back(Create(d));*static_cast(locked.pBits)=0xff987654;BudgetCheck(r,budget);} + CHECK(old->UnlockRect(0)==S_OK);Access(old,0xff987654,false);Release(unused);old->Release();r.Poll(); + if(budget>=4){std::vector hot,cold; + for(unsigned i=0;iUpdateTexture(hot.front(),gpu)==S_OK);Pixels(d,gpu,5);gpu->Release();BudgetCheck(r,budget);Release(cold);Release(hot);} + r.Stop();CHECK(r.mapped==0);Flush(d); +} +static void Concurrent(IDirect3DDevice9* d,unsigned budget) { + std::atomic errors{0};std::atomic go{false}; + IDirect3DTexture9* last[2]{}; + auto worker=[&](unsigned slot){const unsigned tag=slot?0x2200:0x1100;while(!go.load())std::this_thread::yield();for(unsigned i=0;i<48;++i){IDirect3DTexture9 *s=nullptr,*g=nullptr;unsigned side=budget==1?768:512; + if(d->CreateTexture(side,side,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&s,nullptr)!=S_OK){++errors;continue;} + D3DLOCKED_RECT a{};if(s->LockRect(0,&a,nullptr,0)==S_OK){for(unsigned y=0;y(static_cast(a.pBits)+y*a.Pitch),side,0xff000000|tag|i);s->UnlockRect(0);}else ++errors; + if(s->LockRect(0,&a,nullptr,D3DLOCK_READONLY)==S_OK){if(*static_cast(a.pBits)!=(0xff000000|tag|i))++errors;s->UnlockRect(0);}else ++errors; + if(d->CreateTexture(side,side,1,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&g,nullptr)==S_OK){if(d->UpdateTexture(s,g)!=S_OK)++errors;if(i==47)last[slot]=g;else g->Release();}else ++errors;s->Release();}}; + std::thread a(worker,0),b(worker,1);go=true;a.join();b.join();CHECK(errors==0);Flush(d); + for(unsigned slot=0;slot<2;++slot){CHECK(last[slot]);if(last[slot]){SolidPixels(d,last[slot],0xff000000|(slot?0x2200:0x1100)|47);last[slot]->Release();}}Flush(d); +} +static void AllocationFailure(IDirect3DDevice9* d,unsigned budget) { + if(!budget)return; + // All earlier file allocations have been released, so the next source must + // acquire a fresh chunk at the existing MemoryFile creation boundary. + IDirect3DTexture9* p=nullptr;failNextCreateMapping=true; + auto hr=d->CreateTexture(512,512,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&p,nullptr); + std::printf("allocation-failure hr=0x%08lx injected=%u null=%u\n",static_cast(hr),unsigned(!failNextCreateMapping),unsigned(p==nullptr)); + CHECK(!failNextCreateMapping);CHECK(hr==D3DERR_OUTOFVIDEOMEMORY && !p);failNextCreateMapping=false;if(p)p->Release(); + p=Create(d);Access(p,0xff123456);Access(p,0xff123456,false);if(p)p->Release();Flush(d); +} +static void FreshZero(IDirect3DDevice9* d,unsigned budget) { + if (!budget) return; + // Keep the backing chunk alive, then force reuse of a previously written + // range. Fresh storage and recycled storage must have identical visible bytes. + auto keeper=Create(d,8,8);Access(keeper,0xff123456); + zeroBytes=0;counting=true;auto p=Create(d,256,256);counting=false; + const auto freshBytes=zeroBytes; + Access(p,0,false); + D3DLOCKED_RECT a{};CHECK(p->LockRect(0,&a,nullptr,0)==S_OK); + if(a.pBits){for(unsigned y=0;y<256;++y)std::fill_n(reinterpret_cast(static_cast(a.pBits)+y*a.Pitch),256,0xffabcdef);CHECK(p->UnlockRect(0)==S_OK);} + p->Release(); + zeroBytes=0;counting=true;p=Create(d,256,256);counting=false; + const auto recycledBytes=zeroBytes; + CHECK(p->LockRect(0,&a,nullptr,D3DLOCK_READONLY)==S_OK); + bool zero=true;if(a.pBits){for(unsigned y=0;y<256;++y)for(unsigned x=0;x<256;++x)zero &= reinterpret_cast(static_cast(a.pBits)+y*a.Pitch)[x]==0;CHECK(p->UnlockRect(0)==S_OK);}else zero=false; + CHECK(zero);Access(keeper,0xff123456,false); + // A partial first write must leave all untouched bytes at zero. + RECT rect{7,9,13,15};CHECK(p->LockRect(0,&a,&rect,0)==S_OK); + if(a.pBits){for(unsigned y=0;y<6;++y)std::fill_n(reinterpret_cast(static_cast(a.pBits)+y*a.Pitch),6,0xff987654);CHECK(p->UnlockRect(0)==S_OK);} + CHECK(p->LockRect(0,&a,nullptr,D3DLOCK_READONLY)==S_OK);bool partial=true; + if(a.pBits){for(unsigned y=0;y<256;++y)for(unsigned x=0;x<256;++x)partial &= reinterpret_cast(static_cast(a.pBits)+y*a.Pitch)[x]==(x>=7 && x<13 && y>=9 && y<15?0xff987654u:0u);CHECK(p->UnlockRect(0)==S_OK);}else partial=false; + CHECK(partial);Fill(p,3);auto gpu=Create(d,256,256,1,D3DPOOL_DEFAULT);CHECK(d->UpdateTexture(p,gpu)==S_OK);Pixels(d,gpu,3);gpu->Release();p->Release();keeper->Release();Flush(d); + std::printf("fresh-zero freshBytes=%llu recycledBytes=%llu zero=%u partial=%u\n",freshBytes,recycledBytes,unsigned(zero),unsigned(partial)); + CHECK(freshBytes==0);CHECK(recycledBytes>=256*256*4); +} +static void UnusedAdmission(IDirect3DDevice9* d,unsigned budget) { + if(budget<4)return; + Residency r(d);r.Start();std::vector hot,unused; + for(unsigned i=0;iUpdateTexture(incoming,gpu)==S_OK);Pixels(d,gpu,4);gpu->Release();incoming->Release();Release(unused);Release(hot);r.Stop();CHECK(r.mapped==0);Flush(d); +} +static void MappingFailure(IDirect3DDevice9* d,unsigned budget) { + if(budget<4)return; + IDirect3DTexture9* p=nullptr;failNextMap=true; + auto hr=d->CreateTexture(512,512,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&p,nullptr); + std::printf("mapping-failure hr=0x%08lx injected=%u null=%u\n",static_cast(hr),unsigned(!failNextMap),unsigned(p==nullptr)); + CHECK(!failNextMap);CHECK(hr==D3DERR_OUTOFVIDEOMEMORY && !p);failNextMap=false;if(p)p->Release(); + // Failed views must not consume budget after their texture is destroyed. + Edge(d,budget); + auto keeper=Create(d,8,8);Access(keeper,0xff123456); + std::vector pressure; + for(unsigned i=0;iCreateTexture(8,8,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&p,nullptr); + CHECK(!failNextMap);CHECK(hr==D3DERR_OUTOFVIDEOMEMORY && !p);failNextMap=false;if(p)p->Release(); + Access(keeper,0xff123456,false);keeper->Release();Release(pressure);Edge(d,budget); +} +struct BenchResult {U64 releaseSubmit=0,total=0,cycles=0,present=0,drain=0,release=0,end=0,after=0,peak=0,operationBurst=0,operationBurstTicks=0,cleanupCalls=0,cleanupTicks=0;OsCounters calls;std::vector samples;unsigned failedPresents=0;bool valid=true,pixels=true,queryValid=true;}; +static void Draw(IDirect3DDevice9* d,IDirect3DTexture9* texture) { + CHECK(d->SetTexture(0,texture)==S_OK);CHECK(d->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1)==S_OK); + CHECK(d->SetRenderState(D3DRS_ZENABLE,FALSE)==S_OK);CHECK(d->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE)==S_OK); + CHECK(d->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1)==S_OK);CHECK(d->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE)==S_OK); + const Vertex v[]={{0,0,.5f,1,0,0},{64,0,.5f,1,1,0},{0,64,.5f,1,0,1}}; + CHECK(d->BeginScene()==S_OK);CHECK(d->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,v,sizeof(Vertex))==S_OK);CHECK(d->EndScene()==S_OK);CHECK(d->SetTexture(0,nullptr)==S_OK); +} +static BenchResult Batch(IDirect3DDevice9* d,unsigned budget,const std::string& workload,unsigned iterations,Residency* observation,bool measured) { + const unsigned before=failures;BenchResult result; result.samples.reserve(iterations); + // Validation infrastructure is outside the measured resource lifetime. Draw + // into an explicit target so Present's discard swap effect cannot destroy the + // final pixels. The target itself is released inside the lifetime timer. + IDirect3DSurface9 *back=nullptr,*target=nullptr,*readback=nullptr; + CHECK(d->GetRenderTarget(0,&back)==S_OK); + CHECK(d->CreateRenderTarget(32,32,D3DFMT_A8R8G8B8,D3DMULTISAMPLE_NONE,0,FALSE,&target,nullptr)==S_OK); + CHECK(d->CreateOffscreenPlainSurface(32,32,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&readback,nullptr)==S_OK); + // The readback resource is mapped before capture and never enters the file + // allocator after priming GetRenderTargetData's Vulkan readback buffer path. + CHECK(d->GetRenderTargetData(target,readback)==S_OK);Flush(d); + auto poll=[&]{if(observation)observation->Poll();}; + if(observation)observation->Start(); + // Shared setup is untimed for reuse workloads, but its complete release is + // inside the lifetime interval. Create/unused also create incoming inside it. + const unsigned setSize=workload=="below"?48:workload=="over"?116:workload=="admission"?97:88; + std::vector hot,incoming; + auto gpu=Create(d,512,512,1,D3DPOOL_DEFAULT); + const bool creating=workload=="create" || workload=="admission"; + if(workload!="create" && workload!="cold")for(unsigned i=0;iSetRenderTarget(0,target)==S_OK);poll(); + ULONG64 cyclesBegin=0,cyclesEnd=0;os={};workerCalls=0;workerTicks=0;counting=measured;countWorker=measured;CHECK(QueryThreadCycleTime(GetCurrentThread(),&cyclesBegin));const auto begin=Now(); + DWORD expectedColor=Color(0,1); + for(unsigned i=0;iRelease();incoming.erase(incoming.begin()); + Access(hot[i%hot.size()],Color(0,(i%hot.size())%4+1),false); + } + p=Create(d);Fill(p,version); + } + else { + // Hot workload retains at most twelve concurrent cold mappings, then reuses + // all hot entries in order. Unused deliberately admits never-used input. + if(workload=="unused" || (workload=="hot" && i%16==0)) { + auto cold=Create(d);incoming.push_back(cold);if(workload=="hot")Access(cold,0xff334455); + if(incoming.size()>12){incoming.front()->Release();incoming.erase(incoming.begin());} + } + p=hot[i%hot.size()];Access(p,expectedColor); + } + CHECK(d->UpdateTexture(p,gpu)==S_OK);Draw(d,gpu); + const auto presentBegin=Now();auto hr=d->Present(nullptr,nullptr,nullptr,nullptr);result.present+=Now()-presentBegin;if(hr!=S_OK)++result.failedPresents; + if(creating)p->Release(); + if(workload=="cold"){p->Release();hot[i]=nullptr;} + result.samples.push_back(Ns(Now()-op));poll(); + } + result.operationBurst=os.maxBurst;result.operationBurstTicks=os.maxBurstTicks; + // Establish a final real file-close barrier after the measured operations, so + // the sentinel cannot change chunk reuse or zero-initialization in the loop. + findSentinelFile=true;sentinelFile=nullptr;auto sentinel=Create(d,2,2);Access(sentinel,0xff567890);findSentinelFile=false; + CHECK(sentinelFile);ResetEvent(barrierEvent);barrierFile=sentinelFile;poll(); + // Copy the final actual workload draw before releasing its target. The copy + // and GPU synchronization remain in totalNs; CPU pixel inspection is outside. + CHECK(d->GetRenderTargetData(target,readback)==S_OK); + poll();if(observation)result.end=observation->mapped; + inspectRelease=observation && std::getenv("RS_RELEASE_RESIDENCY"); + if(inspectRelease){releaseViews.clear();releaseQueryErrors=0;} + Operation();const auto cleanupBeginCounters=os;const auto cleanupWorkerCalls=workerCalls.load(),cleanupWorkerTicks=workerTicks.load();const auto releaseBegin=Now();Release(incoming);Release(hot);gpu->Release();CHECK(d->SetRenderTarget(0,back)==S_OK);target->Release(); + sentinel->Release();result.releaseSubmit=Ns(Now()-releaseBegin); + CHECK(WaitForSingleObject(barrierEvent,10000)==WAIT_OBJECT_0);barrierFile=nullptr; + result.release=Now()-releaseBegin; + if(inspectRelease){U64 bytes=0,pages=0,resident=0,unmap=0,query=0;for(const auto& v:releaseViews){bytes+=v.bytes;pages+=v.pages;resident+=v.resident;unmap+=v.unmapNs;query+=v.queryNs;} + std::printf("{\"type\":\"release-residency-observation\",\"timedBenchmark\":false,\"views\":%u,\"bytes\":%llu,\"pages\":%llu,\"residentPages\":%llu,\"unmapNs\":%llu,\"queryNs\":%llu,\"queryErrors\":%u}\n",unsigned(releaseViews.size()),bytes,pages,resident,unmap,query,releaseQueryErrors.load());CHECK(releaseQueryErrors==0);} + inspectRelease=false; + const auto drainBegin=Now();const auto drainFailures=failures;Flush(d);result.queryValid=failures==drainFailures;result.drain=Now()-drainBegin;const auto finish=Now();CHECK(QueryThreadCycleTime(GetCurrentThread(),&cyclesEnd));counting=false;countWorker=false; + os.unmaps+=workerCalls.load();os.unmapTicks+=workerTicks.load(); + result.total=Ns(finish-begin);result.cycles=cyclesEnd-cyclesBegin;result.present=Ns(result.present);result.release=Ns(result.release);result.drain=Ns(result.drain);result.calls=os;result.cleanupCalls=os.unmaps-cleanupBeginCounters.unmaps-cleanupWorkerCalls;result.cleanupTicks=os.unmapTicks-cleanupBeginCounters.unmapTicks-cleanupWorkerTicks; + if(observation){observation->Stop();result.peak=observation->peak;result.after=observation->mapped;result.valid=observation->valid && !result.after;} + D3DLOCKED_RECT locked{};CHECK(readback->LockRect(&locked,nullptr,D3DLOCK_READONLY)==S_OK); + result.pixels=locked.pBits && *reinterpret_cast(static_cast(locked.pBits)+8*locked.Pitch+8*4)==expectedColor; + CHECK(result.pixels);if(locked.pBits)CHECK(readback->UnlockRect()==S_OK);readback->Release();back->Release();Flush(d); + result.valid=result.valid && failures==before && !result.failedPresents; + return result; +} +static void Bench(IDirect3DDevice9* d,unsigned budget,const std::string& workload,unsigned iterations) { + Batch(d,budget,workload,16,nullptr,false);Flush(d); + BenchResult time; + {BenchmarkThreadAffinity affinity;CHECK(affinity.valid());time=Batch(d,budget,workload,iterations,nullptr,true);} + Residency observation(d);auto resident=Batch(d,budget,workload,iterations,&observation,false); + time.pixels=time.pixels && resident.pixels;time.valid=time.valid && resident.valid && time.pixels; + std::sort(time.samples.begin(),time.samples.end());auto quantile=[&](unsigned n){return time.samples.empty()?0:time.samples[(time.samples.size()-1)*n/100];}; + std::printf("{\"type\":\"mapping-budget-benchmark\",\"affinityScope\":\"main-thread-only-lowest-allowed\",\"residencyPass\":\"separate-untimed\",\"queryValid\":%s,\"workload\":\"%s\",\"iterations\":%u,\"budgetMiB\":%u,\"valid\":%s,\"pixelsValid\":%s,\"failedPresents\":%u,\"totalNs\":%llu,\"mainThreadCycles\":%llu,\"p95Ns\":%llu,\"p99Ns\":%llu,\"maxNs\":%llu,\"presentNs\":%llu,\"drainNs\":%llu,\"releaseNs\":%llu,\"releaseSubmitNs\":%llu,\"mapCalls\":%llu,\"unmapCalls\":%llu,\"mapNs\":%llu,\"unmapNs\":%llu,\"maxUnmapBurst\":%llu,\"maxUnmapBurstNs\":%llu,\"operationMaxUnmapBurst\":%llu,\"operationMaxUnmapBurstNs\":%llu,\"cleanupUnmapCalls\":%llu,\"cleanupUnmapNs\":%llu,\"mappedPeakBytes\":%llu,\"mappedEndBytes\":%llu,\"mappedAfterReleaseBytes\":%llu}\n", + time.queryValid && resident.queryValid?"true":"false",workload.c_str(),iterations,budget,time.valid?"true":"false",time.pixels?"true":"false",time.failedPresents,time.total,time.cycles,quantile(95),quantile(99),quantile(100),time.present,time.drain,time.release,time.releaseSubmit,time.calls.maps,time.calls.unmaps,Ns(time.calls.mapTicks),Ns(time.calls.unmapTicks),time.calls.maxBurst,Ns(time.calls.maxBurstTicks),time.operationBurst,Ns(time.operationBurstTicks),time.cleanupCalls,Ns(time.cleanupTicks),resident.peak,resident.end,resident.after); + CHECK(time.valid); +} +} +int main(int argc,char** argv) { + using namespace budget_probe; + std::setvbuf(stdout,nullptr,_IONBF,0); + if(argc<4)return 2;const std::string mode=argv[2];const unsigned budget=std::strtoul(argv[3],nullptr,10); + if(mode=="bench" && argc!=6)return 2; + if(mode!="bench" && mode!="retirement" && mode!="retirement-failure" && mode!="retirement-pressure" && mode!="retirement-teardown" && mode!="retirement-cap" && mode!="retirement-retry" && mode!="regression" && mode!="initializer" && mode!="allocation-failure" && mode!="row-stride" && mode!="fresh-zero" && mode!="unused-admission" && mode!="mapping-failure")return 2; + if(mode=="bench") {const std::string w=argv[4];const unsigned n=std::strtoul(argv[5],nullptr,10);if(!n || n>1024 || (w!="below" && w!="hot" && w!="cold" && w!="over" && w!="create" && w!="unused" && w!="admission"))return 2;} + ownerThread=GetCurrentThreadId();LARGE_INTEGER f;QueryPerformanceFrequency(&f);frequency=f.QuadPart; + const std::string config="d3d9.textureMemory="+std::to_string(budget)+";d3d9.presentInterval=0;d3d9.gtaSaCompat=False;dxvk.hud="; + SetEnvironmentVariableA("DXVK_CONFIG",config.c_str());SetEnvironmentVariableA("DXVK_CONFIG_FILE","mapping-budget-no-external.conf");SetEnvironmentVariableA("DXVK_LOG_LEVEL","error");SetEnvironmentVariableA("DXVK_HUD","0"); + barrierEvent=CreateEventA(nullptr,TRUE,FALSE,nullptr);CHECK(barrierEvent); + HMODULE library=LoadLibraryA(argv[1]);CHECK(library);if(!library)return 1; + { + ImportHook mh,uh,ch,sh,clh,th;CHECK(mh.install(library,"MapViewOfFile",reinterpret_cast(&MapHook)));CHECK(uh.install(library,"UnmapViewOfFile",reinterpret_cast(&UnmapHook)));originalMap=reinterpret_cast(mh.original);originalUnmap=reinterpret_cast(uh.original); + if(mode=="retirement-failure"){CHECK(th.install(library,"CreateThread",reinterpret_cast(&CreateWorkerHook)));originalCreateWorker=reinterpret_cast(th.original);} + CHECK(clh.install(library,"CloseHandle",reinterpret_cast(&CloseHook)));originalClose=reinterpret_cast(clh.original); + if(mode=="fresh-zero"){CHECK(sh.install(library,"memset",reinterpret_cast(&SetHook)));originalSet=reinterpret_cast(sh.original);} + CHECK(ch.install(library,"CreateFileMappingA",reinterpret_cast(&CreateMappingHook)));originalCreateMapping=reinterpret_cast(ch.original); + using Factory=IDirect3D9* (WINAPI*)(UINT);auto make=reinterpret_cast(GetProcAddress(library,"Direct3DCreate9"));CHECK(make);auto factory=make(D3D_SDK_VERSION);CHECK(factory); + HWND w=CreateWindowA("STATIC","mapping-budget",WS_POPUP,0,0,64,64,nullptr,nullptr,GetModuleHandleA(nullptr),nullptr);CHECK(w);ShowWindow(w,SW_SHOWNOACTIVATE);UpdateWindow(w); + D3DPRESENT_PARAMETERS pp{};pp.BackBufferWidth=pp.BackBufferHeight=32;pp.BackBufferFormat=D3DFMT_A8R8G8B8;pp.BackBufferCount=1;pp.SwapEffect=D3DSWAPEFFECT_DISCARD;pp.Windowed=TRUE;pp.hDeviceWindow=w;pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9* d=nullptr;CHECK(factory->CreateDevice(0,D3DDEVTYPE_HAL,w,D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&pp,&d)==S_OK); + if(d){if(mode=="bench")Bench(d,budget,argv[4],std::strtoul(argv[5],nullptr,10));else if(mode=="retirement")Retirement(d,budget);else if(mode=="retirement-failure")RetirementFailure(d);else if(mode=="retirement-teardown" || mode=="retirement-cap")RetirementTeardown(d,mode=="retirement-cap");else if(mode=="retirement-pressure" || mode=="retirement-retry")RetirementPressure(d,mode=="retirement-retry");else if(mode=="initializer"){Initializer(d,budget);Concurrent(d,budget);}else if(mode=="allocation-failure")AllocationFailure(d,budget);else if(mode=="mapping-failure")MappingFailure(d,budget);else if(mode=="fresh-zero")FreshZero(d,budget);else if(mode=="unused-admission")UnusedAdmission(d,budget);else if(mode=="row-stride")RowStride(d);else{Edge(d,budget);LockedAndPages(d,budget); + IDirect3DDevice9* other=nullptr;CHECK(factory->CreateDevice(0,D3DDEVTYPE_HAL,w,D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&pp,&other)==S_OK);if(other){auto x=Create(d),y=Create(other);Access(x,1);Access(y,2);x->Release();Access(y,2,false);y->Release();Flush(other);CHECK(other->Release()==0);}} + if(d){CHECK(d->Reset(&pp)==S_OK);CHECK(d->Release()==0);}} + factory->Release();DestroyWindow(w); + } + FreeLibrary(library);CloseHandle(barrierEvent);std::printf("checks=%u failures=%u\n",checks,failures);return failures?1:0; +} diff --git a/tests/mapping-budget-test.ps1 b/tests/mapping-budget-test.ps1 new file mode 100644 index 0000000..33bfbc5 --- /dev/null +++ b/tests/mapping-budget-test.ps1 @@ -0,0 +1,43 @@ +param([string]$Backend, [string]$OutputDirectory, [string]$LlvmMingwBin, [string]$Ninja, [string]$Glslang, + [ValidateSet('regression','initializer','bench','lru','allocation-failure','row-stride','fresh-zero','unused-admission','mapping-failure','retirement','retirement-failure','retirement-pressure','retirement-retry','retirement-teardown','retirement-cap')][string]$Mode='regression', + [int]$BudgetMiB=100, + [ValidateSet('below','hot','cold','over','create','unused','admission')][string]$Workload='hot', + [int]$Iterations=512, [string]$Probe, [switch]$BuildOnly) +$ErrorActionPreference='Stop' +$root=Split-Path -Parent $PSScriptRoot +if(!$Backend){$Backend=Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll'} +if(!$OutputDirectory){$OutputDirectory=Join-Path $root 'out/perf/20261114-f8-tail-mapping/mapping-native'} +New-Item -ItemType Directory -Force -Path $OutputDirectory|Out-Null +$OutputDirectory=(Resolve-Path -LiteralPath $OutputDirectory).Path +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +if(!$Probe){ + $toolchain=Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin ` + -NinjaPath $Ninja -GlslangPath $Glslang + $source=if($Mode -eq 'lru'){'mapping-lru-tests.cpp'}else{'mapping-budget-probe.cpp'} + $Probe=Join-Path $OutputDirectory ([IO.Path]::GetFileNameWithoutExtension($source)+'.exe') + & $toolchain.LlvmMingw.CompilerPath '-std=c++17' '-O2' '-static' (Join-Path $PSScriptRoot $source) '-o' $Probe '-luuid' '-luser32' + if($LASTEXITCODE){throw "Mapping probe compilation failed ($LASTEXITCODE)"} +} +if($BuildOnly){return} +$Probe=(Resolve-Path -LiteralPath $Probe).Path +$arguments=@() +if($Mode -ne 'lru'){ + $libraryArgument='"'+(Resolve-Path -LiteralPath $Backend).Path+'"' + $arguments=@($libraryArgument,$Mode,[string]$BudgetMiB) +} +if($Mode -eq 'bench'){$arguments+=@($Workload,$Iterations)} +$env:DXVK_LOG_PATH=$OutputDirectory +$stdout=Join-Path $OutputDirectory "$Mode-$Workload-$BudgetMiB.stdout.log" +$stderr=Join-Path $OutputDirectory "$Mode-$Workload-$BudgetMiB.stderr.log" +$start=@{FilePath=$Probe;WorkingDirectory=$OutputDirectory;WindowStyle='Hidden';PassThru=$true;RedirectStandardOutput=$stdout;RedirectStandardError=$stderr} +if($arguments.Count){$start.ArgumentList=$arguments} +$process=Start-Process @start +try { + if(!$process.WaitForExit(120000)){$process.Kill();$process.WaitForExit();$result=124}else{$result=$process.ExitCode} +} finally { + Get-Content -LiteralPath $stdout + Get-Content -LiteralPath $stderr + $process.Dispose() +} +exit $result diff --git a/tests/mapping-lru-tests.cpp b/tests/mapping-lru-tests.cpp new file mode 100644 index 0000000..a0557e3 --- /dev/null +++ b/tests/mapping-lru-tests.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + +// Deterministic failure at the real standard containers' allocation boundary. +// No alternate LRU implementation or product allocator parameter is involved. +static long failAfter=-1, allocations=0, live=0; +void* operator new(std::size_t n) { + if(failAfter==0)throw std::bad_alloc(); + if(failAfter>0)--failAfter; + auto p=std::malloc(n?n:1);if(!p)throw std::bad_alloc();++allocations;++live;return p; +} +void operator delete(void* p) noexcept {if(p){--live;std::free(p);}} +void operator delete(void* p,std::size_t) noexcept {::operator delete(p);} +void* operator new[](std::size_t n){return ::operator new(n);} +void operator delete[](void* p) noexcept{::operator delete(p);} +void operator delete[](void* p,std::size_t) noexcept{::operator delete(p);} +#include "../backend/dxvk/src/util/util_lru.h" +static unsigned checks=0,failures=0; +#define CHECK(c) do{++checks;if(!(c)){++failures;std::printf("FAIL line=%u %s\n",__LINE__,#c);}}while(0) +static bool Order(const dxvk::lru_list& l,std::initializer_list expected) { + auto i=l.leastRecentlyUsedIter();for(auto v:expected){if(i==l.leastRecentlyUsedEndIter() || *i++!=v)return false;}return i==l.leastRecentlyUsedEndIter() && l.size()==expected.size(); +} +int main() { + for(long point=0;point<4;++point){const auto before=live;{ + dxvk::lru_list l;l.insert(1);l.insert(2);failAfter=point;bool failed=false; + try{l.insert(3,false);}catch(const std::bad_alloc&){failed=true;}failAfter=-1; + if(failed){CHECK(Order(l,{1,2}));l.insert(3,false);} + CHECK(Order(l,{3,1,2}));l.insert(1,false);CHECK(Order(l,{3,1,2}));l.touch(3);CHECK(Order(l,{1,2,3})); + }CHECK(live==before);} + unsigned injected=0,successful=0; + for(long point=0;point<8;++point){const auto before=live;{ + dxvk::lru_list l;l.insert(1);l.insert(2);failAfter=point;bool failed=false; + try{l.insert(3);}catch(const std::bad_alloc&){failed=true;++injected;}failAfter=-1; + if(failed){CHECK(Order(l,{1,2}));l.remove(3);CHECK(Order(l,{1,2}));l.insert(3);}else ++successful; + CHECK(Order(l,{1,2,3}));l.remove(3);l.remove(1);l.remove(2);CHECK(Order(l,{})); + }CHECK(live==before);} + CHECK(injected>=2 && successful>0); + { + dxvk::lru_list l;l.insert(1);l.insert(2);l.insert(3);const auto count=allocations; + failAfter=0;bool failed=false;try{l.insert(1);l.touch(2);l.touch(2);l.touch(99);}catch(const std::bad_alloc&){failed=true;}failAfter=-1; + CHECK(!failed);CHECK(allocations==count);CHECK(Order(l,{3,1,2})); + if(!failed){auto i=l.remove(l.leastRecentlyUsedIter());CHECK(*i==1);i=l.remove(i);CHECK(*i==2);l.remove(i);CHECK(Order(l,{}));} + } + CHECK(live==0);std::printf("checks=%u failures=%u injected=%u successful=%u live=%ld\n",checks,failures,injected,successful,live);return failures?1:0; +} diff --git a/tests/mapping-toolchain-forwarding-test.ps1 b/tests/mapping-toolchain-forwarding-test.ps1 new file mode 100644 index 0000000..8feabdb --- /dev/null +++ b/tests/mapping-toolchain-forwarding-test.ps1 @@ -0,0 +1,16 @@ +param( + [Parameter(Mandatory)] [string]$LlvmMingwBin, + [Parameter(Mandatory)] [string]$Ninja, + [Parameter(Mandatory)] [string]$Glslang, + [Parameter(Mandatory)] [string]$OutputDirectory +) + +$ErrorActionPreference = 'Stop' +$missingPrefix = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString()) +$env:SA_RENDERSTACK_NINJA = "$missingPrefix-ninja.exe" +$env:SA_RENDERSTACK_GLSLANG = "$missingPrefix-glslang.exe" + +& (Get-Command pwsh).Source -NoProfile -File (Join-Path $PSScriptRoot 'mapping-budget-test.ps1') ` + -Mode lru -OutputDirectory $OutputDirectory -LlvmMingwBin $LlvmMingwBin ` + -Ninja $Ninja -Glslang $Glslang +exit $LASTEXITCODE diff --git a/tests/measurement-coverage-benchmark.ps1 b/tests/measurement-coverage-benchmark.ps1 new file mode 100644 index 0000000..8a92adf --- /dev/null +++ b/tests/measurement-coverage-benchmark.ps1 @@ -0,0 +1,184 @@ +param( + [Parameter(Mandatory)] [ValidateSet('Backend','Runtime')] [string]$Mode, + [Parameter(Mandatory)] [string]$Candidate, + [string]$Baseline, + [Parameter(Mandatory)] [string]$Probe, + [Parameter(Mandatory)] [string]$OutputDirectory, + [ValidateRange(2,30)] [int]$Processes = 6, + [ValidateRange(2,16)] [int]$Rounds = 4, + [ValidateRange(1000,10000)] [int]$CpuMilliseconds = 1000 +) +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +if ($Mode -eq 'Backend' -and -not $Baseline) { throw 'Backend mode requires an immutable baseline DLL.' } +if (Test-Path -LiteralPath $OutputDirectory) { throw 'Use a new benchmark output directory.' } +$Candidate = (Resolve-Path -LiteralPath $Candidate).Path +$Probe = (Resolve-Path -LiteralPath $Probe).Path +if ($Baseline) { $Baseline = (Resolve-Path -LiteralPath $Baseline).Path } +New-Item -ItemType Directory -Path $OutputDirectory | Out-Null +$OutputDirectory = (Resolve-Path -LiteralPath $OutputDirectory).Path +$utf8 = [Text.UTF8Encoding]::new($false) +$binaries = @(@($Candidate,$Baseline,$Probe) | Where-Object { $_ } | Sort-Object -Unique | ForEach-Object { + $item = Get-Item -LiteralPath $_ + [ordered]@{ path=$item.FullName; sha256=(Get-FileHash -LiteralPath $_ -Algorithm SHA256).Hash; size=$item.Length } +}) +$rows = [Collections.Generic.List[object]]::new() +$started = [DateTime]::UtcNow.ToString('o') + +function Assert-Binaries { + foreach ($binary in $binaries) { + if ((Get-FileHash -LiteralPath $binary.path -Algorithm SHA256).Hash -cne $binary.sha256) { + throw "Binary changed during benchmark: $($binary.path)" + } + } +} + +function Invoke-Probe([string]$Name, [string[]]$Arguments) { + Assert-Binaries + $directory = Join-Path $OutputDirectory $Name + New-Item -ItemType Directory -Path $directory | Out-Null + $start = [Diagnostics.ProcessStartInfo]::new() + $start.FileName = $Probe + foreach ($argument in $Arguments) { [void]$start.ArgumentList.Add($argument) } + $start.WorkingDirectory = $directory + $start.UseShellExecute = $false + $start.CreateNoWindow = $true + $start.RedirectStandardOutput = $true + $start.RedirectStandardError = $true + $start.Environment['DXVK_CONFIG'] = 'd3d9.gtaSaCompat = True; d3d9.gtaSaCompatDiagnostics = False; dxvk.hud = 0' + $start.Environment['DXVK_HUD'] = '0' + $start.Environment['DXVK_LOG_PATH'] = $directory + [void]$start.Environment.Remove('DXVK_CONFIG_FILE') + [void]$start.Environment.Remove('DXVK_STATE_CACHE_PATH') + $process = [Diagnostics.Process]::new() + $process.StartInfo = $start + [void]$process.Start() + $stdout = $process.StandardOutput.ReadToEndAsync() + $stderr = $process.StandardError.ReadToEndAsync() + $limit = [Math]::Max(120000, $Rounds * ($CpuMilliseconds + 1500) * 4) + $timer = [Diagnostics.Stopwatch]::StartNew() + while (-not $process.WaitForExit(1000)) { + if ($timer.ElapsedMilliseconds -gt $limit) { + $process.Kill($true) + $process.WaitForExit() + [IO.File]::WriteAllText((Join-Path $directory 'stdout.log'), $stdout.GetAwaiter().GetResult(), $utf8) + [IO.File]::WriteAllText((Join-Path $directory 'stderr.log'), $stderr.GetAwaiter().GetResult(), $utf8) + throw "$Name timed out; evidence retained in $directory" + } + } + $text = $stdout.GetAwaiter().GetResult() + [IO.File]::WriteAllText((Join-Path $directory 'stdout.log'), $text, $utf8) + [IO.File]::WriteAllText((Join-Path $directory 'stderr.log'), $stderr.GetAwaiter().GetResult(), $utf8) + $exitCode = $process.ExitCode + $process.Dispose() + if ($exitCode -ne 0) { throw "$Name failed with exit $exitCode; see $directory" } + Assert-Binaries + @($text -split '\r?\n' | Where-Object { $_.StartsWith('{') } | ForEach-Object { $_ | ConvertFrom-Json }) +} + +function Add-Row($Row) { + $rows.Add($Row) + [IO.File]::AppendAllText((Join-Path $OutputDirectory 'samples.jsonl'), + (($Row | ConvertTo-Json -Depth 8 -Compress) + "`n"), $utf8) +} + +$metadata = [ordered]@{ + mode=$Mode; startedUtc=$started; binaries=$binaries; processes=$Processes; rounds=$Rounds + cpuMilliseconds=$CpuMilliseconds; scheduling='unmodified OS scheduling; runs sequentially' + ordering='AB/BA alternates; A/A control uses identical disabled collection' + interpretation='Actual Vulkan fixture overhead only; not a claim about game FPS' +} +[IO.File]::WriteAllText((Join-Path $OutputDirectory 'metadata.json'), ($metadata | ConvertTo-Json -Depth 8), $utf8) + +if ($Mode -eq 'Backend') { + for ($pair=0; $pair -lt $Processes; ++$pair) { + # Rotate suite order as well as binary order to reduce warm-up and thermal bias. + $suites = @('inactive','active','aa') + for ($slot=0; $slot -lt $suites.Count; ++$slot) { + $suite = $suites[($slot+$pair)%$suites.Count] + $order = if ($pair%2) { @('B','A') } else { @('A','B') } + foreach ($variant in $order) { + $dll = if ($suite -eq 'inactive' -and $variant -eq 'A') { $Baseline } else { $Candidate } + $collection = if ($suite -eq 'active' -and $variant -eq 'B') { 'on' } elseif ($suite -eq 'aa' -and $variant -eq 'B') { 'aa' } else { 'off' } + $name = '{0}-{1:D2}-{2}' -f $suite,$pair,$variant + $parsed = @(Invoke-Probe $name @($dll,'--benchmark',$collection)) + if ($parsed.Count -ne 1) { throw "$name expected one benchmark row, found $($parsed.Count)." } + $row = $parsed[0] + foreach ($field in @('wall_ns_per_iteration','thread_cycles_per_iteration')) { + if (-not $row.PSObject.Properties[$field] -or -not ([double]$row.$field -gt 0) -or + [double]::IsInfinity([double]$row.$field)) { throw "$name invalid $field" } + } + if ($row.failures -ne 0 -or $row.iterations -lt 1 -or $row.successful_presents -ne $row.iterations) { + throw "$name failed real workload or successful-Present checks." + } + if ($row.dropped -ne 0 -or $row.inflight -ne 0 -or + ($collection -eq 'on' -and ($row.supported -ne 1 -or $row.events -le 0))) { + throw "$name incomplete telemetry evidence." + } + $row | Add-Member -NotePropertyName suite -NotePropertyValue $suite + $row | Add-Member -NotePropertyName pair -NotePropertyValue $pair + $row | Add-Member -NotePropertyName label -NotePropertyValue $variant + $row | Add-Member -NotePropertyName order -NotePropertyValue ($order -join '') + Add-Row $row + Write-Output "$name complete" + } + } + } +} else { + for ($process=0; $process -lt $Processes; ++$process) { + $suites = if ($process%2) { @('aa','active') } else { @('active','aa') } + foreach ($suite in $suites) { + $arguments = @('--coverage-benchmark',$Candidate,[string]$Rounds,[string]$CpuMilliseconds) + if ($suite -eq 'aa') { $arguments += '--aa' } + $name = '{0}-{1:D2}' -f $suite,$process + $parsed = @(Invoke-Probe $name $arguments | Where-Object type -eq 'coverage-benchmark') + if ($parsed.Count -ne $Rounds*2*3) { throw "$name expected $($Rounds*6) phase rows, found $($parsed.Count)." } + $seen = [Collections.Generic.HashSet[string]]::new() + foreach ($row in $parsed) { + # The fixture truthfully labels both A/A configurations A. + # Keep that value, and use a separate comparison label by position. + $row | Add-Member -NotePropertyName configuredVariant -NotePropertyValue $row.variant + if ($suite -eq 'aa') { + if ($row.order -cne 'AA' -or $row.variant -cne 'A' -or $row.position -notin @(0,1) -or $row.collectionEnabled) { + throw "$name invalid A/A configuration" + } + $row.variant = if ($row.position -eq 0) { 'A' } else { 'B' } + } elseif ([bool]$row.collectionEnabled -ne ($row.variant -eq 'B')) { + throw "$name collection configuration differs from its comparison label" + } + $key = '{0}/{1}/{2}' -f $row.round,$row.variant,$row.phase + if (-not $seen.Add($key) -or $row.round -lt 0 -or $row.round -ge $Rounds -or + $row.variant -notin @('A','B') -or $row.phase -notin @('baseline','cpu','attribution') -or + -not ([double]$row.elapsedNs -gt 0) -or -not ([double]$row.presents -gt 0)) { + throw "$name invalid or repeated phase row $key" + } + if (-not $row.valid -or $row.failedPresents -ne 0 -or -not $row.cpuTimeAvailable) { + throw "$name incomplete real-workload or thread-CPU evidence: $key" + } + foreach ($field in @('workPresentTotalNs','presentTotalNs','maxWorkPresentNs','maxPresentNs', + 'completionPresentNs','completionWorkNs','cpuFinishingNs','cpuFinishingObserved','reportWaitNs','reportWasPending')) { + if (-not $row.PSObject.Properties[$field]) { throw "$name missing capture-tail field: $field" } + } + if ($row.iterations -le 0 -or $row.workPresentTotalNs -le 0 -or $row.presentTotalNs -le 0 -or + $row.mainSamples -le 0 -or $row.mainDropped -ne 0 -or $row.csDropped -ne 0 -or + $row.dropped -ne 0 -or $row.frameTraceDropped -ne 0) { throw "$name incomplete capture counts: $key" } + if ($row.collectionEnabled -and ($row.csSamples -le 0 -or $row.events -le 0 -or $row.memorySnapshots -lt 2)) { + throw "$name enabled coverage did not reach the report: $key" + } + if (-not $row.collectionEnabled -and ($row.csSamples -ne 0 -or $row.events -ne 0 -or $row.memorySnapshots -ne 0)) { + throw "$name disabled control collected extra coverage: $key" + } + $row | Add-Member -NotePropertyName suite -NotePropertyValue $suite + $row | Add-Member -NotePropertyName processIndex -NotePropertyValue $process + Add-Row $row + } + Write-Output "$name complete: $($parsed.Count) phase rows" + } + } +} +Assert-Binaries +$metadata.endedUtc = [DateTime]::UtcNow.ToString('o') +$metadata.rows = $rows.Count +[IO.File]::WriteAllText((Join-Path $OutputDirectory 'metadata.json'), ($metadata | ConvertTo-Json -Depth 8), $utf8) +& (Join-Path $PSScriptRoot 'measurement-coverage-summary.ps1') -InputDirectory $OutputDirectory +Write-Output "Benchmark artifacts: $OutputDirectory" diff --git a/tests/measurement-coverage-summary-test.ps1 b/tests/measurement-coverage-summary-test.ps1 new file mode 100644 index 0000000..42debaa --- /dev/null +++ b/tests/measurement-coverage-summary-test.ps1 @@ -0,0 +1,42 @@ +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$root = Split-Path -Parent $PSScriptRoot +$directory = Join-Path $root ('out/perf/20261112-measurement-coverage/summary-fixture-{0}-{1}' -f $PID,[DateTime]::UtcNow.Ticks) +New-Item -ItemType Directory -Path $directory | Out-Null +[IO.File]::WriteAllText((Join-Path $directory 'metadata.json'), '{"mode":"Runtime"}') +$rows = @( + foreach ($variant in @('A','B')) { + $sampledPresents = if ($variant -eq 'A') { 5 } else { 10 } + $cycles = if ($variant -eq 'A') { 200 } else { 400 } + $report = Join-Path $directory ($variant+'.log') + [IO.File]::WriteAllText($report, + "cpu captureId=1 samples=2 samplingElapsedNs=1000 sampledPresents=$sampledPresents`n") + foreach ($phase in @('baseline','cpu')) { + [ordered]@{ suite='active'; processIndex=0; round=0; variant=$variant; phase=$phase; + elapsedNs=2000; presents=20; mainCpuNs=0; csCpuNs=0; mainThreadCycles=$cycles; + mainSamples=2; reportPath=$report } + } + } +) +$rows | ForEach-Object { $_ | ConvertTo-Json -Compress } | Set-Content -LiteralPath (Join-Path $directory 'samples.jsonl') -Encoding utf8 +& (Join-Path $PSScriptRoot 'measurement-coverage-summary.ps1') -InputDirectory $directory +$result = Get-Content -LiteralPath (Join-Path $directory 'summary.json') -Raw | ConvertFrom-Json +$cycle = @($result.summary | Where-Object comparison -eq 'active, cpu, mainCyclesPerSampledPresent') +if ($cycle.Count -ne 1 -or $cycle[0].aMedian -ne 40 -or $cycle[0].bMedian -ne 40 -or + $cycle[0].medianProcessChangePercent -ne 0) { throw 'Sampler cycles were not normalized with the same-window Presents.' } +$cpu = @($result.summary | Where-Object { $_.comparison -like '*CpuNsPerPresent' }) +if ($cpu.Count -ne 4 -or @($cpu | Where-Object { $_.pairs -ne 1 -or $_.zeroAPairs -ne 1 -or + $_.zeroBPairs -ne 1 -or $null -ne $_.medianProcessChangePercent }).Count) { + throw 'Zero CPU accounting pairs were lost or assigned a fabricated relative percentage.' +} +if ($result.reports.Count -ne 2) { throw 'Same-window source reports and hashes are missing.' } +Write-Output 'PASS same-window cycle normalization and retained zero CPU accounting pairs' + +# A mismatched report must fail, rather than quietly normalizing a different capture. +[IO.File]::WriteAllText((Join-Path $directory 'B.log'), + "cpu captureId=1 samples=99 samplingElapsedNs=1000 sampledPresents=10`n") +$rejected = $false +try { & (Join-Path $PSScriptRoot 'measurement-coverage-summary.ps1') -InputDirectory $directory | Out-Null } +catch { $rejected = $_.Exception.Message -like '*sample count differs*' } +if (-not $rejected) { throw 'The summary accepted a mismatched source report.' } +Write-Output 'PASS mismatched capture evidence is rejected' diff --git a/tests/measurement-coverage-summary.ps1 b/tests/measurement-coverage-summary.ps1 new file mode 100644 index 0000000..574c85b --- /dev/null +++ b/tests/measurement-coverage-summary.ps1 @@ -0,0 +1,118 @@ +param([Parameter(Mandatory)] [string]$InputDirectory) +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$directory = (Resolve-Path -LiteralPath $InputDirectory).Path +$metadata = Get-Content -LiteralPath (Join-Path $directory 'metadata.json') -Raw | ConvertFrom-Json +$rows = @(Get-Content -LiteralPath (Join-Path $directory 'samples.jsonl') | ForEach-Object { $_ | ConvertFrom-Json }) +$utf8 = [Text.UTF8Encoding]::new($false) +$paired = [Collections.Generic.List[object]]::new() +$reports = [Collections.Generic.Dictionary[string,object]]::new() +function Median([object[]]$Values) { + $sorted = @($Values | Where-Object { $null -ne $_ } | ForEach-Object { [double]$_ } | Sort-Object) + if (-not $sorted.Count) { return $null } + $mid = [int][Math]::Floor($sorted.Count/2) + if ($sorted.Count%2) { return $sorted[$mid] } + return ($sorted[$mid-1]+$sorted[$mid])/2 +} +function Read-SamplerWindow($Row) { + if ($reports.ContainsKey($Row.reportPath)) { return $reports[$Row.reportPath] } + $found = $null + $reader = [IO.File]::OpenText($Row.reportPath) + try { while ($null -ne ($line = $reader.ReadLine())) { + if (-not $line.StartsWith('cpu captureId=')) { continue } + $presents = [regex]::Match($line, '\bsampledPresents=(\d+)\b') + $elapsed = [regex]::Match($line, '\bsamplingElapsedNs=(\d+)\b') + $samples = [regex]::Match($line, '\bsamples=(\d+)\b') + if (-not $presents.Success -or -not $elapsed.Success -or -not $samples.Success) { + throw "Missing sampler-window fields: $($Row.reportPath)" + } + if ([uint64]$samples.Groups[1].Value -ne [uint64]$Row.mainSamples) { throw 'Report sample count differs from benchmark row.' } + $found = [pscustomobject]@{ path=$Row.reportPath; + sha256=(Get-FileHash -LiteralPath $Row.reportPath -Algorithm SHA256).Hash; + presents=[uint64]$presents.Groups[1].Value; elapsedNs=[uint64]$elapsed.Groups[1].Value } + break + } } finally { $reader.Dispose() } + if (-not $found -or $found.presents -eq 0 -or $found.elapsedNs -eq 0) { + throw "No valid sampler-window evidence: $($Row.reportPath)" + } + $reports.Add($Row.reportPath,$found) + $found +} +function Add-Pair($A,$B,[string]$Metric,[double]$Av,[double]$Bv,[string]$Phase='', + [bool]$AObserved=$true,[bool]$BObserved=$true) { + if ($Av -lt 0 -or $Bv -lt 0 -or [double]::IsNaN($Av) -or [double]::IsNaN($Bv) -or + [double]::IsInfinity($Av) -or [double]::IsInfinity($Bv)) { throw "Invalid $Metric value" } + $relative = if ($AObserved -and $BObserved -and $Av -gt 0) { 100*($Bv/$Av-1) } else { $null } + $process = if ($metadata.mode -eq 'Backend') { $A.pair } else { $A.processIndex } + $round = if ($metadata.mode -eq 'Backend') { 0 } else { $A.round } + $paired.Add([pscustomobject]@{ suite=$A.suite; process=$process; round=$round; phase=$Phase; + metric=$Metric; a=$(if ($AObserved) { $Av } else { $null }); b=$(if ($BObserved) { $Bv } else { $null }); + delta=$(if ($AObserved -and $BObserved) { $Bv-$Av } else { $null }); changePercent=$relative; + observedA=$AObserved; observedB=$BObserved; + zeroA=($AObserved -and $Av -eq 0); zeroB=($BObserved -and $Bv -eq 0) }) +} +if ($metadata.mode -eq 'Backend') { + foreach ($group in ($rows | Group-Object suite,pair)) { + $a = @($group.Group | Where-Object label -eq 'A'); $b = @($group.Group | Where-Object label -eq 'B') + if ($a.Count -ne 1 -or $b.Count -ne 1 -or $a[0].iterations -ne $b[0].iterations) { throw "Unpaired backend rows: $($group.Name)" } + foreach ($metric in @('wall_ns_per_iteration','thread_cycles_per_iteration')) { + Add-Pair $a[0] $b[0] $metric ([double]$a[0].$metric) ([double]$b[0].$metric) + } + } +} elseif ($metadata.mode -eq 'Runtime') { + foreach ($group in ($rows | Group-Object suite,processIndex,round,phase)) { + $a = @($group.Group | Where-Object variant -eq 'A'); $b = @($group.Group | Where-Object variant -eq 'B') + if ($a.Count -ne 1 -or $b.Count -ne 1) { throw "Unpaired runtime rows: $($group.Name)" } + foreach ($metric in @('elapsedNs','mainCpuNs','csCpuNs')) { + Add-Pair $a[0] $b[0] ($metric+'PerPresent') ([double]$a[0].$metric/[double]$a[0].presents) ` + ([double]$b[0].$metric/[double]$b[0].presents) $a[0].phase + } + if ($a[0].phase -eq 'cpu') { + $aw = Read-SamplerWindow $a[0]; $bw = Read-SamplerWindow $b[0] + Add-Pair $a[0] $b[0] 'mainCyclesPerSampledPresent' ` + ([double]$a[0].mainThreadCycles/[double]$aw.presents) ` + ([double]$b[0].mainThreadCycles/[double]$bw.presents) 'cpu' + foreach ($metric in @('maxWorkPresentNs','maxPresentNs','completionPresentNs','completionWorkNs','cpuFinishingNs','reportWaitNs')) { + if ($a[0].PSObject.Properties[$metric] -and $b[0].PSObject.Properties[$metric]) { + $observedA = $metric -ne 'cpuFinishingNs' -or $a[0].cpuFinishingObserved + $observedB = $metric -ne 'cpuFinishingNs' -or $b[0].cpuFinishingObserved + Add-Pair $a[0] $b[0] $metric ([double]$a[0].$metric) ([double]$b[0].$metric) 'capture' $observedA $observedB + } + } + foreach ($metric in @('workPresentTotalNs','presentTotalNs')) { + if ($a[0].PSObject.Properties[$metric] -and $b[0].PSObject.Properties[$metric]) { + if ($a[0].iterations -le 0 -or $b[0].iterations -le 0) { throw 'Missing timed work/Present count.' } + Add-Pair $a[0] $b[0] ($metric+'PerIteration') ` + ([double]$a[0].$metric/[double]$a[0].iterations) ` + ([double]$b[0].$metric/[double]$b[0].iterations) 'capture' + } + } + } + } +} else { throw "Unknown mode: $($metadata.mode)" } +$summary = @( + foreach ($group in ($paired | Group-Object suite,phase,metric)) { + $changes = @($group.Group | Group-Object process | ForEach-Object { + $valid = @($_.Group | Where-Object { $null -ne $_.changePercent }) + [pscustomobject]@{ process=$_.Name; pairs=$_.Count; relativePairs=$valid.Count; + deltaMedian=(Median @($_.Group | ForEach-Object delta)); + changePercent=(Median @($valid | ForEach-Object changePercent)) } + }) + $allRelative = @($group.Group | Where-Object { $null -eq $_.changePercent }).Count -eq 0 + $cpuTime = $group.Group[0].metric -in @('mainCpuNsPerPresent','csCpuNsPerPresent') + [pscustomobject]@{ comparison=$group.Name; pairs=$group.Count; processes=$changes.Count; + aMedian=(Median @($group.Group | ForEach-Object a)); bMedian=(Median @($group.Group | ForEach-Object b)); + deltaMedian=(Median @($group.Group | ForEach-Object delta)); + medianProcessChangePercent=$(if ($allRelative) { Median @($changes | ForEach-Object changePercent) } else { $null }); + zeroAPairs=@($group.Group | Where-Object zeroA).Count; zeroBPairs=@($group.Group | Where-Object zeroB).Count; + unobservedAPairs=@($group.Group | Where-Object { -not $_.observedA }).Count; + unobservedBPairs=@($group.Group | Where-Object { -not $_.observedB }).Count; + cpuAccountingQuantized=$cpuTime; processChanges=$changes } + } +) +$result = [ordered]@{ mode=$metadata.mode; pairs=$paired.Count; + timingSemantics='Phase CPU time retains zeros and finite OS accounting resolution. Cycles are normalized only by Presents from the same CPU sampler window. Inclusive/parallel thread costs are not additive.'; + reports=@($reports.Values); summary=$summary } +[IO.File]::WriteAllText((Join-Path $directory 'paired.json'), (ConvertTo-Json -InputObject @($paired.ToArray()) -Depth 8), $utf8) +[IO.File]::WriteAllText((Join-Path $directory 'summary.json'), (ConvertTo-Json -InputObject $result -Depth 10), $utf8) +Write-Output "Summary: $(Join-Path $directory 'summary.json')" diff --git a/tests/measurement-runtime-test.ps1 b/tests/measurement-runtime-test.ps1 index 34be4c9..db70262 100644 --- a/tests/measurement-runtime-test.ps1 +++ b/tests/measurement-runtime-test.ps1 @@ -1,9 +1,18 @@ param([string]$EvidenceName = 'green', [string]$Backend, [switch]$BuildOnly, [string]$MSBuild, [switch]$AllowNonV18MsBuild, [string]$RuntimeSource, - [switch]$PendingStopOnly, [switch]$DeviceOwnerOnly, [switch]$UnicodeModulesOnly) + [bool]$StorageTestSync = $true, + [switch]$PendingStopOnly, [switch]$TelemetryGapOnly, [switch]$DeviceOwnerOnly, [switch]$UnicodeModulesOnly, + [ValidateSet('sealed-empty','high','normal','inactive','open','write','read','queue','cap','handoff','cancelled','worker','allocation','start-fail','start-throw','stop-fail','stop-throw','report-open','report-write')] + [string]$EventStreamMode) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest $root = Split-Path -Parent $PSScriptRoot +if ($EventStreamMode -and (-not $Backend -or $PendingStopOnly -or $TelemetryGapOnly -or $DeviceOwnerOnly -or $UnicodeModulesOnly)) { + throw 'EventStreamMode requires an explicit backend and cannot be combined with another focused mode.' +} +if ($TelemetryGapOnly -and (-not $Backend -or $PendingStopOnly -or $DeviceOwnerOnly -or $UnicodeModulesOnly)) { + throw 'TelemetryGapOnly requires an explicit backend and cannot be combined with another focused mode.' +} $out = Join-Path $root 'out/perf/20261027-unified-measurement/runtime' New-Item -ItemType Directory -Path (Join-Path $out 'scripts') -Force | Out-Null . (Join-Path $root 'tools/lib/process-runner.ps1') @@ -11,7 +20,7 @@ New-Item -ItemType Directory -Path (Join-Path $out 'scripts') -Force | Out-Null $msBuildInfo = Find-RenderStackMSBuild -MsBuildPath $MSBuild -RepoRoot $root -AllowNonV18MsBuild:$AllowNonV18MsBuild $include = Join-Path $root 'out/deps/Microsoft.DXSDK.D3DX/9.29.952.8/package/build/native/include' $buildArguments = @((Join-Path $PSScriptRoot 'measurement-runtime-tests.vcxproj'), '/nologo', '/m:1', '/nr:false', '/t:Build', - '/p:Configuration=Release', '/p:Platform=Win32', '/p:PreferredToolArchitecture=x64', "/p:D3DX9IncludeDir=$include") + ('/p:StorageTestSync=' + $StorageTestSync.ToString().ToLowerInvariant()), '/p:Configuration=Release', '/p:Platform=Win32', '/p:PreferredToolArchitecture=x64', "/p:D3DX9IncludeDir=$include") if ($msBuildInfo.ProductMajor -lt 18) { $buildArguments += '/p:PlatformToolset=v143' } $testExe = Join-Path $out 'bin/MeasurementRuntimeTests.exe' if ($RuntimeSource) { @@ -41,10 +50,13 @@ if ($BuildOnly) { return } Push-Location $out try { $testArguments = @() + if ($EventStreamMode) { $testArguments += '--event-stream-test' } if ($PendingStopOnly) { $testArguments += '--pending-stop-only' } + if ($TelemetryGapOnly) { $testArguments += '--telemetry-gap-only' } if ($DeviceOwnerOnly) { $testArguments += '--device-owner-only' } if ($UnicodeModulesOnly) { $testArguments += '--unicode-modules-only' } if ($Backend) { $testArguments += $Backend } + if ($EventStreamMode) { $testArguments += $EventStreamMode } & $testExe @testArguments 2>&1 | Tee-Object -FilePath (Join-Path $out "$EvidenceName-test.log") $result = $LASTEXITCODE diff --git a/tests/measurement-runtime-tests.cpp b/tests/measurement-runtime-tests.cpp index 04847da..cc62abc 100644 --- a/tests/measurement-runtime-tests.cpp +++ b/tests/measurement-runtime-tests.cpp @@ -6,17 +6,71 @@ #include #include #include +#include +#include +#include +#include #include #include #include +#include "lib/win32-import-hook.h" #if __has_include("../src/bridge/legacy/BridgeMeasurementRuntime.h") #include "../src/bridge/legacy/BridgeMeasurementRuntime.h" #include "../src/bridge/legacy/MeasurementSymbols.h" #include "../backend/dxvk/src/d3d9/d3d9_bridge_diagnostics.h" +#include "../backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h" +#include "../backend/dxvk/src/util/util_thread_scheduling.h" #define HAS_MEASUREMENT_RUNTIME 1 #endif +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) +#include "../src/bridge/legacy/MeasurementEventStorage.h" +namespace BridgeMeasurement::detail { +std::atomic g_pauseEmptyStorage{false}; +HANDLE g_emptyStorageSnapshot = nullptr, g_releaseEmptyStorage = nullptr; +void MeasurementStorageEmptyHeadSnapshot(MeasurementEventStorage&) noexcept { + if (!g_pauseEmptyStorage.exchange(false)) return; + SetEvent(g_emptyStorageSnapshot); + // A finite timeout keeps a broken fixture bounded; the caller asserts entry. + WaitForSingleObject(g_releaseEmptyStorage, 10000); +} +} +#endif + +// Deterministic fault injection is confined to this fixture executable. The +// allocator rejects one matching allocation on the selected calling thread. +std::atomic g_failAllocationThread{0}; +std::atomic g_failAllocationSize{0}; +std::atomic g_failAllocationCountdown{0}; +std::atomic g_workerAllocationCount{0}; +std::atomic g_workerAllocationSizes[16]{}; +std::atomic g_seenMainSampleBuffer{false}; +void* operator new(std::size_t size) +{ + const auto target = g_failAllocationSize.load(std::memory_order_relaxed); + if (target && g_failAllocationThread.load(std::memory_order_relaxed) == GetCurrentThreadId()) { + if (target == SIZE_MAX - 3) { + const auto index = g_workerAllocationCount.fetch_add(1); + if (index < std::size(g_workerAllocationSizes)) g_workerAllocationSizes[index] = size; + if (!g_seenMainSampleBuffer.load() && size >= 1024 * 1024) g_seenMainSampleBuffer = true; + else if (g_seenMainSampleBuffer.load() && g_failAllocationSize.exchange(0) == target) throw std::bad_alloc(); + } else if (target == SIZE_MAX - 2) { + const auto index = g_workerAllocationCount.fetch_add(1); + if (index < std::size(g_workerAllocationSizes)) g_workerAllocationSizes[index] = size; + const auto remaining = g_failAllocationCountdown.load(std::memory_order_relaxed); + if (remaining && g_failAllocationCountdown.fetch_sub(1) == 1 && + g_failAllocationSize.exchange(0) == target) throw std::bad_alloc(); + } else if ((target == SIZE_MAX || (target == SIZE_MAX - 1 && size >= 96 * 1024 && size <= 512 * 1024) || + (target < SIZE_MAX - 3 && size >= target && size - target <= 128)) && + g_failAllocationSize.exchange(0) == target) throw std::bad_alloc(); + } + if (void* value = std::malloc(size ? size : 1)) return value; + throw std::bad_alloc(); +} +void operator delete(void* value) noexcept { std::free(value); } +void operator delete(void* value, std::size_t) noexcept { std::free(value); } + namespace { unsigned g_checks = 0, g_failures = 0; void Check(bool value, const char* message) @@ -31,13 +85,67 @@ void Check(bool value, const char* message) using namespace BridgeMeasurement; std::atomic g_f7{false}, g_f8{false}; bool g_failWorker = false; +bool g_failOneWorker = false; +std::atomic g_delayNextWorker{false}; bool g_delayReportWorker = false; +bool g_deviceHasTelemetry = false; +bool g_matchHostWorkerScheduling = false; +renderstack::scheduling::Options g_hostWorkerSchedulingOptions{}; +std::atomic g_failNextWorkerAllocationOrdinal{0}; +std::atomic g_failNextWorkerCsObject{false}; HANDLE g_reportWorkerStarted = nullptr, g_releaseReportWorker = nullptr; std::atomic g_presentFrame{0}; +std::atomic g_holdGameFrame{false}; +std::atomic g_throwFrameRead{false}; std::vector g_workers; +using StartAuditFn = HRESULT (STDMETHODCALLTYPE*)(ID3D9BridgeDiagnostics*, UINT64, UINT); +using StopAuditFn = HRESULT (STDMETHODCALLTYPE*)(ID3D9BridgeDiagnostics*, UINT64); +StartAuditFn g_realStartAudit = nullptr; +StopAuditFn g_realStopAudit = nullptr; +std::atomic g_throwAfterAuditStart{false}, g_throwAfterAuditStop{false}; + +HRESULT STDMETHODCALLTYPE FaultAfterAuditStart(ID3D9BridgeDiagnostics* self, UINT64 capture, UINT duration) +{ + const auto result = g_realStartAudit(self, capture, duration); + if (SUCCEEDED(result) && g_throwAfterAuditStart.exchange(false)) + throw std::runtime_error("fixture exception after real StartAudit"); + return result; +} +HRESULT STDMETHODCALLTYPE FaultAfterAuditStop(ID3D9BridgeDiagnostics* self, UINT64 capture) +{ + const auto result = g_realStopAudit(self, capture); + if (SUCCEEDED(result) && g_throwAfterAuditStop.exchange(false)) + throw std::runtime_error("fixture exception after real StopAudit"); + return result; +} +struct DiagnosticFault { + ID3D9BridgeDiagnostics* diagnostics = nullptr; + void** original = nullptr; + std::array replacement{}; + explicit DiagnosticFault(IDirect3DDevice9* device) { + if (FAILED(device->QueryInterface(IID_D3D9BridgeDiagnostics, reinterpret_cast(&diagnostics))) || !diagnostics) return; + original = *reinterpret_cast(diagnostics); + std::copy(original, original + replacement.size(), replacement.begin()); + g_realStartAudit = reinterpret_cast(replacement[4]); + g_realStopAudit = reinterpret_cast(replacement[5]); + replacement[4] = reinterpret_cast(&FaultAfterAuditStart); + replacement[5] = reinterpret_cast(&FaultAfterAuditStop); + *reinterpret_cast(diagnostics) = replacement.data(); + } + ~DiagnosticFault() { + if (diagnostics) { + *reinterpret_cast(diagnostics) = original; + diagnostics->Release(); + } + g_throwAfterAuditStart = g_throwAfterAuditStop = false; + } +}; bool TestKeyDown(int key) { return key == VK_F7 ? g_f7.load() : key == VK_F8 && g_f8.load(); } -bool TestFrame(std::uint32_t& frame) { frame = g_presentFrame.load(std::memory_order_relaxed); return true; } +bool TestFrame(std::uint32_t& frame) { + if (g_throwFrameRead.exchange(false)) throw std::runtime_error("fixture frame callback failure"); + frame = g_presentFrame.load(std::memory_order_relaxed); return true; +} void TestLog(const char* text) { std::printf("runtime: %s\n", text ? text : ""); } struct DelayedWorker { LPTHREAD_START_ROUTINE start; void* context; }; DWORD WINAPI StartDelayedWorker(void* context) @@ -47,14 +155,34 @@ DWORD WINAPI StartDelayedWorker(void* context) WaitForSingleObject(g_releaseReportWorker, INFINITE); return worker->start(worker->context); } +struct HostScheduledWorker { LPTHREAD_START_ROUTINE entry; void* context; }; +DWORD WINAPI StartHostScheduledWorker(void* context) { + std::unique_ptr start(static_cast(context)); + renderstack::scheduling::ThreadSchedulingScope scheduling( + g_hostWorkerSchedulingOptions, renderstack::scheduling::Role::Background, &TestLog); + return start->entry(start->context); +} HANDLE StartTestWorker(LPTHREAD_START_ROUTINE start, void* context) { - if (g_failWorker) return nullptr; - const bool delay = g_delayReportWorker && g_workers.size() == 1; + if (g_failWorker || std::exchange(g_failOneWorker, false)) return nullptr; + const bool delay = g_delayNextWorker.exchange(false) || g_delayReportWorker && g_workers.size() == (g_deviceHasTelemetry ? 3 : 2); auto* delayed = delay ? new DelayedWorker{start, context} : nullptr; - HANDLE handle = CreateThread(nullptr, 0, delay ? StartDelayedWorker : start, - delay ? delayed : context, 0, nullptr); - if (!handle) delete delayed; + const auto failureOrdinal = g_failNextWorkerAllocationOrdinal.exchange(0); + const bool failCsObject = g_failNextWorkerCsObject.exchange(false); + const auto entry = delay ? StartDelayedWorker : start; + void* entryContext = delay ? static_cast(delayed) : context; + auto* scheduled = g_matchHostWorkerScheduling ? new HostScheduledWorker{entry, entryContext} : nullptr; + HANDLE handle = CreateThread(nullptr, 0, scheduled ? StartHostScheduledWorker : entry, + scheduled ? static_cast(scheduled) : entryContext, failureOrdinal ? CREATE_SUSPENDED : 0, nullptr); + if (handle && failureOrdinal) { + g_workerAllocationCount = 0; + g_seenMainSampleBuffer = false; + g_failAllocationThread = GetThreadId(handle); + g_failAllocationCountdown = failureOrdinal; + g_failAllocationSize = failCsObject ? SIZE_MAX - 3 : SIZE_MAX - 2; + ResumeThread(handle); + } + if (!handle) { delete scheduled; delete delayed; } if (handle) g_workers.push_back(handle); return handle; } @@ -113,6 +241,10 @@ struct DeviceFixture { parameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window, D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, ¶meters, &device); + ID3D9BridgeTelemetry* telemetry = nullptr; + g_deviceHasTelemetry = device && SUCCEEDED(device->QueryInterface(IID_D3D9BridgeTelemetry, + reinterpret_cast(&telemetry))) && telemetry; + if (telemetry) telemetry->Release(); } ~DeviceFixture() { @@ -124,25 +256,45 @@ struct DeviceFixture { HRESULT Present() { const HRESULT result = device->Present(nullptr, nullptr, nullptr, nullptr); - ++g_presentFrame; + if (!g_holdGameFrame.load()) ++g_presentFrame; RuntimePresent(device, result); return result; } }; +unsigned StorageTempFiles() { + wchar_t directory[MAX_PATH]{}, pattern[MAX_PATH]{}; + const auto length = GetTempPathW(MAX_PATH, directory); + if (!length || length >= MAX_PATH) return 0; + swprintf_s(pattern, L"%sF8e*.tmp", directory); + WIN32_FIND_DATAW found{}; + HANDLE search = FindFirstFileW(pattern, &found); + if (search == INVALID_HANDLE_VALUE) return 0; + unsigned count = 0; + do { if (!(found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) ++count; } while (FindNextFileW(search, &found)); + FindClose(search); return count; +} + struct RuntimeFixture { HANDLE stopEvent = nullptr; std::string directory; RuntimeConfig config{}; bool stopped = false; + unsigned initialStorageFiles = 0; explicit RuntimeFixture(const char* caseName) { RuntimeShutdown(); + initialStorageFiles = StorageTempFiles(); g_f7 = g_f8 = g_failWorker = false; g_delayReportWorker = false; + g_delayNextWorker = false; + g_failNextWorkerAllocationOrdinal = 0; + g_failNextWorkerCsObject = false; g_workers.clear(); g_presentFrame = 0; + g_holdGameFrame = false; + g_throwFrameRead = false; directory = (std::filesystem::current_path() / "cases" / (std::string(caseName) + "-" + std::to_string(GetCurrentProcessId()))).string(); std::filesystem::create_directories(std::filesystem::path(directory) / "scripts"); @@ -170,6 +322,7 @@ struct RuntimeFixture { CloseHandle(worker); } g_workers.clear(); + Check(StorageTempFiles() == initialStorageFiles, "capture owner removes temporary event files after all workers join"); if (g_reportWorkerStarted) CloseHandle(g_reportWorkerStarted); if (g_releaseReportWorker) CloseHandle(g_releaseReportWorker); g_reportWorkerStarted = g_releaseReportWorker = nullptr; @@ -186,6 +339,30 @@ RuntimeSnapshot Snapshot() return snapshot; } +void TestFrameCorrelation() +{ + FrameTrace trace; + trace.Add(10, 20, 1, Phase::Baseline, true); + trace.Add(20, 30, 2, Phase::Cpu, true); + trace.Add(40, 60, 3, Phase::Attribution, false); + std::vector ids, lengths; + const auto covered = trace.ForEachOverlap(15, 45, [&](const FrameRecord& row, auto a, auto b) { + ids.push_back(row.frameId); lengths.push_back(b - a); + }); + Check(ids == std::vector({1, 2, 3}) && + lengths == std::vector({5, 10, 5}) && covered == 20, + "cross-frame spans retain each half-open overlap and leave the phase gap unmatched"); + unsigned calls = 0; + Check(trace.ForEachOverlap(20, 20, [&](const FrameRecord&, auto, auto) { ++calls; }) == 0 && !calls, + "zero-length spans and exact boundaries do not create false overlaps"); + Controller controller({1, 1, 1, 50}); + controller.Key(false, true, 100, true); + controller.Present(1'000'100, false); + controller.Present(2'000'100, true); + Check(controller.trace().size() == 1 && !controller.trace().data()[0].beginIsSuccessfulPresent, + "failed phase-entry Present marks the next interval boundary truncated"); +} + bool WaitPhase(DeviceFixture& device, Phase phase, DWORD maximumMs = 8000) { const ULONGLONG deadline = GetTickCount64() + maximumMs; @@ -336,14 +513,15 @@ void TestComplete(DeviceFixture& device) std::vector phases{snapshot.phase}; unsigned journalTransactions = 0; bool journalOk = true, stableIdentity = true; - bool observerStagesSeparated = g_workers.empty(); + bool observerStagesSeparated = true; + bool sampledCpuHud = false; const ULONGLONG deadline = GetTickCount64() + 10000; while (snapshot.phase != Phase::Complete && snapshot.phase != Phase::Error && GetTickCount64() < deadline) { if (snapshot.phase == Phase::Baseline || snapshot.phase == Phase::Cpu || snapshot.phase == Phase::CpuFinishing) { observerStagesSeparated &= !g_properShadersStateAttributionActive.load(std::memory_order_acquire); - if (snapshot.phase == Phase::Baseline) observerStagesSeparated &= g_workers.empty(); + if (snapshot.phase == Phase::Baseline) observerStagesSeparated &= snapshot.cpuSamples == 0; if (diagnostics) { D3D9BridgeAuditSnapshot audit{}; audit.StructSize = sizeof(audit); @@ -358,6 +536,15 @@ void TestComplete(DeviceFixture& device) } device.Present(); snapshot = Snapshot(); + if (snapshot.phase == Phase::Cpu && diagnostics) { + D3D9BridgeHudSnapshot hud{}; + hud.StructSize = sizeof(hud); hud.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + if (SUCCEEDED(diagnostics->GetHud(&hud))) { + const std::string line(hud.Lines[4]); + sampledCpuHud |= line.find("sampling") != std::string::npos && + line.find("baseline") == std::string::npos; + } + } stableIdentity &= snapshot.captureId == capture; if (phases.back() != snapshot.phase) phases.push_back(snapshot.phase); if (snapshot.phase == Phase::AttributionFinishing && @@ -394,6 +581,8 @@ void TestComplete(DeviceFixture& device) "a key held across report finalization does not start a delayed capture"); Check(observerStagesSeparated, "baseline runs without sampling and baseline/CPU phases leave journal and backend attribution inactive"); + if (diagnostics) Check(sampledCpuHud, + "actual published HUD describes CPU sampling while its worker is still running"); Check(stableIdentity && snapshot.cpuSamples > 0 && snapshot.suspendedNs > 0 && snapshot.phaseStats[0].presents > 0 && snapshot.phaseStats[1].presents > 0 && snapshot.phaseStats[2].presents > 0, @@ -479,6 +668,357 @@ void TestComplete(DeviceFixture& device) runtime.Stop(); } +// This exercises the production runtime and real device over multiple memory +// snapshot intervals, including unchanged game-frame observations and actual +// Win32 minimize/restore transitions. Old backends must report absence rather +// than suppressing the main sampler or pretending telemetry was captured. +void TestCoverageCapture(DeviceFixture& device) +{ + RuntimeFixture runtime("coverage-capture"); + runtime.config.timing = {160, 240, 300, 50}; + runtime.config.intervalMs = 3; + runtime.Configure(); + ID3D9BridgeTelemetry* telemetry = nullptr; + const bool hasTelemetry = SUCCEEDED(device.device->QueryInterface(IID_D3D9BridgeTelemetry, + reinterpret_cast(&telemetry))) && telemetry; + if (telemetry) telemetry->Release(); + + g_f8 = true; + device.Present(); + const auto capture = Snapshot().captureId; + g_holdGameFrame = true; + ShowWindow(device.window, SW_MINIMIZE); + IDirect3DTexture9* stalledTexture = nullptr; + if (SUCCEEDED(device.device->CreateTexture(1024, 1024, 1, D3DUSAGE_DYNAMIC, + D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &stalledTexture, nullptr)) && stalledTexture) { + D3DLOCKED_RECT locked{}; + if (SUCCEEDED(stalledTexture->LockRect(0, &locked, nullptr, D3DLOCK_DISCARD))) + stalledTexture->UnlockRect(0); + stalledTexture->Release(); + } + Sleep(700); // no Presents: the observer and snapshot-gap report must retain the gap + const auto deadline = GetTickCount64() + 15000; + unsigned presents = 0; + while (Snapshot().phase != Phase::Complete && Snapshot().phase != Phase::Error && + GetTickCount64() < deadline) { + if (presents == 12) { + ShowWindow(device.window, SW_RESTORE); + g_holdGameFrame = false; + } + IDirect3DTexture9* texture = nullptr; + if (presents % 16 == 0 && SUCCEEDED(device.device->CreateTexture(512, 512, 1, + D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, nullptr)) && texture) { + D3DLOCKED_RECT locked{}; + if (SUCCEEDED(texture->LockRect(0, &locked, nullptr, D3DLOCK_DISCARD))) + texture->UnlockRect(0); + texture->Release(); + } + device.Present(); + ++presents; + Sleep(2); + } + auto snapshot = Snapshot(); + while (snapshot.workerBusy && GetTickCount64() < deadline) { + device.Present(); Sleep(1); snapshot = Snapshot(); + } + Check(capture && snapshot.phase == Phase::Complete && !snapshot.workerBusy, + "coverage capture completes through the actual runtime and device"); + std::ifstream file(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + Check(report.find("cpuStream kind=main available=1") != std::string::npos && + report.find("sample stream=main ") != std::string::npos, + "combined report labels main-thread samples separately"); + if (device.backend) Check(report.find("cpuStream kind=cs available=1") != std::string::npos && + report.find("sample stream=cs ") != std::string::npos && + (report.find("caller stream=cs kind=structural-frame-chain") != std::string::npos || + report.find("caller stream=cs kind=heuristic-candidate") != std::string::npos), + "real backend CS thread contributes labelled samples and at least one qualified caller"); + else Check(report.find("cpuStream kind=cs available=0") != std::string::npos, + "unsupported CS thread sampling is explicitly unavailable"); + Check(report.find("context kind=window") != std::string::npos && + report.find("minimized=1") != std::string::npos && + report.find("context kind=gameFrame") != std::string::npos && + report.find("unchanged=1") != std::string::npos && + report.find("context kind=presentGap") != std::string::npos && + report.find("reuseCandidate confidence=heuristic") != std::string::npos, + "report records real window and unchanged game-frame evidence as a heuristic candidate"); + if (hasTelemetry) { + Check(report.find("telemetry available=1") != std::string::npos && + report.find("telemetryEvent ") != std::string::npos && + report.find("memorySnapshot ") != std::string::npos && + report.find("memoryChunk ") != std::string::npos && + report.find("frameOverlap ") != std::string::npos, + "real telemetry events and allocator snapshots reach the frame-correlated report"); + Check(report.find("boundary=periodic") != std::string::npos && + report.find("cadenceGap=1") != std::string::npos && + report.find("memoryChangeSemantics=adjacent-snapshots") != std::string::npos, + "periodic snapshot reports its delayed interval and bounded adjacent-history semantics"); + auto value = [](const std::string& line, const char* key, std::uint64_t& out) { + const auto position = line.find(std::string(" ") + key + "="); + if (position == std::string::npos) return false; + const char* begin = line.c_str() + position + std::strlen(key) + 2; + char* end = nullptr; + out = std::strtoull(begin, &end, 10); + return end != begin; + }; + std::map> frames, events; + std::istringstream lines(report); + std::string line; + unsigned checked = 0; + bool validOverlap = true, sawSlowOverlap = false; + while (std::getline(lines, line)) { + std::uint64_t id = 0, begin = 0, end = 0; + if (line.rfind("frame ", 0) == 0 && value(line, "id", id) && + value(line, "beginNs", begin) && value(line, "endNs", end)) frames[id] = {begin, end}; + if (line.rfind("telemetryEvent ", 0) == 0 && value(line, "index", id) && + value(line, "beginNs", begin) && value(line, "endNs", end)) events[id] = {begin, end}; + if (line.rfind("frameOverlap ", 0) != 0) continue; + std::uint64_t event = 0, frame = 0, overlap = 0; + const bool fields = value(line, "event", event) && value(line, "frame", frame) && + value(line, "beginNs", begin) && value(line, "endNs", end) && value(line, "overlapNs", overlap); + if (!fields || !events.count(event) || !frames.count(frame)) { validOverlap = false; continue; } + const auto& span = events[event]; const auto& interval = frames[frame]; + validOverlap &= begin == (std::max)(span.first, interval.first) && + end == (std::min)(span.second, interval.second) && end > begin && overlap == end - begin; + sawSlowOverlap |= line.find("class=slow") != std::string::npos; + ++checked; + } + Check(validOverlap && checked > 0 && sawSlowOverlap, + "real event overlaps equal the independently checked frame intersections including a slow frame"); + } else Check(report.find("telemetry available=0") != std::string::npos, + "older backend explicitly reports missing event and memory telemetry"); + g_holdGameFrame = false; + g_f8 = false; + runtime.Stop(); +} + +void TestAllocationFailureAtCaptureEnd(DeviceFixture& device, bool reportAllocation) +{ + RuntimeFixture runtime(reportAllocation ? "report-allocation-failure" : "snapshot-allocation-failure"); + runtime.config.timing = {10, 100, 100, 50}; + runtime.config.telemetry = !reportAllocation; + runtime.Configure(); + ID3D9BridgeTelemetry* telemetry = nullptr; + const bool supported = SUCCEEDED(device.device->QueryInterface(IID_D3D9BridgeTelemetry, + reinterpret_cast(&telemetry))) && telemetry; + if (!reportAllocation && !supported) { + if (telemetry) telemetry->Release(); + runtime.Stop(); return; + } + g_f8 = true; + Check(WaitPhase(device, Phase::Attribution), "allocation-failure fixture reaches attribution"); + const auto capture = Snapshot().captureId; + Sleep(120); + g_failAllocationThread = GetCurrentThreadId(); + g_failAllocationSize = reportAllocation ? SIZE_MAX : 256 * sizeof(D3D9BridgeMemoryChunk); + try { device.Present(); } catch (const std::bad_alloc&) { /* host's BestEffort boundary swallows this */ } + const bool injected = g_failAllocationSize.exchange(0) == 0; + auto snapshot = Snapshot(); + const auto deadline = GetTickCount64() + 2000; + while (snapshot.workerBusy && GetTickCount64() < deadline) { + device.Present(); Sleep(1); snapshot = Snapshot(); + } + if (reportAllocation) { + Check(injected && snapshot.phase == Phase::Error && snapshot.lastError[0], + "report allocation failure becomes a visible error instead of false completion"); + } else { + D3D9BridgeTelemetryRead read{}; + read.StructSize = sizeof(read); read.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + const bool stopped = telemetry && SUCCEEDED(telemetry->ReadEvents(capture, &read, nullptr, 0)) && !read.Active; + std::ifstream file(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + Check(injected && stopped && snapshot.phase == Phase::Complete && + report.find("failures=1") != std::string::npos && + (snapshot.unavailableMask & RuntimeTelemetryUnavailable) != 0, + "snapshot allocation failure still stops real telemetry and reports partial capture"); + if (telemetry && !stopped) telemetry->StopCapture(capture); + } + if (telemetry) telemetry->Release(); + g_f8 = false; + runtime.Stop(); +} + +void TestContextFailureStatus(DeviceFixture& device) +{ + RuntimeFixture runtime("context-worker-failure"); + runtime.Configure(); + g_throwFrameRead = true; + g_f8 = true; + const bool completed = WaitPhase(device, Phase::Complete); + auto snapshot = Snapshot(); + const auto deadline = GetTickCount64() + 5000; + while (snapshot.workerBusy && GetTickCount64() < deadline) { + device.Present(); Sleep(1); snapshot = Snapshot(); + } + std::ifstream file(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + Check(completed && !snapshot.workerBusy && (snapshot.unavailableMask & RuntimeContextUnavailable) && + report.find("status=complete-partial") != std::string::npos && + report.find("contextSummary available=0") != std::string::npos, + "context observer failure is reflected in both capture status and detailed report"); + runtime.Stop(); +} + +void TestContextAllocationFailure(DeviceFixture& device) +{ + RuntimeFixture runtime("context-allocation-failure"); + runtime.Configure(); + g_failAllocationThread = GetCurrentThreadId(); + g_failAllocationSize = SIZE_MAX - 1; + g_f8 = true; + try { device.Present(); } catch (const std::bad_alloc&) { } + const bool injected = g_failAllocationSize.exchange(0) == 0; + const bool completed = WaitPhase(device, Phase::Complete); + auto snapshot = Snapshot(); + const auto deadline = GetTickCount64() + 5000; + while (snapshot.workerBusy && GetTickCount64() < deadline) { + device.Present(); Sleep(1); snapshot = Snapshot(); + } + std::ifstream file(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + Check(injected && completed && !snapshot.workerBusy && + (snapshot.unavailableMask & RuntimeContextUnavailable) && + report.find("contextSummary available=0") != std::string::npos, + "context allocation failure preserves the remaining capture and reports partial status"); + runtime.Stop(); +} + +void TestCpuJobAllocationFailure(DeviceFixture& device) +{ + RuntimeFixture runtime("cpu-job-allocation-failure"); + runtime.config.timing = {100, 1000, 100, 50}; + runtime.Configure(); + ID3D9BridgeTelemetry* telemetry = nullptr; + const bool supported = SUCCEEDED(device.device->QueryInterface(IID_D3D9BridgeTelemetry, + reinterpret_cast(&telemetry))) && telemetry; + g_f8 = true; + device.Present(); + const auto capture = Snapshot().captureId; + Sleep(120); + g_failAllocationThread = GetCurrentThreadId(); + g_failAllocationSize = SIZE_MAX; + try { device.Present(); } catch (const std::bad_alloc&) { /* production BestEffort boundary */ } + const bool injected = g_failAllocationSize.exchange(0) == 0; + auto snapshot = Snapshot(); + const bool errorImmediate = snapshot.phase == Phase::Error; + D3D9BridgeTelemetryRead read{}; + read.StructSize = sizeof(read); read.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + const bool stopped = !supported || + (SUCCEEDED(telemetry->ReadEvents(capture, &read, nullptr, 0)) && !read.Active); + const auto deadline = GetTickCount64() + 2000; + while (snapshot.workerBusy && GetTickCount64() < deadline) { + device.Present(); Sleep(1); snapshot = Snapshot(); + } + std::ifstream file(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + Check(capture && injected && errorImmediate && stopped && !snapshot.workerBusy && + (snapshot.unavailableMask & RuntimeCpuUnavailable) && report.find("status=error") != std::string::npos, + "CPU job allocation failure stops active telemetry and reports Error at the phase boundary"); + if (Snapshot().phase != Phase::Error) RuntimeInterrupt(device.device, "fixture cleanup"); + if (telemetry) telemetry->Release(); + g_f8 = false; + runtime.Stop(); +} + +void ArmCsResolveAllocationFailure(const char*, const RuntimeCpuReport&) +{ + // The production main-stream adapter callback runs after main symbol + // processing; fail the next allocation on that same sampler thread. + g_failAllocationThread = GetCurrentThreadId(); + g_failAllocationSize = SIZE_MAX; +} + +void TestCsFailureIsolation(DeviceFixture& device, bool resolutionFailure) +{ + if (!device.backend) return; + RuntimeFixture runtime(resolutionFailure ? "cs-resolution-failure" : "cs-allocation-failure"); + runtime.config.context = false; // the first fixture worker is the sampler + runtime.config.timing = {30, 200, 50, 50}; + runtime.config.intervalMs = 2; + runtime.config.csIntervalMs = 5; + if (resolutionFailure) runtime.config.appendCpuReport = ArmCsResolveAllocationFailure; + runtime.Configure(); + g_f8 = true; + device.Present(); + if (!resolutionFailure) { g_failNextWorkerAllocationOrdinal = 1; g_failNextWorkerCsObject = true; } + const auto deadline = GetTickCount64() + 6000; + RuntimeSnapshot snapshot{}; + do { + device.Present(); Sleep(1); snapshot = Snapshot(); + } while (snapshot.phase != Phase::Complete && snapshot.phase != Phase::Error && GetTickCount64() < deadline); + while (snapshot.workerBusy && GetTickCount64() < deadline) { + device.Present(); Sleep(1); snapshot = Snapshot(); + } + const bool injected = g_failAllocationSize.exchange(0) == 0; + std::ifstream file(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + const char* reason = resolutionFailure ? "cs-symbol-resolution-failed" : "cs-allocation-failed"; + const bool passed = injected && snapshot.phase == Phase::Complete && !snapshot.workerBusy && snapshot.cpuSamples > 0 && + (snapshot.unavailableMask & RuntimeCsCpuUnavailable) && + report.find("cpuStream kind=main available=1") != std::string::npos && + report.find("cpuStream kind=cs available=0") != std::string::npos && report.find(reason) != std::string::npos && + (!resolutionFailure || snapshot.csSamples > 0); + if (!passed && !resolutionFailure) { + std::printf("CS allocation fixture injected=%u phase=%u mainSamples=%llu error=%s allocationSizes=", + unsigned(injected), unsigned(snapshot.phase), snapshot.cpuSamples, snapshot.lastError); + const auto count = (std::min)(g_workerAllocationCount.load(), unsigned(std::size(g_workerAllocationSizes))); + for (unsigned i = 0; i < count; ++i) std::printf("%s%zu", i ? "," : "", g_workerAllocationSizes[i].load()); + std::printf("\n"); + } + Check(passed, + resolutionFailure ? "CS symbol resolution failure preserves main results and raw CS samples" : + "CS-only allocation failure preserves successful main sampling"); + g_f8 = false; + runtime.Stop(); +} + +void TestAuditBoundaryException(DeviceFixture& device, bool startFailure) +{ + if (!device.backend) return; + RuntimeFixture runtime(startFailure ? "audit-start-exception" : "audit-stop-exception"); + runtime.config.timing = {30, 100, 60, 50}; + runtime.Configure(); + RuntimePrepareDevice(device.device); + DiagnosticFault fault(device.device); + ID3D9BridgeTelemetry* telemetry = nullptr; + const bool supported = SUCCEEDED(device.device->QueryInterface(IID_D3D9BridgeTelemetry, + reinterpret_cast(&telemetry))) && telemetry; + if (!fault.diagnostics || !supported) { + if (telemetry) telemetry->Release(); runtime.Stop(); return; + } + if (startFailure) g_throwAfterAuditStart = true; else g_throwAfterAuditStop = true; + g_f8 = true; + RuntimeSnapshot snapshot{}; + const auto deadline = GetTickCount64() + 4000; + do { + try { device.Present(); } catch (const std::exception&) { /* production BestEffort boundary */ } + Sleep(1); snapshot = Snapshot(); + } while (snapshot.phase != Phase::Complete && snapshot.phase != Phase::Error && GetTickCount64() < deadline); + const auto capture = snapshot.captureId; + const auto drainDeadline = GetTickCount64() + 2000; + while (snapshot.workerBusy && GetTickCount64() < drainDeadline) { + try { device.Present(); } catch (const std::exception&) { } + Sleep(1); snapshot = Snapshot(); + } + D3D9BridgeAuditSnapshot audit{}; + audit.StructSize = sizeof(audit); audit.Version = D3D9_BRIDGE_DIAGNOSTICS_VERSION; + D3D9BridgeTelemetryRead read{}; + read.StructSize = sizeof(read); read.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + const bool stopped = SUCCEEDED(fault.diagnostics->GetAudit(&audit)) && !audit.Active && + SUCCEEDED(telemetry->ReadEvents(capture, &read, nullptr, 0)) && !read.Active; + const bool injected = startFailure ? !g_throwAfterAuditStart.load() : !g_throwAfterAuditStop.load(); + std::ifstream file(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(file)), {}); + Check(capture && injected && stopped && snapshot.phase == Phase::Complete && !snapshot.workerBusy && + (snapshot.unavailableMask & RuntimeBackendUnavailable) && report.find("status=complete-partial") != std::string::npos, + startFailure ? "exception after real StartAudit stops audit and completes remaining capture as partial" : + "exception after real StopAudit cannot skip telemetry, context and report cleanup"); + if (!stopped) { telemetry->StopCapture(capture); fault.diagnostics->StopAudit(capture); } + telemetry->Release(); + g_f8 = false; + runtime.Stop(); +} + void TestWorkerFailure(DeviceFixture& device) { RuntimeFixture runtime(device.backend ? "dxvk-worker-failure" : "system-worker-failure"); @@ -498,10 +1038,24 @@ void TestInterruption(DeviceFixture& device, bool release) (device.backend ? "dxvk-reset" : "system-reset")); runtime.config.timing.cpuMs = 5000; runtime.Configure(); + HANDLE finished = CreateEventA(nullptr, TRUE, FALSE, nullptr); + std::thread watchdog([&] { + if (WaitForSingleObject(finished, 15000) != WAIT_OBJECT_0) { + std::fputs("FAIL: CS sampling Reset/Release watchdog expired\n", stderr); + std::fflush(stderr); + TerminateProcess(GetCurrentProcess(), 87); + } + }); g_f8 = true; const bool sampling = WaitPhase(device, Phase::Cpu); const auto before = Snapshot(); Check(sampling && before.workerBusy, "lifecycle fixture reaches an in-flight sampling worker"); + const auto workDeadline = GetTickCount64() + 150; + while (GetTickCount64() < workDeadline) { + device.device->Clear(0, nullptr, D3DCLEAR_TARGET, 0xff102030, 1, 0); + if (SUCCEEDED(device.device->BeginScene())) device.device->EndScene(); + device.Present(); Sleep(1); + } RuntimeInterrupt(device.device, release ? "Release" : "Reset", release); const auto after = Snapshot(); Check(after.phase == Phase::Interrupted && after.captureId == before.captureId && @@ -516,6 +1070,11 @@ void TestInterruption(DeviceFixture& device, bool release) "real D3D9 Reset succeeds after runtime interruption"); } runtime.Stop(); + if (device.backend) Check(Snapshot().csSamples > 0, + "CS samples were actually captured while real work overlapped Reset/Release cancellation"); + SetEvent(finished); + watchdog.join(); + CloseHandle(finished); } void TestShutdown(DeviceFixture& device) @@ -641,6 +1200,8 @@ void TestAdmittedJournalInterruption(DeviceFixture& device, bool lateRestore, bo requireEvent(restored, 5000, "actual journal Restore/Reset did not finish after owner interruption returned"); Check(reportWorker && WaitForSingleObject(reportWorker, 5000) == WAIT_OBJECT_0, "actual report worker reaches completion after its bounded journal drain"); + const auto allWorkersDeadline = GetTickCount64() + 5000; + while (Snapshot().workerBusy && GetTickCount64() < allWorkersDeadline) Sleep(1); presentOnOwner(); const auto completed = Snapshot(); JournalAttributionSummary finalJournal{}; @@ -674,7 +1235,16 @@ constexpr BenchmarkVertex BenchmarkTriangle[] = { {3, 3, 0, 1, 0xff80c0ff}, {61, 3, 0, 1, 0xff80c0ff}, {3, 61, 0, 1, 0xff80c0ff} }; -bool BenchmarkWorkload(DeviceFixture& device, HRESULT& presented) +std::uint64_t BenchmarkNowNs() +{ + static const auto frequency = [] { LARGE_INTEGER value{}; QueryPerformanceFrequency(&value); return value.QuadPart; }(); + LARGE_INTEGER value{}; QueryPerformanceCounter(&value); + if (frequency <= 0 || value.QuadPart < 0) return 0; + const auto t = std::uint64_t(value.QuadPart), f = std::uint64_t(frequency); + return (t / f) * 1'000'000'000ull + (t % f) * 1'000'000'000ull / f; +} + +bool BenchmarkWorkload(DeviceFixture& device, HRESULT& presented, std::uint64_t* presentNs = nullptr) { HRESULT work = device.device->Clear(0, nullptr, D3DCLEAR_TARGET, 0xff102030, 1, 0); const HRESULT begun = device.device->BeginScene(); @@ -685,7 +1255,9 @@ bool BenchmarkWorkload(DeviceFixture& device, HRESULT& presented) const HRESULT ended = device.device->EndScene(); if (FAILED(ended)) work = ended; } else work = begun; + const auto presentBegin = presentNs ? BenchmarkNowNs() : 0; presented = device.Present(); + if (presentNs) *presentNs = BenchmarkNowNs() - presentBegin; return SUCCEEDED(work); } @@ -850,20 +1422,883 @@ void TestBenchmark(DeviceFixture& device, unsigned rounds, unsigned iterations, fence->Release(); if (valid && activeCapture) BenchmarkActiveCapture(device); } + +bool CoverageBenchmarkVariant(DeviceFixture& device, IDirect3DQuery9* fence, unsigned round, + const char* order, unsigned position, bool enabled, unsigned cpuMs) +{ + RuntimeFixture runtime(enabled ? "coverage-bench-B" : "coverage-bench-A"); + runtime.config.timing = {250, cpuMs, 250, 50}; + runtime.config.intervalMs = 5; + runtime.config.telemetry = runtime.config.csCpu = runtime.config.context = enabled; + runtime.Configure(); + RuntimePrepareDevice(device.device); + device.device->SetRenderState(D3DRS_LIGHTING, FALSE); + device.device->SetRenderState(D3DRS_ZENABLE, FALSE); + device.device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); + device.device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); + device.device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); + IDirect3DTexture9* texture = nullptr; + if (FAILED(device.device->CreateTexture(256, 256, 1, D3DUSAGE_DYNAMIC, + D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, nullptr)) || !texture) return false; + bool valid = true; + std::uint64_t workPresentTotalNs = 0, presentTotalNs = 0, maxWorkPresentNs = 0, maxPresentNs = 0; + std::uint64_t lastWorkNs = 0, lastPresentNs = 0, completionPresentNs = 0, completionWorkNs = 0; + std::uint64_t cpuFinishingNs = 0, cpuFinishingBegin = 0, reportWaitNs = 0; + bool cpuFinishingActive = false, cpuFinishingObserved = false, reportWasPending = false; + (void)BenchmarkNowNs(); // initialize QPC frequency before timed work + auto work = [&](unsigned iteration, bool timed) { + const auto workBegin = timed ? BenchmarkNowNs() : 0; + D3DLOCKED_RECT locked{}; + HRESULT update = texture->LockRect(0, &locked, nullptr, D3DLOCK_DISCARD); + if (SUCCEEDED(update)) { + *static_cast(locked.pBits) = 0xff000000u | iteration; + update = texture->UnlockRect(0); + } + if (FAILED(update)) valid = false; + if (iteration % 32 == 0) { + IDirect3DTexture9* transient = nullptr; + if (FAILED(device.device->CreateTexture(256, 256, 1, D3DUSAGE_DYNAMIC, + D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &transient, nullptr)) || !transient) valid = false; + if (transient) transient->Release(); + } + HRESULT presented = E_FAIL; + std::uint64_t presentNs = 0; + if (!BenchmarkWorkload(device, presented, timed ? &presentNs : nullptr) || presented != S_OK) valid = false; + if (timed) { + lastWorkNs = BenchmarkNowNs() - workBegin; lastPresentNs = presentNs; + workPresentTotalNs += lastWorkNs; presentTotalNs += lastPresentNs; + maxWorkPresentNs = (std::max)(maxWorkPresentNs, lastWorkNs); + maxPresentNs = (std::max)(maxPresentNs, lastPresentNs); + } + }; + for (unsigned i = 0; i < 64 && valid; ++i) work(i, false); + valid &= BenchmarkFence(fence); + g_f8 = true; + unsigned iterations = 0; + const auto deadline = GetTickCount64() + cpuMs + 15000; + RuntimeSnapshot snapshot{}; + while (valid && GetTickCount64() < deadline) { + work(iterations++, true); + snapshot = Snapshot(); + const auto observedAt = BenchmarkNowNs(); + if (snapshot.phase == Phase::CpuFinishing) { + if (!cpuFinishingActive) { cpuFinishingBegin = observedAt; cpuFinishingActive = cpuFinishingObserved = true; } + } else if (cpuFinishingActive) { + cpuFinishingNs += observedAt - cpuFinishingBegin; cpuFinishingActive = false; + } + if (snapshot.phase == Phase::Complete || snapshot.phase == Phase::Error) { + completionPresentNs = lastPresentNs; completionWorkNs = lastWorkNs; break; + } + Sleep(1); + } + if (cpuFinishingActive) cpuFinishingNs += BenchmarkNowNs() - cpuFinishingBegin; + const auto reportWaitBegin = BenchmarkNowNs(); + reportWasPending = snapshot.workerBusy; + while (snapshot.workerBusy && GetTickCount64() < deadline) { + device.Present(); Sleep(1); snapshot = Snapshot(); + } + reportWaitNs = BenchmarkNowNs() - reportWaitBegin; + valid &= snapshot.phase == Phase::Complete && !snapshot.workerBusy && snapshot.cpuSamples > 0 && + iterations > 0 && completionPresentNs > 0 && completionWorkNs >= completionPresentNs && + maxWorkPresentNs >= maxPresentNs && workPresentTotalNs >= presentTotalNs; + std::ifstream reportFile(snapshot.reportPath); + const std::string report((std::istreambuf_iterator(reportFile)), {}); + valid &= enabled ? (snapshot.csSamples > 0 && snapshot.telemetryEvents > 0 && snapshot.memorySnapshots >= 2 && + report.find("telemetry available=1") != std::string::npos && + report.find("contextSummary available=1") != std::string::npos) : + (snapshot.csSamples == 0 && snapshot.telemetryEvents == 0 && + report.find("contextSummary available=0 reason=disabled") != std::string::npos); + valid &= BenchmarkFence(fence); + const char* names[] = {"baseline", "cpu", "attribution"}; + for (unsigned phase = 0; phase < 3; ++phase) { + const auto& stats = snapshot.phaseStats[phase]; + std::printf("{\"type\":\"coverage-benchmark\",\"pid\":%lu,\"round\":%u,\"variant\":\"%c\",\"order\":\"%s\",\"position\":%u,\"collectionEnabled\":%s,\"counterScope\":\"capture\",\"phase\":\"%s\",\"elapsedNs\":%llu,\"presents\":%llu,\"failedPresents\":%llu,\"mainCpuNs\":%llu,\"csCpuNs\":%llu,\"cpuTimeAvailable\":%s,\"mainSamples\":%llu,\"csSamples\":%llu,\"mainMissed\":%llu,\"csMissed\":%llu,\"mainDropped\":%llu,\"csDropped\":%llu,\"suspendedNs\":%llu,\"csSuspendedNs\":%llu,\"mainThreadCycles\":%llu,\"csThreadCycles\":%llu,\"events\":%llu,\"filtered\":%llu,\"dropped\":%llu,\"memorySnapshots\":%llu,\"frameTraceDropped\":%llu,\"workPresentTotalNs\":%llu,\"presentTotalNs\":%llu,\"maxWorkPresentNs\":%llu,\"maxPresentNs\":%llu,\"completionPresentNs\":%llu,\"completionWorkNs\":%llu,\"cpuFinishingNs\":%llu,\"cpuFinishingObserved\":%s,\"reportWaitNs\":%llu,\"reportWasPending\":%s,\"iterations\":%u,\"valid\":%s,\"reportPath\":%s}\n", + GetCurrentProcessId(), round, enabled ? 'B' : 'A', order, position, enabled ? "true" : "false", names[phase], + stats.elapsedNs(), stats.presents, stats.failedPresents, snapshot.phaseMainCpuNs[phase], + snapshot.phaseCsCpuNs[phase], (snapshot.threadActivityMask & (1u << phase)) ? "true" : "false", + snapshot.cpuSamples, snapshot.csSamples, snapshot.cpuMissed, snapshot.csMissed, + snapshot.cpuDropped, snapshot.csDropped, snapshot.suspendedNs, snapshot.csSuspendedNs, + snapshot.mainThreadCycles, snapshot.csThreadCycles, snapshot.telemetryEvents, + snapshot.telemetryFiltered, snapshot.telemetryDropped, snapshot.memorySnapshots, + snapshot.frameTraceDropped, workPresentTotalNs, presentTotalNs, maxWorkPresentNs, maxPresentNs, + completionPresentNs, completionWorkNs, cpuFinishingNs, cpuFinishingObserved ? "true" : "false", + reportWaitNs, reportWasPending ? "true" : "false", iterations, valid ? "true" : "false", JsonString(snapshot.reportPath).c_str()); + } + std::fflush(stdout); + texture->Release(); + g_f8 = false; + runtime.Stop(); + return valid; +} + +void TestCoverageBenchmark(DeviceFixture& device, unsigned rounds, unsigned cpuMs, bool aa) +{ + if (!device.backend || rounds < 1 || rounds > 64 || cpuMs < 200 || cpuMs > 30000) { + Check(false, "coverage benchmark requires backend, 1-64 rounds, CPU 200-30000 ms"); + return; + } + IDirect3DQuery9* fence = nullptr; + if (FAILED(device.device->CreateQuery(D3DQUERYTYPE_EVENT, &fence)) || !fence) { + Check(false, "coverage benchmark creates GPU fence"); return; + } + bool valid = true; + for (unsigned round = 0; round < rounds && valid; ++round) { + const char* order = aa ? "AA" : round % 2 ? "BA" : "AB"; + for (unsigned position = 0; position < 2 && valid; ++position) + valid = CoverageBenchmarkVariant(device, fence, round, order, position, order[position] == 'B', cpuMs); + } + fence->Release(); + Check(valid, "coverage benchmark completes controlled real draw/update/Present captures"); +} +// Platform-boundary fault injection: patch only this fixture's Win32 imports. +std::atomic g_storageFault{0}; +std::atomic g_storageHandle{INVALID_HANDLE_VALUE}; +std::atomic g_reportOutputHandle{INVALID_HANDLE_VALUE}; +HANDLE g_releaseStorageWrite = nullptr; +HANDLE g_storageReadEntered = nullptr, g_releaseStorageRead = nullptr; +using StorageCreateFn = decltype(&CreateFileW); +using StorageWriteFn = decltype(&WriteFile); +using StorageReadFn = decltype(&ReadFile); +StorageCreateFn g_realStorageCreate = CreateFileW; +StorageWriteFn g_realStorageWrite = WriteFile; +StorageReadFn g_realStorageRead = ReadFile; +HANDLE WINAPI StorageCreate(LPCWSTR path, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES security, + DWORD disposition, DWORD flags, HANDLE templateFile) { + const bool storage = path && std::wcsstr(path, L"F8e") && (flags & FILE_FLAG_DELETE_ON_CLOSE); + const bool report = path && std::wcsstr(path, L"BridgeD3D9.measurement-"); + if (report && g_storageFault == 6) { SetLastError(ERROR_ACCESS_DENIED); return INVALID_HANDLE_VALUE; } + if (storage && g_storageFault == 1) { SetLastError(ERROR_ACCESS_DENIED); return INVALID_HANDLE_VALUE; } + const auto file = g_realStorageCreate(path, access, share, security, disposition, flags, templateFile); + if (storage) g_storageHandle = file; + if (report) g_reportOutputHandle = file; + return file; +} +BOOL WINAPI StorageWrite(HANDLE file, LPCVOID bytes, DWORD size, LPDWORD written, LPOVERLAPPED overlapped) { + if (file == g_reportOutputHandle && g_storageFault == 7) { *written = 0; SetLastError(ERROR_DISK_FULL); return FALSE; } + if (file == g_storageHandle && g_storageFault == 2) { *written = 0; SetLastError(ERROR_DISK_FULL); return FALSE; } + if (file == g_storageHandle && g_storageFault == 4) WaitForSingleObject(g_releaseStorageWrite, 10000); + return g_realStorageWrite(file, bytes, size, written, overlapped); +} +BOOL WINAPI StorageRead(HANDLE file, LPVOID bytes, DWORD size, LPDWORD read, LPOVERLAPPED overlapped) { + if (file == g_storageHandle && g_storageFault == 5) { SetEvent(g_storageReadEntered); WaitForSingleObject(g_releaseStorageRead, 10000); } + if (file == g_storageHandle && g_storageFault == 3) { *read = 0; SetLastError(ERROR_READ_FAULT); return FALSE; } + return g_realStorageRead(file, bytes, size, read, overlapped); +} +struct StorageFaultImports { + std::vector> patched; + StorageFaultImports() { + const auto base = reinterpret_cast(GetModuleHandleW(nullptr)); + const auto dos = reinterpret_cast(base); + const auto nt = reinterpret_cast(base + dos->e_lfanew); + const auto directory = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; + auto descriptor = reinterpret_cast(base + directory.VirtualAddress); + for (; descriptor->Name; ++descriptor) { + auto thunk = reinterpret_cast(base + descriptor->FirstThunk); + for (; thunk->u1.Function; ++thunk) { + const auto original = thunk->u1.Function; + ULONG_PTR replacement = 0; + if (original == reinterpret_cast(g_realStorageCreate)) replacement = reinterpret_cast(&StorageCreate); + if (original == reinterpret_cast(g_realStorageWrite)) replacement = reinterpret_cast(&StorageWrite); + if (original == reinterpret_cast(g_realStorageRead)) replacement = reinterpret_cast(&StorageRead); + if (!replacement) continue; + auto* slot = &thunk->u1.Function; + DWORD protection = 0; VirtualProtect(slot, sizeof(*slot), PAGE_READWRITE, &protection); + *slot = replacement; DWORD ignored = 0; VirtualProtect(slot, sizeof(*slot), protection, &ignored); + patched.emplace_back(slot, original); + } + } + } + ~StorageFaultImports() { + for (const auto& entry : patched) { + DWORD protection = 0; VirtualProtect(entry.first, sizeof(*entry.first), PAGE_READWRITE, &protection); + *entry.first = entry.second; DWORD ignored = 0; VirtualProtect(entry.first, sizeof(*entry.first), protection, &ignored); + } + } +}; + +using TelemetryStartFn = HRESULT (STDMETHODCALLTYPE*)(ID3D9BridgeTelemetry*, const D3D9BridgeTelemetryConfig*); +using TelemetryStopFn = HRESULT (STDMETHODCALLTYPE*)(ID3D9BridgeTelemetry*, UINT64); +TelemetryStartFn g_realTelemetryStart = nullptr; +TelemetryStopFn g_realTelemetryStop = nullptr; +unsigned g_telemetryBoundaryFault = 0; +HANDLE g_backendUnmapEntered = nullptr, g_releaseBackendUnmap = nullptr, g_backendUnmapExited = nullptr; +std::atomic g_pauseBackendUnmap{false}; +std::atomic g_backendUnmapThread{0}; +decltype(&UnmapViewOfFile) g_realBackendUnmap = nullptr; +BOOL WINAPI PauseBackendUnmap(LPCVOID address) { + const bool pause = g_pauseBackendUnmap.exchange(false); + if (pause) { + g_backendUnmapThread = GetCurrentThreadId(); + SetEvent(g_backendUnmapEntered); + WaitForSingleObject(g_releaseBackendUnmap, 5000); + } + const BOOL result = g_realBackendUnmap(address); + if (pause) SetEvent(g_backendUnmapExited); + return result; +} +HRESULT STDMETHODCALLTYPE TelemetryStartBoundary(ID3D9BridgeTelemetry* self, const D3D9BridgeTelemetryConfig* config) { + if (g_telemetryBoundaryFault == 1) return E_FAIL; + if (g_telemetryBoundaryFault == 2) { g_realTelemetryStart(self, config); throw std::runtime_error("fixture telemetry start failure after real StartCapture"); } + // Exercise a real backend ring overflow with a small test-only capacity. + if (g_telemetryBoundaryFault == 5) { + auto limited = *config; limited.MaxEvents = 1; + return g_realTelemetryStart(self, &limited); + } + return g_realTelemetryStart(self, config); +} +HRESULT STDMETHODCALLTYPE TelemetryStopBoundary(ID3D9BridgeTelemetry* self, UINT64 id) { + if (g_telemetryBoundaryFault == 3) return E_FAIL; + if (g_telemetryBoundaryFault == 4) throw std::runtime_error("fixture telemetry stop failure"); + if (g_telemetryBoundaryFault == 5 || g_telemetryBoundaryFault == 6) { + // The control finishes any real in-flight backend spans before stop. + const auto deadline = GetTickCount64() + 4000; + D3D9BridgeTelemetryRead read{}; + do { + read.StructSize = sizeof(read); read.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + if (SUCCEEDED(self->ReadEvents(id, &read, nullptr, 0)) && !read.InFlight) break; + Sleep(1); + } while (GetTickCount64() < deadline); + } + const auto result = g_realTelemetryStop(self, id); + if (g_telemetryBoundaryFault == 7 && SUCCEEDED(result)) { + // Let the real blocked operation finish after stop but before the host's final read. + SetEvent(g_releaseBackendUnmap); + const auto deadline = GetTickCount64() + 4000; + D3D9BridgeTelemetryRead read{}; + do { + read.StructSize = sizeof(read); read.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + if (SUCCEEDED(self->ReadEvents(id, &read, nullptr, 0)) && read.LateDropped) break; + Sleep(1); + } while (GetTickCount64() < deadline); + } + return result; +} +struct TelemetryBoundaryImports { + ID3D9BridgeTelemetry* telemetry = nullptr; + void** original = nullptr; + std::array methods{}; + bool patched = false; + bool SetMethods(void* start, void* stop) { + DWORD protection = 0; + if (!VirtualProtect(original + 3, 2 * sizeof(void*), PAGE_READWRITE, &protection)) return false; + original[3] = start; original[4] = stop; + DWORD ignored = 0; + const bool restored = VirtualProtect(original + 3, 2 * sizeof(void*), protection, &ignored) != FALSE; + Check(restored, "telemetry fixture restores original vtable page protection"); + return true; + } + TelemetryBoundaryImports(IDirect3DDevice9* device, unsigned fault) { + // No instrumentation in the normal/benchmark path. Cloning an apphelp + // COM-router vtable copies its Release hook to an unregistered identity + // and can route Release recursively back to that same hook. + if (!fault) return; + if (FAILED(device->QueryInterface(IID_D3D9BridgeTelemetry, reinterpret_cast(&telemetry))) || !telemetry) return; + original = *reinterpret_cast(telemetry); + methods = { original[3], original[4] }; + g_realTelemetryStart = reinterpret_cast(methods[0]); + g_realTelemetryStop = reinterpret_cast(methods[1]); + g_telemetryBoundaryFault = fault; + patched = SetMethods(reinterpret_cast(&TelemetryStartBoundary), reinterpret_cast(&TelemetryStopBoundary)); + Check(patched, "telemetry fixture patches only private StartCapture and StopCapture slots"); + } + ~TelemetryBoundaryImports() { + if (telemetry) { + if (patched) Check(SetMethods(methods[0], methods[1]), "telemetry fixture restores original private method slots"); + g_telemetryBoundaryFault = 0; + telemetry->StopCapture(Snapshot().captureId); + telemetry->Release(); + } + } +}; +struct TelemetryVtableProbe { + ID3D9BridgeTelemetry* telemetry = nullptr; + void** table = nullptr; + std::array unknown{}; + explicit TelemetryVtableProbe(IDirect3DDevice9* device) { + if (FAILED(device->QueryInterface(IID_D3D9BridgeTelemetry, reinterpret_cast(&telemetry))) || !telemetry) return; + table = *reinterpret_cast(telemetry); + std::copy(table, table + unknown.size(), unknown.begin()); + } + bool Matches() const { + return !telemetry || (*reinterpret_cast(telemetry) == table && + std::equal(unknown.begin(), unknown.end(), table)); + } + ~TelemetryVtableProbe() { if (telemetry) telemetry->Release(); } +}; + +// Pause only a real backend UnmapViewOfFile call. The finite wait and destructor +// release ensure a failed assertion cannot strand the retirement worker. +struct HeldBackendUnmap { + ImportHook hook; + bool installed = false; + explicit HeldBackendUnmap(HMODULE module) { + g_backendUnmapEntered = CreateEventW(nullptr, TRUE, FALSE, nullptr); + g_releaseBackendUnmap = CreateEventW(nullptr, TRUE, FALSE, nullptr); + g_backendUnmapExited = CreateEventW(nullptr, TRUE, FALSE, nullptr); + if (g_backendUnmapEntered && g_releaseBackendUnmap && g_backendUnmapExited) { + installed = hook.install(module, "UnmapViewOfFile", reinterpret_cast(&PauseBackendUnmap)); + if (installed) g_realBackendUnmap = reinterpret_cast(hook.original); + } + Check(installed, "test installs bounded backend unmap timing hook"); + } + void Release() { if (g_releaseBackendUnmap) SetEvent(g_releaseBackendUnmap); } + ~HeldBackendUnmap() { + g_pauseBackendUnmap = false; + Release(); + if (g_backendUnmapEntered && WaitForSingleObject(g_backendUnmapEntered, 0) == WAIT_OBJECT_0) + Check(WaitForSingleObject(g_backendUnmapExited, 10000) == WAIT_OBJECT_0, + "blocked backend unmap leaves its test-only timing hook"); + if (g_backendUnmapEntered) CloseHandle(g_backendUnmapEntered); + if (g_releaseBackendUnmap) CloseHandle(g_releaseBackendUnmap); + if (g_backendUnmapExited) CloseHandle(g_backendUnmapExited); + g_backendUnmapEntered = g_releaseBackendUnmap = g_backendUnmapExited = nullptr; + } +}; + +void TestTelemetryCaptureCompleteness(DeviceFixture& device, const char* mode) +{ + const bool overflow = std::strcmp(mode, "overflow") == 0; + const bool activeControl = std::strcmp(mode, "active-complete") == 0; + const bool late = std::strcmp(mode, "late-drop") == 0; + const unsigned boundaryMode = overflow ? 5 : activeControl ? 6 : late ? 7 : 0; + RuntimeFixture runtime(mode); + runtime.config.cpu = runtime.config.csCpu = runtime.config.context = runtime.config.journal = false; + runtime.config.timing = {500, 1, 20, 50}; + runtime.Configure(); + RuntimePrepareDevice(device.device); + TelemetryBoundaryImports boundary(device.device, boundaryMode); + ID3D9BridgeTelemetry* telemetry = nullptr; + const bool supported = SUCCEEDED(device.device->QueryInterface(IID_D3D9BridgeTelemetry, + reinterpret_cast(&telemetry))) && telemetry; + Check(supported && (!boundaryMode || boundary.patched), "real backend telemetry is available for completeness test"); + if (!supported || (boundaryMode && !boundary.patched)) { if (telemetry) telemetry->Release(); return; } + + std::unique_ptr held; + std::array textures{}; + if (!overflow) { + held = std::make_unique(device.module); + if (!held->installed) { telemetry->Release(); return; } + for (auto& texture : textures) { + const auto hr = device.device->CreateTexture(1024, 1024, 1, 0, D3DFMT_A8R8G8B8, + D3DPOOL_SYSTEMMEM, &texture, nullptr); + Check(SUCCEEDED(hr) && texture, "mapped texture is created for real deferred retirement"); + } + if (!textures[0] || !textures[1] || !textures[2]) { + for (auto* texture : textures) if (texture) texture->Release(); + telemetry->Release(); return; + } + } + + g_f8 = true; device.Present(); g_f8 = false; + const auto capture = Snapshot().captureId; + Check(capture != 0, "completeness fixture starts a real runtime capture"); + if (overflow) { + for (unsigned i = 0; i < 8; ++i) { + IDirect3DTexture9* texture = nullptr; + const auto hr = device.device->CreateTexture(256, 256, 1, 0, D3DFMT_A8R8G8B8, + D3DPOOL_SYSTEMMEM, &texture, nullptr); + Check(SUCCEEDED(hr) && texture, "overflow fixture creates a real mapped texture"); + if (texture) texture->Release(); + } + } else { + g_backendUnmapThread = 0; + g_pauseBackendUnmap = true; + textures[0]->Release(); textures[0] = nullptr; + const bool entered = WaitForSingleObject(g_backendUnmapEntered, 4000) == WAIT_OBJECT_0; + Check(entered && g_backendUnmapThread.load() != GetCurrentThreadId(), + "real deferred retirement is paused on its background thread"); + if (!entered) { held->Release(); RuntimeInterrupt(device.device, "unmap hook timeout"); + for (auto* texture : textures) if (texture) texture->Release(); telemetry->Release(); return; } + } + + auto readCounters = [&]() { + D3D9BridgeTelemetryRead read{}; + read.StructSize = sizeof(read); read.Version = D3D9_BRIDGE_TELEMETRY_VERSION; + Check(SUCCEEDED(telemetry->ReadEvents(capture, &read, nullptr, 0)), "real backend counters are readable"); + return read; + }; + const auto active = readCounters(); + if (overflow) Check(active.Active && active.Capacity == 1 && active.Dropped > 0, + "real backend ring overflows under the test-only one-event capacity"); + else { + Check(active.Active && active.InFlight > 0, "real backend reports the held span while capture is active"); + device.Present(); + Check((Snapshot().unavailableMask & RuntimeTelemetryUnavailable) == 0, + "an active in-flight span alone does not mark telemetry unavailable"); + } + + if (activeControl) { + held->Release(); + Check(WaitForSingleObject(g_backendUnmapExited, 5000) == WAIT_OBJECT_0, + "active backend span is released before capture stop"); + const auto deadline = GetTickCount64() + 4000; + D3D9BridgeTelemetryRead read{}; + do { read = readCounters(); if (!read.InFlight) break; Sleep(1); } while (GetTickCount64() < deadline); + Check(!read.InFlight && !read.Dropped && !read.LateDropped, + "active control actually drains backend spans without event loss"); + } + + if (overflow || activeControl) { + Check(WaitPhase(device, Phase::Complete), "completeness fixture reaches normal capture completion"); + } else { + RuntimeInterrupt(device.device, "telemetry stop boundary fixture"); + } + auto stopped = readCounters(); + if (!overflow && !activeControl && !late) { + Check(!stopped.Active && stopped.InFlightAtStop > 0 && stopped.InFlight > 0, + "stop records the still-blocked real backend span before it can finish late"); + held->Release(); + } else if (late) { + Check(!stopped.Active && stopped.InFlightAtStop > 0 && stopped.LateDropped > 0, + "real backend counts the span finishing after StopCapture"); + } else { + Check(!stopped.Active && !stopped.InFlightAtStop && !stopped.InFlight && !stopped.LateDropped, + "normal stop observes no unfinished or late backend spans"); + } + if (held) held->Release(); + for (auto* texture : textures) if (texture) texture->Release(); + + auto snapshot = Snapshot(); + const auto deadline = GetTickCount64() + 15000; + while (snapshot.workerBusy && GetTickCount64() < deadline) { Sleep(1); snapshot = Snapshot(); } + Check(!snapshot.workerBusy && snapshot.telemetryReportFinished, "capture report and storage workers finish"); + std::ifstream input(snapshot.reportPath); + std::string header, line, telemetryLine, storageLine; + while (std::getline(input, line)) { + if (line.rfind("# measurement ", 0) == 0) header = line; + if (line.rfind("telemetry available=", 0) == 0) telemetryLine = line; + if (line.rfind("telemetryStorage ", 0) == 0) storageLine = line; + } + const auto value = [](const std::string& row, const char* key) { + const auto p = row.find(key); + return p == std::string::npos ? 0ull : std::strtoull(row.c_str() + p + std::strlen(key), nullptr, 10); + }; + const bool incomplete = !activeControl; + Check(((snapshot.unavailableMask & RuntimeTelemetryUnavailable) != 0) == incomplete, + "terminal runtime telemetry availability matches actual backend completeness"); + Check(((value(header, "unavailableMask=") & RuntimeTelemetryUnavailable) != 0) == incomplete, + "final report header carries the same telemetry availability"); + Check((value(storageLine, "complete=") == 0) == incomplete, + "final storage completeness matches the terminal telemetry status"); + if (overflow) Check(value(telemetryLine, "backendDropped=") > 0 && header.find("status=complete-partial") != std::string::npos, + "real backend overflow produces a completed partial report"); + if (!overflow && !activeControl && !late) + Check(value(telemetryLine, "inFlightAtStop=") > 0 && value(telemetryLine, "inFlight=") > 0, + "report preserves a real backend span still open at stop and final read"); + if (late) Check(value(telemetryLine, "inFlightAtStop=") > 0 && value(telemetryLine, "lateDroppedAtRead=") > 0, + "report preserves actual stop-in-flight and late-drop counters"); + if (activeControl) Check(value(telemetryLine, "backendDropped=") == 0 && + value(telemetryLine, "inFlightAtStop=") == 0 && value(telemetryLine, "lateDroppedAtRead=") == 0, + "completed control has no backend telemetry gap"); + runtime.Stop(); + telemetry->Release(); +} + +void TestEventStream(DeviceFixture& device, const char* mode, bool correctness) +{ + const bool sealRace = std::strcmp(mode, "sealed-empty") == 0; +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) + if (sealRace) { + detail::g_emptyStorageSnapshot = CreateEventW(nullptr, TRUE, FALSE, nullptr); + detail::g_releaseEmptyStorage = CreateEventW(nullptr, TRUE, FALSE, nullptr); + } +#else + if (sealRace) { Check(false, "sealed-empty regression requires the test-only storage synchronization build"); return; } +#endif + const bool inactive = std::strcmp(mode, "inactive") == 0; + const bool high = std::strcmp(mode, "high") == 0; + const unsigned fault = std::strcmp(mode, "open") == 0 ? 1 : std::strcmp(mode, "write") == 0 ? 2 : + std::strcmp(mode, "read") == 0 ? 3 : std::strcmp(mode, "queue") == 0 ? 4 : std::strcmp(mode, "handoff") == 0 ? 5 : std::strcmp(mode, "report-open") == 0 ? 6 : std::strcmp(mode, "report-write") == 0 ? 7 : std::strcmp(mode, "cancelled") == 0 ? 8 : 0; + const bool cap = std::strcmp(mode, "cap") == 0; + const unsigned boundaryFault = std::strcmp(mode, "start-fail") == 0 ? 1 : std::strcmp(mode, "start-throw") == 0 ? 2 : + std::strcmp(mode, "stop-fail") == 0 ? 3 : std::strcmp(mode, "stop-throw") == 0 ? 4 : 0; + TelemetryVtableProbe identity(device.device); + TelemetryBoundaryImports boundary(device.device, boundaryFault); + Check(identity.Matches(), "fixture preserves native telemetry vtable and IUnknown compatibility-router identity"); + StorageFaultImports imports; + g_storageFault = fault; g_storageHandle = g_reportOutputHandle = INVALID_HANDLE_VALUE; + g_releaseStorageWrite = CreateEventW(nullptr, TRUE, FALSE, nullptr); + g_storageReadEntered = CreateEventW(nullptr, TRUE, FALSE, nullptr); + g_releaseStorageRead = CreateEventW(nullptr, TRUE, FALSE, nullptr); + const bool workerFault = std::strcmp(mode, "worker") == 0; + const bool allocationFault = std::strcmp(mode, "allocation") == 0; + RuntimeFixture runtime("event-stream"); + runtime.config.cpu = runtime.config.csCpu = runtime.config.context = runtime.config.journal = false; + runtime.config.timing = {cap ? 120000u : 30000u, 1, 1, 50}; + runtime.Configure(); + RuntimePrepareDevice(device.device); + ID3D9BridgeTelemetry* telemetry = nullptr; + const bool supported = SUCCEEDED(device.device->QueryInterface(IID_D3D9BridgeTelemetry, + reinterpret_cast(&telemetry))) && telemetry; + if (telemetry) telemetry->Release(); + if (!supported && !inactive) { + runtime.Stop(); + CloseHandle(g_releaseStorageWrite); CloseHandle(g_storageReadEntered); CloseHandle(g_releaseStorageRead); + g_releaseStorageWrite = g_storageReadEntered = g_releaseStorageRead = nullptr; + g_storageFault = 0; +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) + if (sealRace) { + CloseHandle(detail::g_emptyStorageSnapshot); CloseHandle(detail::g_releaseEmptyStorage); + detail::g_emptyStorageSnapshot = detail::g_releaseEmptyStorage = nullptr; + } +#endif + return; + } + UINT64 startPresentNs = 0, startCycles = 0, startCycleBegin = 0, startCycleEnd = 0; + if (!inactive) { + if (workerFault) g_failOneWorker = true; + if (allocationFault) { g_failAllocationThread = GetCurrentThreadId(); g_failAllocationSize = 32768 * sizeof(D3D9BridgeTelemetryEvent); } + QueryThreadCycleTime(GetCurrentThread(), &startCycleBegin); + const auto startPresentBegin = BenchmarkNowNs(); + g_f8 = true; device.Present(); g_f8 = false; + startPresentNs = BenchmarkNowNs() - startPresentBegin; + QueryThreadCycleTime(GetCurrentThread(), &startCycleEnd); startCycles = startCycleEnd - startCycleBegin; + if (allocationFault) Check(g_failAllocationSize.exchange(0) == 0, "capture event staging allocation fault is exercised"); + } + // Explicit untimed warmup uses the same actual allocation/map/release path. + for (unsigned i = 0; i < 32; ++i) { + IDirect3DTexture9* warm = nullptr; + if (SUCCEEDED(device.device->CreateTexture(128, 128, 1, 0, D3DFMT_A8R8G8B8, + D3DPOOL_SYSTEMMEM, &warm, nullptr)) && warm) { D3DLOCKED_RECT lock{}; if (SUCCEEDED(warm->LockRect(0, &lock, nullptr, 0))) warm->UnlockRect(0); warm->Release(); } + } + device.Present(); + UINT64 cyclesBegin = 0, cyclesEnd = 0; + QueryThreadCycleTime(GetCurrentThread(), &cyclesBegin); + const auto started = BenchmarkNowNs(); + UINT64 maxPresentNs = 0, successfulPresents = 0, failedPresents = 0; + const unsigned operations = cap ? 600000 : high ? 80000 : fault == 4 ? 6000 : (fault || boundaryFault || workerFault || allocationFault || cap) ? 256 : 4096; + bool workOk = true; + std::uint64_t lateBegin = 0, lateEnd = 0; + std::vector latencies; + latencies.reserve(1 + operations / 32); + auto batchBegin = BenchmarkNowNs(); + for (unsigned i = 0; i <= operations; ++i) { + const auto begin = BenchmarkNowNs(); + if (i == operations) lateBegin = begin; + IDirect3DTexture9* texture = nullptr; + const UINT width = i == operations ? 516 : 128; + if (SUCCEEDED(device.device->CreateTexture(width, 128, 1, 0, + D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &texture, nullptr)) && texture) { + D3DLOCKED_RECT lock{}; + if (SUCCEEDED(texture->LockRect(0, &lock, nullptr, 0))) { + std::memset(lock.pBits, int(i), width * 4); + workOk &= SUCCEEDED(texture->UnlockRect(0)); + } else workOk = false; + texture->Release(); + } else workOk = false; + if (i == operations) lateEnd = BenchmarkNowNs(); + if (i % 32 == 0 || i == operations) { + const auto presentBegin = BenchmarkNowNs(); + const auto result = device.Present(); + maxPresentNs = (std::max)(maxPresentNs, BenchmarkNowNs() - presentBegin); + if (result == S_OK) ++successfulPresents; else ++failedPresents; + workOk &= result == S_OK; + latencies.push_back(BenchmarkNowNs() - batchBegin); + batchBegin = BenchmarkNowNs(); + } + } + IDirect3DQuery9* fence = nullptr; + workOk &= SUCCEEDED(device.device->CreateQuery(D3DQUERYTYPE_EVENT, &fence)) && fence; + if (fence) { + workOk &= SUCCEEDED(fence->Issue(D3DISSUE_END)); + const auto drainDeadline = GetTickCount64() + 10000; + HRESULT ready = S_FALSE; + while (ready == S_FALSE && GetTickCount64() < drainDeadline) { ready = fence->GetData(nullptr, 0, D3DGETDATA_FLUSH); Sleep(0); } + workOk &= ready == S_OK; fence->Release(); + } +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) + if (sealRace) { + // All earlier real resource events have been published by Present. Hold + // the actual storage writer after an empty head snapshot, then leave + // the final real backend resource batch for the producer's last drain. + detail::g_pauseEmptyStorage = true; + const bool paused = WaitForSingleObject(detail::g_emptyStorageSnapshot, 5000) == WAIT_OBJECT_0; + Check(paused, "sealed-empty interleaving pauses actual storage after its empty head snapshot"); + if (!paused) detail::g_pauseEmptyStorage = false; + lateBegin = BenchmarkNowNs(); + IDirect3DTexture9* finalTexture = nullptr; + workOk &= SUCCEEDED(device.device->CreateTexture(520, 128, 1, 0, + D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &finalTexture, nullptr)) && finalTexture; + if (finalTexture) { + D3DLOCKED_RECT lock{}; + const auto locked = finalTexture->LockRect(0, &lock, nullptr, 0); + workOk &= SUCCEEDED(locked); + if (SUCCEEDED(locked)) { std::memset(lock.pBits, 42, 520 * 4); workOk &= SUCCEEDED(finalTexture->UnlockRect(0)); } + finalTexture->Release(); + } + lateEnd = BenchmarkNowNs(); + // Close the real frame interval and publish its resource events while + // the writer still holds the empty snapshot. Interrupt then seals it. + workOk &= device.Present() == S_OK; + } +#endif + const auto workNs = BenchmarkNowNs() - started; + QueryThreadCycleTime(GetCurrentThread(), &cyclesEnd); + SetEvent(g_releaseStorageWrite); + if (fault == 1 || fault == 2) { + const auto omissionDeadline = GetTickCount64() + 1000; + while (!Snapshot().telemetryDropped && GetTickCount64() < omissionDeadline) Sleep(1); + const auto active = Snapshot(); + Check(!active.telemetryReportFinished && !active.telemetryEvents && active.telemetryDropped > 0, + "active snapshot counts spooled records and never counts omitted queue acceptance as retained"); + } + if (fault == 8) { + g_reportWorkerStarted = CreateEventW(nullptr, TRUE, FALSE, nullptr); + g_releaseReportWorker = CreateEventW(nullptr, TRUE, FALSE, nullptr); + g_delayNextWorker = true; + } + UINT64 stopCyclesBegin = 0, stopCyclesEnd = 0, completionInterruptNs = 0; + if (!inactive) { + QueryThreadCycleTime(GetCurrentThread(), &stopCyclesBegin); + const auto stopBegin = BenchmarkNowNs(); + RuntimeInterrupt(device.device, "event-stream-work-complete"); + completionInterruptNs = BenchmarkNowNs() - stopBegin; + QueryThreadCycleTime(GetCurrentThread(), &stopCyclesEnd); + } +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) + if (sealRace) SetEvent(detail::g_releaseEmptyStorage); +#endif + if (fault == 8) { + Check(WaitForSingleObject(g_reportWorkerStarted, 5000) == WAIT_OBJECT_0, "cancelled storage test delays the actual report worker before reader handoff"); + SetEvent(runtime.stopEvent); + Check(!g_workers.empty() && WaitForSingleObject(g_workers.front(), 5000) == WAIT_OBJECT_0, + "shutdown closes unclaimed storage before report reader starts"); + SetEvent(g_releaseReportWorker); + } + if (fault == 5) { + Check(WaitForSingleObject(g_storageReadEntered, 5000) == WAIT_OBJECT_0, "shutdown handoff reaches actual report-file read"); + SetEvent(runtime.stopEvent); + Sleep(5); + Check(GetFileType(g_storageHandle) != FILE_TYPE_UNKNOWN, "reader-owned temporary file remains open across shutdown"); + SetEvent(g_releaseStorageRead); + } + const auto reportBegin = BenchmarkNowNs(); + auto snapshot = Snapshot(); + const auto deadline = GetTickCount64() + (boundaryFault ? 3000 : cap ? 120000 : 30000); + while (snapshot.workerBusy && GetTickCount64() < deadline) { Sleep(1); snapshot = Snapshot(); } + const auto reportNs = BenchmarkNowNs() - reportBegin; + if (boundaryFault && snapshot.workerBusy) { + Check(false, "backend start/stop failure cannot leave report worker waiting on an unlaunched storage worker"); + std::fflush(stdout); TerminateProcess(GetCurrentProcess(), 87); + } + std::ifstream input(snapshot.reportPath); + std::string line; + std::uint64_t events = 0, tailIndex = 0, overlaps = 0, backendDropped = 0, hostDropped = 0; + std::string storageLine, telemetryLine, reportHeader, lastReportLine; + bool contextSummaryPresent = false; + bool late = false; + while (std::getline(input, line)) { + if (!line.empty()) lastReportLine = line; + contextSummaryPresent |= line.rfind("contextSummary ", 0) == 0; + if (line.rfind("# measurement ", 0) == 0) reportHeader = line; + if (line.rfind("telemetryStorage ", 0) == 0) storageLine = line; + if (line.rfind("telemetry available=", 0) == 0) { + telemetryLine = line; + const auto parse = [&](const char* key) { const auto pos = line.find(key); return pos == std::string::npos ? 0ull : std::strtoull(line.c_str() + pos + std::strlen(key), nullptr, 10); }; + backendDropped = parse("backendDropped="); hostDropped = parse("hostDropped="); + } + if (line.rfind("telemetryEvent ", 0) == 0) { + ++events; + unsigned long long index=0, id=0, begin=0, end=0; + unsigned kind=0; + char kindName[128]{}; + if (std::sscanf(line.c_str(), "telemetryEvent index=%llu captureId=%llu kind=%u kindName=%127s beginNs=%llu endNs=%llu", + &index, &id, &kind, kindName, &begin, &end) == 6 && begin >= lateBegin && end <= lateEnd) { + late = true; tailIndex = index; + } + } + if (line.rfind("frameOverlap event=" + std::to_string(tailIndex) + " ", 0) == 0 && late) ++overlaps; + } + const auto valueFrom = [](const std::string& row, const char* key) { + const auto position = row.find(key); + return position == std::string::npos ? 0ull : std::strtoull(row.c_str() + position + std::strlen(key), nullptr, 0); + }; + const bool reportRequested = !inactive; + const bool workersFinished = !snapshot.workerBusy; + const bool storageMetadataPresent = !storageLine.empty(); + const bool finalizedHeader = !reportHeader.empty() && reportHeader.find("status=pending") == std::string::npos && + valueFrom(reportHeader, "captureId=") == snapshot.captureId; + const bool finalRecord = lastReportLine.rfind("reportEnd ", 0) == 0 && + valueFrom(lastReportLine, "captureId=") == snapshot.captureId && valueFrom(lastReportLine, "completed=") == 1; + // Preserved baseline RuntimeSource predates reportEnd. Its existing final + // context/error row is accepted explicitly as the legacy format terminator. + const bool legacyFinalRecord = !storageMetadataPresent && contextSummaryPresent && + (lastReportLine.rfind("contextSummary ", 0) == 0 || lastReportLine.rfind("error=", 0) == 0); + const bool reportComplete = reportRequested && workersFinished && input.is_open() && !input.bad() && + finalizedHeader && (finalRecord || legacyFinalRecord) && snapshot.phase != Phase::Error; + const auto storageErrors = storageMetadataPresent ? valueFrom(storageLine, "errors=") : snapshot.telemetryStorageErrors; + const auto storageUnread = storageMetadataPresent ? valueFrom(storageLine, "unread=") : snapshot.telemetryUnread; + const char* storageStatusSource = inactive ? "inactive" : storageMetadataPresent ? "report-footer" : + snapshot.telemetryReportFinished ? "runtime-snapshot" : "legacy-counters"; + const bool legacyComplete = reportComplete && !backendDropped && !hostDropped && + valueFrom(telemetryLine, "available=") && !valueFrom(telemetryLine, "active=") && + !valueFrom(telemetryLine, "remaining=") && !valueFrom(telemetryLine, "inFlight=") && + !valueFrom(telemetryLine, "inFlightAtStop=") && !valueFrom(telemetryLine, "lateDroppedAtRead=") && + telemetryLine.find("error=\"\"") != std::string::npos; + const bool storageComplete = storageMetadataPresent ? valueFrom(storageLine, "complete=") == 1 : legacyComplete; + const bool valid = workOk && workersFinished && (!reportRequested || reportComplete); + Check(workOk && !snapshot.workerBusy, "event-stream real resource work, Present and GPU drain finish"); + if ((fault == 6 || fault == 7) && correctness) { + Check(snapshot.phase == Phase::Error && !reportComplete, + "report output open/write failure cannot be reported as successful report completion"); + Check(snapshot.telemetryEvents == 0 && snapshot.telemetryDropped == snapshot.telemetryUnread && snapshot.telemetryUnread > 0, + "failed report leaves zero finalized events and accounts unfinalized stream as dropped"); + } else if (allocationFault && correctness) { + Check((snapshot.unavailableMask & RuntimeTelemetryUnavailable) && !snapshot.workerBusy && + telemetryLine.find("storage-allocation-failed") != std::string::npos, + "event staging allocation failure reports unavailable and finishes without worker/file leak"); + } else if (((fault && fault != 5) || cap || boundaryFault || workerFault) && correctness) { + const auto parse = [&](const char* key) { const auto position = storageLine.find(key); return position == std::string::npos ? 0ull : std::strtoull(storageLine.c_str()+position+std::strlen(key), nullptr, 0); }; + const auto received = parse("received="), written = parse("written="), omitted = parse("omitted="); + Check(storageLine.find("complete=0") != std::string::npos && (parse("errors=") || boundaryFault) && + (snapshot.unavailableMask & RuntimeTelemetryUnavailable), "storage fault remains explicitly incomplete after report generation"); + const auto maskPosition = reportHeader.find("unavailableMask="); + const auto headerMask = maskPosition == std::string::npos ? 0 : std::strtoul(reportHeader.c_str() + maskPosition + 16, nullptr, 10); + Check((headerMask & RuntimeTelemetryUnavailable) != 0, "final report header retains storage read/write errors"); + Check(received == written + omitted && written == events + parse("unread="), + "storage fault conserves received, written, omitted, reported and unread counts"); + Check(snapshot.telemetryEvents == events && snapshot.telemetryEvents + snapshot.telemetryDropped == received + backendDropped, + "terminal snapshot conserves actual reported events and omitted/unread losses"); + Check(StorageTempFiles() == runtime.initialStorageFiles, + "storage worker removes its capture temporary file before completion"); + } else if (!inactive && correctness) { + Check(!high || events > 65536, "event stream retains more than the former lifetime limit"); + Check(late && overlaps > 0 && (!high || tailIndex >= 65536), "known late real event retains its frame association"); + Check(events == snapshot.telemetryEvents && !snapshot.telemetryDropped, + "event stream report and snapshot conserve retained events without loss"); + if (sealRace) { + Check(events > 0 && valueFrom(storageLine, "received=") == events && + valueFrom(storageLine, "accepted=") == events && valueFrom(storageLine, "written=") == events && + valueFrom(storageLine, "reported=") == events, + "sealed-empty final report physically conserves received accepted written and reported events"); + Check(!storageErrors && !storageUnread && !valueFrom(storageLine, "omitted=") && + snapshot.telemetryReportFinished && !snapshot.telemetryUnread && !snapshot.telemetryStorageErrors, + "sealed-empty storage terminal state has zero omissions unread records and errors"); + // Present can still be executing on CS when this fixture interrupts + // capture. Its newly recorded chunk span may legitimately cross stop; + // physical storage conservation must not hide that backend boundary. + Check(storageComplete == legacyComplete, + "sealed-empty completeness agrees with actual backend stop and in-flight counters"); + Check(StorageTempFiles() == runtime.initialStorageFiles, + "sealed-empty terminal storage worker removes its temporary file"); + } + } else if (inactive) Check(!snapshot.captureId && !snapshot.workerBusy && g_workers.empty(), "inactive F8 starts no event storage worker"); + if (fault == 6 || fault == 7) { + if (!correctness) Check(valid, "benchmark rejects failed report output"); + } else Check(inactive ? !reportRequested && !reportComplete : reportComplete, + "report completion requires an opened finalized report and valid terminal row"); + std::sort(latencies.begin(), latencies.end()); + const auto percentile = [&](unsigned n) { return latencies.empty() ? 0ull : latencies[(latencies.size()-1)*n/100]; }; + std::printf("{\"type\":\"event-stream-benchmark\",\"mode\":\"%s\",\"operations\":%u,\"warmupOperations\":32,\"latencyScope\":\"resource-batch-and-Present\",\"operationsPerBatch\":32,\"hostWorkerWrapper\":%s,\"workerSchedulingEnabled\":%s,\"workerSchedulingMmcss\":%s,\"processPriorityClass\":%lu,\"mainThreadCycles\":%llu,\"maxPresentNs\":%llu,\"successfulPresents\":%llu,\"failedPresents\":%llu,\"backendDropped\":%llu,\"hostDropped\":%llu,\"valid\":%s,\"workPresentDrainNs\":%llu,\"reportWaitNs\":%llu,\"startPresentNs\":%llu,\"completionInterruptNs\":%llu,\"startCycles\":%llu,\"stopCycles\":%llu,\"totalMainNs\":%llu,\"totalMainThreadCycles\":%llu,\"p95Ns\":%llu,\"p99Ns\":%llu,\"maxNs\":%llu,\"events\":%llu,\"dropped\":%llu,\"unavailableMask\":%u,\"lateEvent\":%s,\"lateFrameOverlaps\":%llu,\"reportRequested\":%s,\"workersFinished\":%s,\"reportComplete\":%s,\"storageMetadataPresent\":%s,\"storageStatusSource\":\"%s\",\"storageComplete\":%s,\"storageErrors\":%llu,\"storageUnread\":%llu,\"snapshotEvents\":%llu,\"snapshotUnread\":%llu,\"snapshotReportFinished\":%s,\"reportPath\":%s}\n", + mode, operations+1, g_matchHostWorkerScheduling ? "true" : "false", g_hostWorkerSchedulingOptions.enabled ? "true" : "false", g_hostWorkerSchedulingOptions.mmcss ? "true" : "false", GetPriorityClass(GetCurrentProcess()), cyclesEnd-cyclesBegin, maxPresentNs, successfulPresents, failedPresents, backendDropped, hostDropped, valid ? "true" : "false", workNs, reportNs, startPresentNs, completionInterruptNs, startCycles, stopCyclesEnd-stopCyclesBegin, startPresentNs+workNs+completionInterruptNs, startCycles+(cyclesEnd-cyclesBegin)+(stopCyclesEnd-stopCyclesBegin), percentile(95), percentile(99), percentile(100), events, + snapshot.telemetryDropped, snapshot.unavailableMask, late ? "true" : "false", overlaps, + reportRequested ? "true" : "false", workersFinished ? "true" : "false", reportComplete ? "true" : "false", + storageMetadataPresent ? "true" : "false", storageStatusSource, storageComplete ? "true" : "false", storageErrors, storageUnread, + snapshot.telemetryEvents, snapshot.telemetryUnread, snapshot.telemetryReportFinished ? "true" : "false", JsonString(snapshot.reportPath).c_str()); + runtime.Stop(); + g_storageFault = 0; + CloseHandle(g_releaseStorageWrite); g_releaseStorageWrite = nullptr; + CloseHandle(g_storageReadEntered); CloseHandle(g_releaseStorageRead); + g_storageReadEntered = g_releaseStorageRead = nullptr; +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) + if (sealRace) { + CloseHandle(detail::g_emptyStorageSnapshot); CloseHandle(detail::g_releaseEmptyStorage); + detail::g_emptyStorageSnapshot = detail::g_releaseEmptyStorage = nullptr; + } +#endif +} + +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) +void TestStorageTerminalAccounting() { + // The old empty-head exit would also skip events published before abort. + // The second case simulates an unexpected accepted-record loss and checks + // that the terminal invariant exposes it instead of inventing omissions. + for (bool unexplainedLoss : {false, true}) { + auto storage = std::make_unique(); + storage->ready = false; + storage->consumed = true; + detail::g_emptyStorageSnapshot = CreateEventW(nullptr, TRUE, FALSE, nullptr); + detail::g_releaseEmptyStorage = CreateEventW(nullptr, TRUE, FALSE, nullptr); + detail::g_pauseEmptyStorage = true; + const auto before = StorageTempFiles(); + std::thread worker([&] { storage->Run(); }); + Check(WaitForSingleObject(detail::g_emptyStorageSnapshot, 5000) == WAIT_OBJECT_0, + "terminal accounting holds actual storage at an empty head snapshot"); + D3D9BridgeTelemetryEvent event{}; + event.CaptureId = 42; + storage->Push(event); + if (unexplainedLoss) storage->tail.store(storage->head.load()); + else storage->abort.store(true, std::memory_order_release); + storage->sealed.store(true, std::memory_order_release); + SetEvent(detail::g_releaseEmptyStorage); + worker.join(); + if (unexplainedLoss) { + Check((storage->errors.load() & 256) && storage->received == 1 && storage->accepted == 1 && + storage->written == 0 && storage->omitted == 0, + "terminal invariant flags unaccounted acceptance without duplicating or inventing loss counts"); + } else { + Check(storage->received == 1 && storage->accepted == 1 && storage->written == 0 && + storage->omitted == 1 && storage->reported == 0 && storage->errors == 64, + "abort observed after empty snapshot drains accepted event exactly once as cancelled omission"); + } + Check(storage->ready && storage->file == INVALID_HANDLE_VALUE && StorageTempFiles() == before, + "terminal accounting joins writer and removes its temporary file"); + CloseHandle(detail::g_emptyStorageSnapshot); CloseHandle(detail::g_releaseEmptyStorage); + detail::g_emptyStorageSnapshot = detail::g_releaseEmptyStorage = nullptr; + } +} +#endif + #endif } int main(int argc, char** argv) { #if defined(HAS_MEASUREMENT_RUNTIME) + const bool eventCorrectness = argc > 1 && std::strcmp(argv[1], "--event-stream-test") == 0; + const bool eventStream = eventCorrectness || (argc > 1 && std::strcmp(argv[1], "--event-stream-benchmark") == 0); const bool benchmark = argc > 1 && std::strcmp(argv[1], "--benchmark") == 0; + const bool coverageBenchmark = argc > 1 && std::strcmp(argv[1], "--coverage-benchmark") == 0; + const bool storageOnly = argc > 1 && std::strcmp(argv[1], "--storage-terminal-only") == 0; + const bool faultOnly = argc > 1 && std::strcmp(argv[1], "--fault-only") == 0; + const bool csAllocOnly = argc > 1 && std::strcmp(argv[1], "--cs-alloc-only") == 0; const bool pendingOnly = argc > 1 && std::strcmp(argv[1], "--pending-stop-only") == 0; + const bool telemetryGapOnly = argc > 1 && std::strcmp(argv[1], "--telemetry-gap-only") == 0; const bool ownerOnly = argc > 1 && std::strcmp(argv[1], "--device-owner-only") == 0; const bool unicodeOnly = argc > 1 && std::strcmp(argv[1], "--unicode-modules-only") == 0; - const char* backend = (benchmark || pendingOnly || ownerOnly || unicodeOnly) ? (argc > 2 ? argv[2] : nullptr) : (argc > 1 ? argv[1] : nullptr); - DeviceFixture device(backend, benchmark ? 640u : 64u, benchmark ? 480u : 64u, benchmark); + g_matchHostWorkerScheduling = (eventStream && !eventCorrectness) || benchmark || coverageBenchmark; + g_hostWorkerSchedulingOptions = renderstack::scheduling::ReadOptions(); + const char* backend = (eventStream || benchmark || coverageBenchmark || storageOnly || faultOnly || csAllocOnly || pendingOnly || telemetryGapOnly || ownerOnly || unicodeOnly) ? (argc > 2 ? argv[2] : nullptr) : (argc > 1 ? argv[1] : nullptr); + DeviceFixture device(backend, (benchmark || coverageBenchmark) ? 640u : 64u, + (benchmark || coverageBenchmark) ? 480u : 64u, benchmark || coverageBenchmark); Check(device.device != nullptr, "real D3D9 HAL device creates for production runtime fixture"); - if (device.device && benchmark) { + if (!benchmark && !coverageBenchmark) TestFrameCorrelation(); + if (device.device && eventStream) { + TestEventStream(device, argc > 3 ? argv[3] : "high", eventCorrectness); + } else if (device.device && telemetryGapOnly) { + for (const char* mode : {"overflow", "active-complete", "stop-inflight", "late-drop"}) + TestTelemetryCaptureCompleteness(device, mode); +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) + } else if (device.device && storageOnly) { + TestStorageTerminalAccounting(); +#endif + } else if (device.device && csAllocOnly) { + TestCsFailureIsolation(device, false); + } else if (device.device && faultOnly) { + TestAllocationFailureAtCaptureEnd(device, false); + TestAllocationFailureAtCaptureEnd(device, true); + TestContextFailureStatus(device); + TestContextAllocationFailure(device); + TestCpuJobAllocationFailure(device); + TestCsFailureIsolation(device, false); + TestCsFailureIsolation(device, true); + TestAuditBoundaryException(device, true); + TestAuditBoundaryException(device, false); + } else if (device.device && coverageBenchmark) { + TestCoverageBenchmark(device, argc > 3 ? std::strtoul(argv[3], nullptr, 10) : 4, + argc > 4 ? std::strtoul(argv[4], nullptr, 10) : 1000, + argc > 5 && std::strcmp(argv[5], "--aa") == 0); + } else if (device.device && benchmark) { IDirect3DSurface9* backBuffer = nullptr; D3DSURFACE_DESC description{}; const bool fullHudViewport = IsWindowVisible(device.window) && @@ -883,10 +2318,39 @@ int main(int argc, char** argv) } else if (device.device && unicodeOnly) { TestUnicodeModuleEnumeration(device); } else if (device.device) { +#if defined(RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC) + TestEventStream(device, "sealed-empty", true); + TestStorageTerminalAccounting(); +#endif + TestEventStream(device, "high", true); + TestEventStream(device, "report-open", true); + TestEventStream(device, "report-write", true); + TestEventStream(device, "open", true); + TestEventStream(device, "write", true); + TestEventStream(device, "read", true); + TestEventStream(device, "queue", true); + TestEventStream(device, "cancelled", true); + TestEventStream(device, "handoff", true); + TestEventStream(device, "worker", true); + TestEventStream(device, "allocation", true); + TestEventStream(device, "start-fail", true); + TestEventStream(device, "start-throw", true); + TestEventStream(device, "stop-fail", true); + TestEventStream(device, "stop-throw", true); TestDisabled(device); TestSecondaryDeviceCannotClaimCapture(device); TestUnicodeModuleEnumeration(device); TestComplete(device); + TestCoverageCapture(device); + TestAllocationFailureAtCaptureEnd(device, false); + TestAllocationFailureAtCaptureEnd(device, true); + TestContextFailureStatus(device); + TestContextAllocationFailure(device); + TestCpuJobAllocationFailure(device); + TestCsFailureIsolation(device, false); + TestCsFailureIsolation(device, true); + TestAuditBoundaryException(device, true); + TestAuditBoundaryException(device, false); TestWorkerFailure(device); TestInterruption(device, false); TestShutdown(device); diff --git a/tests/measurement-runtime-tests.vcxproj b/tests/measurement-runtime-tests.vcxproj index 46b4ab2..ec2541f 100644 --- a/tests/measurement-runtime-tests.vcxproj +++ b/tests/measurement-runtime-tests.vcxproj @@ -7,8 +7,12 @@ $(ProjectDir)..\out\perf\20261027-unified-measurement\runtime\bin\$(ProjectDir)..\out\perf\20261027-unified-measurement\runtime\obj\MeasurementRuntimeTests $(ProjectDir)..\src\bridge\legacy\BridgeMeasurementRuntime.cpp + + true + RENDERSTACK_MEASUREMENT_STORAGE_TEST_SYNC; + - MaxSpeedWIN32;NDEBUG;_WINDOWS;NOMINMAX;%(PreprocessorDefinitions)MultiThreadedstdcpplatestfalse/utf-8 %(AdditionalOptions)$(ProjectDir)..\src\bridge\legacy;$(ProjectDir)..\sdk\include;$(ProjectDir)..\backend\dxvk\include\vulkan\include;$(D3DX9IncludeDir);%(AdditionalIncludeDirectories) + MaxSpeed$(StorageTestDefinitions)WIN32;NDEBUG;_WINDOWS;NOMINMAX;%(PreprocessorDefinitions)MultiThreadedstdcpplatestfalse/utf-8 %(AdditionalOptions)$(ProjectDir)..\src\bridge\legacy;$(ProjectDir)..\sdk\include;$(ProjectDir)..\backend\dxvk\include\vulkan\include;$(D3DX9IncludeDir);%(AdditionalIncludeDirectories) Consoled3d9.lib;uuid.lib;user32.lib;bcrypt.lib;%(AdditionalDependencies) diff --git a/tests/measurement-telemetry-probe.cpp b/tests/measurement-telemetry-probe.cpp new file mode 100644 index 0000000..a412021 --- /dev/null +++ b/tests/measurement-telemetry-probe.cpp @@ -0,0 +1,413 @@ +#include +#include +#include "../backend/dxvk/src/d3d9/d3d9_bridge_telemetry.h" +#include "../backend/dxvk/src/dxvk/dxvk_telemetry.h" +#include +#include +#include +#include +#include +#include +#include +#include + +static std::atomic checks{0}, failures{0}; +#define CHECK(c) do { ++checks; if (!(c)) { ++failures; std::printf("FAIL line=%u %s\n", __LINE__, #c); } } while (0) + +static D3D9BridgeTelemetryConfig Config(UINT64 id, UINT maxEvents = 16384) { + D3D9BridgeTelemetryConfig c{}; c.StructSize=sizeof(c); c.Version=1; + c.CaptureId=id; c.MaxEvents=maxEvents; return c; +} +static D3D9BridgeTelemetryRead Read(ID3D9BridgeTelemetry* t, UINT64 id, + D3D9BridgeTelemetryEvent* events=nullptr, UINT count=0) { + D3D9BridgeTelemetryRead r{}; r.StructSize=sizeof(r); r.Version=1; + CHECK(t->ReadEvents(id,&r,events,count)==S_OK); return r; +} +static D3D9BridgeMemorySnapshot Snapshot(ID3D9BridgeTelemetry* t, UINT64 id, + D3D9BridgeMemoryChunk* chunks, UINT chunkCount, UINT* masks, UINT maskCount) { + D3D9BridgeMemorySnapshot s{}; s.StructSize=sizeof(s); s.Version=1; + CHECK(t->GetMemorySnapshot(id,&s,chunks,chunkCount,masks,maskCount)==S_OK); return s; +} +// Deterministically hold a real recorder token over Stop and Start. A mere +// scheduling race in the device test cannot guarantee this ordering. +static void TestRecorderGenerations() { + dxvk::DxvkTelemetry recorder; + D3D9BridgeTelemetryRead r{}; r.StructSize=sizeof(r); r.Version=1; + CHECK(FAILED(recorder.read(10,&r,nullptr,0))); + auto c=Config(10,2); CHECK(recorder.start(&c)==S_OK); + auto old=recorder.begin(); CHECK(old.generation!=0); + CHECK(recorder.stop(10)==S_OK); + CHECK(recorder.read(10,&r,nullptr,0)==S_OK); + CHECK(r.Started==1 && r.InFlight==1 && r.InFlightAtStop==1); + c=Config(11,2); CHECK(recorder.start(&c)==S_OK); + recorder.finish(old,D3D9_BRIDGE_TELEMETRY_CS_WAIT,123,0,0,0); + CHECK(recorder.read(11,&r,nullptr,0)==S_OK); + CHECK(r.Started==0 && r.InFlight==0 && r.Remaining==0); + auto current=recorder.begin(); recorder.finish(current,D3D9_BRIDGE_TELEMETRY_CS_WAIT,456,0,0,0); + D3D9BridgeTelemetryEvent e[2]{}; + CHECK(recorder.read(11,&r,e,2)==S_OK); + CHECK(r.Written==1 && e[0].CaptureId==11 && e[0].Identity==456); + auto late=recorder.begin(); CHECK(recorder.stop(11)==S_OK); + recorder.finish(late,D3D9_BRIDGE_TELEMETRY_CS_WAIT,789,0,0,0); + CHECK(recorder.read(11,&r,nullptr,0)==S_OK); + CHECK(r.InFlightAtStop==1 && r.InFlight==0 && r.LateDropped==1); + CHECK(FAILED(recorder.start(&c))); + c=Config(12,2); c.MinDurationNs=~UINT64(0); c.MinBytes=~UINT64(0); + CHECK(recorder.start(&c)==S_OK); + auto filtered=recorder.begin(); recorder.finish(filtered,D3D9_BRIDGE_TELEMETRY_ALLOC,1,1,0,0); + try { + dxvk::DxvkTelemetryScope scope(&recorder,D3D9_BRIDGE_TELEMETRY_ALLOC,2,1); + throw std::runtime_error("fixture allocation failure"); + } catch(const std::runtime_error&) { } + CHECK(recorder.stop(12)==S_OK); CHECK(recorder.read(12,&r,e,2)==S_OK); + CHECK(r.Filtered==1 && r.Written==1 && (e[0].Flags&D3D9_BRIDGE_TELEMETRY_FAILED) && e[0].Result<0); + c=Config(13,2); CHECK(recorder.start(&c)==S_OK); + for(unsigned i=0;i<4;i++) { auto token=recorder.begin(); recorder.finish(token,D3D9_BRIDGE_TELEMETRY_ALLOC,i,1,0,0); } + CHECK(recorder.stop(13)==S_OK); CHECK(recorder.read(13,&r,e,2)==S_OK); + CHECK(r.Written==2 && r.Dropped==2 && e[0].Identity==0 && e[1].Identity==1); +} +static void Fence(IDirect3DDevice9* d) { + IDirect3DQuery9* query=nullptr; + if(SUCCEEDED(d->CreateQuery(D3DQUERYTYPE_EVENT,&query)) && query) { + CHECK(SUCCEEDED(query->Issue(D3DISSUE_END))); + HRESULT hr=S_FALSE; const auto deadline=GetTickCount64()+10000; + while(hr==S_FALSE && GetTickCount64()GetData(nullptr,0,D3DGETDATA_FLUSH); + if(hr==S_FALSE) Sleep(1); + } + if(hr!=S_OK) std::printf("fence result=0x%08lx\n",(unsigned long)hr); + CHECK(hr==S_OK); query->Release(); + } else CHECK(false); +} +static void Work(IDirect3DDevice9* d, unsigned iterations, bool waitEach=true) { + for (unsigned i=0;iCreateTexture(512,512,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&source,nullptr))); + CHECK(SUCCEEDED(d->CreateTexture(512,512,1,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&destination,nullptr))); + CHECK(SUCCEEDED(d->CreateVertexBuffer(131072,D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,0,D3DPOOL_DEFAULT,&vb,nullptr))); + if(source && destination) { + D3DLOCKED_RECT rect{}; CHECK(SUCCEEDED(source->LockRect(0,&rect,nullptr,0))); + if(rect.pBits) { for(unsigned y=0;y<512;y++) std::memset(static_cast(rect.pBits)+y*rect.Pitch,int(i&255),512*4); } + CHECK(SUCCEEDED(source->UnlockRect(0))); + CHECK(SUCCEEDED(d->UpdateTexture(source,destination))); + } + if(vb) { + void* data=nullptr; CHECK(SUCCEEDED(vb->Lock(0,0,&data,D3DLOCK_DISCARD))); + if(data) std::memset(data,0x5a,131072); + CHECK(SUCCEEDED(vb->Unlock())); + } + if(waitEach) Fence(d); + if(vb) vb->Release(); if(destination) destination->Release(); if(source) source->Release(); + } +} +static void CheckPixels(IDirect3DDevice9* d) { + IDirect3DTexture9 *source=nullptr,*destination=nullptr; + CHECK(SUCCEEDED(d->CreateTexture(16,16,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&source,nullptr))); + CHECK(SUCCEEDED(d->CreateTexture(16,16,1,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&destination,nullptr))); + if(source && destination) { + D3DLOCKED_RECT r{}; CHECK(SUCCEEDED(source->LockRect(0,&r,nullptr,0))); + if(r.pBits) for(unsigned y=0;y<16;y++) for(unsigned x=0;x<16;x++) + reinterpret_cast(static_cast(r.pBits)+r.Pitch*y)[x]=0xff3a6b9c; + CHECK(SUCCEEDED(source->UnlockRect(0))); CHECK(SUCCEEDED(d->UpdateTexture(source,destination))); + struct Vertex { float x,y,z,rhw; DWORD color; float u,v; }; + const Vertex v[]={{-0.5f,-0.5f,0,1,0xffffffff,0,0},{63.5f,-0.5f,0,1,0xffffffff,1,0}, + {-0.5f,63.5f,0,1,0xffffffff,0,1},{63.5f,63.5f,0,1,0xffffffff,1,1}}; + CHECK(SUCCEEDED(d->SetTexture(0,destination))); + CHECK(SUCCEEDED(d->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1))); + CHECK(SUCCEEDED(d->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1))); + CHECK(SUCCEEDED(d->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE))); + CHECK(SUCCEEDED(d->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1))); + CHECK(SUCCEEDED(d->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE))); + CHECK(SUCCEEDED(d->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_POINT))); + CHECK(SUCCEEDED(d->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_POINT))); + CHECK(SUCCEEDED(d->SetRenderState(D3DRS_ZENABLE,FALSE))); + CHECK(SUCCEEDED(d->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE))); + CHECK(SUCCEEDED(d->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE))); + CHECK(SUCCEEDED(d->Clear(0,nullptr,D3DCLEAR_TARGET,0xff000000,1.0f,0))); + CHECK(SUCCEEDED(d->BeginScene())); CHECK(SUCCEEDED(d->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,v,sizeof(Vertex)))); + CHECK(SUCCEEDED(d->EndScene())); CHECK(SUCCEEDED(d->SetTexture(0,nullptr))); + } + IDirect3DSurface9 *target=nullptr,*copy=nullptr; + CHECK(SUCCEEDED(d->GetRenderTarget(0,&target))); + CHECK(SUCCEEDED(d->CreateOffscreenPlainSurface(64,64,D3DFMT_X8R8G8B8,D3DPOOL_SYSTEMMEM,©,nullptr))); + if(target && copy) { + CHECK(SUCCEEDED(d->GetRenderTargetData(target,copy))); + D3DLOCKED_RECT r{}; CHECK(SUCCEEDED(copy->LockRect(&r,nullptr,D3DLOCK_READONLY))); + if(r.pBits) CHECK((reinterpret_cast(static_cast(r.pBits)+r.Pitch*32)[32]&0xffffff)==0x3a6b9c); + CHECK(SUCCEEDED(copy->UnlockRect())); + } + if(copy) copy->Release(); if(target) target->Release(); + if(destination) destination->Release(); if(source) source->Release(); +} +static UINT64 TestNowNs() { + LARGE_INTEGER counter{},frequency{}; + QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&counter); + const UINT64 t=UINT64(counter.QuadPart), f=UINT64(frequency.QuadPart); + return (t/f)*1000000000ull+(t%f)*1000000000ull/f; +} +static void TestPresentLatency(HMODULE module) { + using CreateEx = HRESULT (WINAPI*)(UINT,IDirect3D9Ex**); + auto createEx=reinterpret_cast(GetProcAddress(module,"Direct3DCreate9Ex")); + CHECK(createEx!=nullptr); if(!createEx) return; + IDirect3D9Ex* factory=nullptr; CHECK(SUCCEEDED(createEx(D3D_SDK_VERSION,&factory)) && factory); + if(!factory) return; + HWND window=CreateWindowA("STATIC","Present latency fixture",WS_POPUP,0,0,256,256,nullptr,nullptr,GetModuleHandle(nullptr),nullptr); + CHECK(window!=nullptr); + ShowWindow(window,SW_SHOWNOACTIVATE); UpdateWindow(window); + D3DPRESENT_PARAMETERS pp{}; pp.Windowed=TRUE; pp.SwapEffect=D3DSWAPEFFECT_DISCARD; + pp.hDeviceWindow=window; pp.BackBufferWidth=256; pp.BackBufferHeight=256; + pp.BackBufferFormat=D3DFMT_X8R8G8B8; pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9Ex* device=nullptr; + auto hr=factory->CreateDeviceEx(0,D3DDEVTYPE_HAL,window,D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED, + &pp,nullptr,&device); + CHECK(SUCCEEDED(hr) && device); + if(device) { + CHECK(SUCCEEDED(device->SetMaximumFrameLatency(1))); + ID3D9BridgeTelemetry* t=nullptr; + CHECK(device->QueryInterface(IID_D3D9BridgeTelemetry,reinterpret_cast(&t))==S_OK && t); + if(t) { + auto c=Config(1,65536); CHECK(t->StartCapture(&c)==S_OK); + // No frame has been queued, so the existing latency fence is already + // satisfied. The no-op SetMaximumFrameLatency wait must not be logged. + CHECK(SUCCEEDED(device->SetMaximumFrameLatency(1))); + D3D9BridgeTelemetryEvent initial[128]{}; + auto initialRead=Read(t,1,initial,128); + for(UINT i=0;i intervals; + unsigned successfulPresents=0; + for(unsigned i=0;i<80;i++) { + // Keep the real GPU/presenter queue occupied before each Present. + for(unsigned j=0;j<16;j++) CHECK(SUCCEEDED(device->Clear(0,nullptr,D3DCLEAR_TARGET,0xff000000u|(i<<8)|j,1.0f,0))); + const auto begin=TestNowNs(); hr=device->PresentEx(nullptr,nullptr,nullptr,nullptr,0); const auto end=TestNowNs(); + successfulPresents+=hr==S_OK; CHECK(hr==S_OK); intervals.push_back({begin,end}); + } + CHECK(t->StopCapture(1)==S_OK); + std::vector events(65536); + auto read=Read(t,1,events.data(),UINT(events.size())); + unsigned frameWaits=0,withinPresent=0,acquires=0,acquiresWithinPresent=0; + UINT64 maxWaitNs=0,maxPresentNs=0; + for(const auto& interval:intervals) maxPresentNs=std::max(maxPresentNs,interval.end-interval.begin); + constexpr UINT frameLatencyKind=9; // Private contract expected by this regression. + constexpr UINT presentAcquireKind=10; + for(UINT i=0;i=event.BeginNs); + for(const auto& interval:intervals) if(event.BeginNs>=interval.begin && event.EndNs<=interval.end) { + if(frame) ++withinPresent; else ++acquiresWithinPresent; + break; + } + } + } + std::printf("present_latency successful_presents=%u frame_waits=%u within_present=%u acquires=%u acquires_within_present=%u max_frame_wait_ns=%llu max_present_ns=%llu dropped=%llu\n", + successfulPresents,frameWaits,withinPresent,acquires,acquiresWithinPresent,maxWaitNs,maxPresentNs,read.Dropped); + CHECK(frameWaits>0 && withinPresent==frameWaits && read.Dropped==0); + CHECK(acquires>=intervals.size() && acquiresWithinPresent==acquires); + t->Release(); + } + device->Release(); + } + factory->Release(); if(window) DestroyWindow(window); +} +static void Test(IDirect3DDevice9* d, ID3D9BridgeTelemetry* t, IDirect3D9* factory, HWND window, D3DPRESENT_PARAMETERS pp) { + auto c=Config(101); + CHECK(t->StartCapture(nullptr)==E_POINTER); + auto bad=c; bad.StructSize=0; CHECK(FAILED(t->StartCapture(&bad))); + bad=c; bad.Version=2; CHECK(FAILED(t->StartCapture(&bad))); + bad=c; bad.CaptureId=0; CHECK(FAILED(t->StartCapture(&bad))); + bad=c; bad.MaxEvents=0; CHECK(FAILED(t->StartCapture(&bad))); + bad=c; bad.MaxEvents=D3D9_BRIDGE_TELEMETRY_MAX_EVENTS+1; CHECK(FAILED(t->StartCapture(&bad))); + CHECK(t->StartCapture(&c)==S_OK); CHECK(FAILED(t->StartCapture(&c))); + CHECK(FAILED(t->StopCapture(102))); + D3D9BridgeTelemetryRead invalid{}; invalid.StructSize=sizeof(invalid); invalid.Version=1; + CHECK(FAILED(t->ReadEvents(102,&invalid,nullptr,0))); + CHECK(FAILED(t->ReadEvents(101,&invalid,nullptr,1))); + auto before=Snapshot(t,101,nullptr,0,nullptr,0); + CHECK(before.TotalChunks==0 || (before.Flags&D3D9_BRIDGE_MEMORY_CHUNKS_TRUNCATED)); + Work(d,12); CheckPixels(d); Fence(d); + std::vector chunks(4096); + std::vector masks(65536); + auto base=Snapshot(t,101,chunks.data(),UINT(chunks.size()),masks.data(),UINT(masks.size())); + std::vector baseChunks(chunks.begin(),chunks.begin()+base.CopiedChunks); + std::vector baseMasks(masks.begin(),masks.begin()+base.CopiedMasks); + std::vector held; + for(unsigned i=0;i<8;i++) { + IDirect3DTexture9* texture=nullptr; + CHECK(SUCCEEDED(d->CreateTexture(1024,1024,1,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&texture,nullptr))); + if(texture) held.push_back(texture); + } + Fence(d); + auto snap=Snapshot(t,101,chunks.data(),UINT(chunks.size()),masks.data(),UINT(masks.size())); + CHECK(snap.TimestampNs>0 && snap.TotalChunks>=snap.CopiedChunks && snap.TotalMasks>=snap.CopiedMasks); + CHECK(snap.CopiedChunks>0 && snap.TotalCapacity>=snap.TotalUsed); + for(UINT i=0;ibase.TotalUsed); + bool sameCookieChanged=false; + for(UINT i=0;iRelease(); + Fence(d); + auto released=Snapshot(t,101,chunks.data(),UINT(chunks.size()),masks.data(),UINT(masks.size())); + CHECK(released.TotalUsedCreateTexture(4608,4096,1,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&large,nullptr))); + if(large) large->Release(); Fence(d); + CHECK(t->StopCapture(101)==S_OK); CHECK(t->StopCapture(101)==S_FALSE); + CHECK(FAILED(t->StartCapture(&c))); + D3D9BridgeMemorySnapshot stopped{}; stopped.StructSize=sizeof(stopped); stopped.Version=1; + CHECK(FAILED(t->GetMemorySnapshot(101,&stopped,nullptr,0,nullptr,0))); + std::vector events(16384); + auto r=Read(t,101,events.data(),UINT(events.size())); + CHECK(r.Written>0 && !r.Active && r.Filtered==0 && r.Dropped==0); + unsigned kinds[9]={}; unsigned resourceAlloc=0,resourceFree=0,deviceAlloc=0,deviceFree=0; + bool fileMap=false,fileUnmap=false,osMap=false,osUnmap=false,matchedOs=false,matchedCache=false; + for(UINT i=0;i=e.BeginNs && e.ThreadId); + if(e.Kind<9) kinds[e.Kind]++; + if(e.Flags&D3D9_BRIDGE_TELEMETRY_RESOURCE) { + resourceAlloc+=e.Kind==D3D9_BRIDGE_TELEMETRY_ALLOC; + resourceFree+=e.Kind==D3D9_BRIDGE_TELEMETRY_FREE; + if(e.Flags&D3D9_BRIDGE_TELEMETRY_CACHE_RETURN) + for(UINT j=0;j0); + CHECK(kinds[D3D9_BRIDGE_TELEMETRY_FREE]>0); + CHECK(kinds[D3D9_BRIDGE_TELEMETRY_CS_WAIT]>0); + CHECK(fileMap && fileUnmap); + CHECK(osMap && osUnmap && matchedOs); + CHECK(resourceAlloc && resourceFree && deviceAlloc && deviceFree); + // D3D9 never passes a local allocation cache; the sole frontend using it + // is D3D11, which is disabled in this backend. Report it rather than claim + // coverage of a path this fixture cannot reach. + std::printf("events=%u dropped=%llu filtered=%llu alloc=%u free=%u map=%u unmap=%u cs=%u resource=%u gpu=%u chunks=%u masks=%u fileMap=%u fileUnmap=%u\n", + r.Written,r.Dropped,r.Filtered,kinds[1],kinds[2],kinds[3],kinds[4],kinds[5],kinds[6],kinds[7], + snap.CopiedChunks,snap.CopiedMasks,unsigned(fileMap),unsigned(fileUnmap)); + std::printf("resource_alloc=%u resource_free=%u device_alloc=%u device_free=%u os_map=%u os_unmap=%u matched_os=%u matched_cache=%u snapshot_used_before=%llu held=%llu released=%llu\n", + resourceAlloc,resourceFree,deviceAlloc,deviceFree,unsigned(osMap),unsigned(osUnmap),unsigned(matchedOs),unsigned(matchedCache), + base.TotalUsed,snap.TotalUsed,released.TotalUsed); + CHECK(Read(t,101).Remaining==0); + c=Config(102,2); CHECK(t->StartCapture(&c)==S_OK); + CHECK(FAILED(t->ReadEvents(101,&invalid,nullptr,0))); Work(d,4); + r=Read(t,102); CHECK(r.Remaining==2 && r.Dropped>0); + CHECK(t->StopCapture(102)==S_OK); + c=Config(103); c.MinBytes=~UINT64(0); c.MinDurationNs=~UINT64(0); + CHECK(t->StartCapture(&c)==S_OK); Work(d,2); CHECK(t->StopCapture(103)==S_OK); + r=Read(t,103); CHECK(r.Filtered>0); + // Actual resource calls overlap Stop/Start; a completion from a prior + // generation must never acquire the next generation's identity. + std::atomic run{true}; + std::thread worker([&] { while(run.load()) Work(d,1); }); + for(UINT64 id=200;id<220;id++) { + c=Config(id,4096); CHECK(t->StartCapture(&c)==S_OK); Sleep(2); CHECK(t->StopCapture(id)==S_OK); + auto copy=Read(t,id,events.data(),4096); + for(UINT i=0;iStartCapture(&c)==S_OK); + CHECK(SUCCEEDED(d->Reset(&pp))); Work(d,1); + CHECK(t->StopCapture(400)==S_OK); + IDirect3DDevice9* secondary=nullptr; + CHECK(SUCCEEDED(factory->CreateDevice(0,D3DDEVTYPE_HAL,window,D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&pp,&secondary))); + ID3D9BridgeTelemetry* other=nullptr; + if(secondary) CHECK(secondary->QueryInterface(IID_D3D9BridgeTelemetry,reinterpret_cast(&other))==S_OK && other); + if(other) { + c=Config(500); CHECK(t->StartCapture(&c)==S_OK); CHECK(other->StartCapture(&c)==S_OK); + Work(d,2); Work(secondary,2); CHECK(t->StopCapture(500)==S_OK); CHECK(other->StopCapture(500)==S_OK); + auto primary=Read(t,500,events.data(),4096); std::vector first(events.begin(),events.begin()+primary.Written); + auto second=Read(other,500,events.data(),4096); + CHECK(primary.Written && second.Written); + for(UINT i=0;iRelease(); + } + if(secondary) secondary->Release(); +} + +int main(int argc,char** argv) { + std::setvbuf(stdout,nullptr,_IONBF,0); + if(argc<2 || argc>4) return 2; + auto module=LoadLibraryA(argv[1]); + if(!module) { std::printf("FAIL LoadLibrary %lu\n",GetLastError()); return 2; } + auto create=reinterpret_cast(GetProcAddress(module,"Direct3DCreate9")); + auto factory=create?create(D3D_SDK_VERSION):nullptr; if(!factory) return 2; + auto window=CreateWindowA("STATIC","Telemetry fixture",WS_POPUP,0,0,64,64,nullptr,nullptr,GetModuleHandle(nullptr),nullptr); + D3DPRESENT_PARAMETERS pp{}; pp.Windowed=TRUE; pp.SwapEffect=D3DSWAPEFFECT_DISCARD; + pp.hDeviceWindow=window; pp.BackBufferWidth=64; pp.BackBufferHeight=64; + pp.BackBufferFormat=D3DFMT_X8R8G8B8; pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9* device=nullptr; + auto hr=factory->CreateDevice(0,D3DDEVTYPE_HAL,window,D3DCREATE_HARDWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&pp,&device); + if(FAILED(hr)) { std::printf("FAIL CreateDevice 0x%08lx\n",(unsigned long)hr); return 2; } + ID3D9BridgeTelemetry* telemetry=nullptr; + hr=device->QueryInterface(IID_D3D9BridgeTelemetry,reinterpret_cast(&telemetry)); + std::printf("telemetry QI hr=0x%08lx\n",(unsigned long)hr); + if(argc>=3 && std::strcmp(argv[2],"--benchmark")==0) { + if(argc!=4 || (std::strcmp(argv[3],"on") && std::strcmp(argv[3],"off") && std::strcmp(argv[3],"aa"))) return 2; + bool active=std::strcmp(argv[3],"on")==0; + if(active && !telemetry) { std::puts("benchmark unavailable=telemetry-interface"); return 3; } + ShowWindow(window,SW_SHOWNOACTIVATE); UpdateWindow(window); + for(unsigned i=0;i<4;i++) { Work(device,1,false); CHECK(device->Present(nullptr,nullptr,nullptr,nullptr)==S_OK); } + Fence(device); + if(active) { auto c=Config(9001,65536); CHECK(telemetry->StartCapture(&c)==S_OK); } + LARGE_INTEGER f{},a{},b{},fenceEnd{}; ULONGLONG ca=0,cb=0; + QueryPerformanceFrequency(&f); QueryThreadCycleTime(GetCurrentThread(),&ca); QueryPerformanceCounter(&a); + constexpr unsigned iterations=300; + unsigned successfulPresents=0; + for(unsigned i=0;iPresent(nullptr,nullptr,nullptr,nullptr); + successfulPresents+=result==S_OK; CHECK(result==S_OK); + } + QueryPerformanceCounter(&b); QueryThreadCycleTime(GetCurrentThread(),&cb); + Fence(device); QueryPerformanceCounter(&fenceEnd); + D3D9BridgeTelemetryRead r{}; + if(active) { CHECK(telemetry->StopCapture(9001)==S_OK); r=Read(telemetry,9001); } + std::printf("{\"variant\":\"%s\",\"supported\":%u,\"iterations\":%u,\"successful_presents\":%u,\"wall_ns_per_iteration\":%.3f,\"thread_cycles_per_iteration\":%.3f,\"separate_fence_ns\":%.3f,\"events\":%u,\"filtered\":%llu,\"dropped\":%llu,\"inflight\":%llu,\"failures\":%u}\n", + argv[3],unsigned(telemetry!=nullptr),iterations,successfulPresents,double(b.QuadPart-a.QuadPart)*1e9/double(f.QuadPart)/iterations, + double(cb-ca)/iterations,double(fenceEnd.QuadPart-b.QuadPart)*1e9/double(f.QuadPart), + r.Remaining,r.Filtered,r.Dropped,r.InFlightAtStop,failures.load()); + } else if(argc==3 && std::strcmp(argv[2],"--present")==0) { + CHECK(hr==S_OK && telemetry); + if(telemetry) TestPresentLatency(module); + } else { + CHECK(hr==S_OK && telemetry); + if(telemetry) { TestRecorderGenerations(); Test(device,telemetry,factory,window,pp); TestPresentLatency(module); } + } + if(telemetry) telemetry->Release(); device->Release(); factory->Release(); DestroyWindow(window); FreeLibrary(module); + std::printf("checks=%u failures=%u\n",checks.load(),failures.load()); return failures.load()?1:0; +} diff --git a/tests/measurement-telemetry-test.ps1 b/tests/measurement-telemetry-test.ps1 new file mode 100644 index 0000000..d9a1f6b --- /dev/null +++ b/tests/measurement-telemetry-test.ps1 @@ -0,0 +1,40 @@ +param([string]$Backend, [string]$OutputDirectory, [string]$LlvmMingwBin, + [switch]$CompileOnly, [switch]$Benchmark, [switch]$DisableCompat, [switch]$PresentOnly, [ValidateSet('off','on','aa')][string]$Collection='off') +$ErrorActionPreference='Stop' +$root=Split-Path -Parent $PSScriptRoot +if (-not $Backend) { $Backend=Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll' } +if (-not $OutputDirectory) { $OutputDirectory=Join-Path $root 'out/perf/20261112-measurement-coverage/backend' } +New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null +$OutputDirectory=(Resolve-Path -LiteralPath $OutputDirectory).Path +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$toolchain=Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin +$exe=Join-Path $OutputDirectory 'measurement-telemetry-probe.exe' +& $toolchain.LlvmMingw.CompilerPath '-std=c++17' '-O2' '-static' (Join-Path $PSScriptRoot 'measurement-telemetry-probe.cpp') (Join-Path $root 'backend/dxvk/src/dxvk/dxvk_telemetry.cpp') '-I' (Join-Path $root 'backend/dxvk/include/vulkan/include') '-o' $exe '-luuid' '-luser32' '-lgdi32' +if ($LASTEXITCODE -ne 0) { throw "Probe compile failed: $LASTEXITCODE" } +if ($CompileOnly) { exit 0 } +$backendPath=(Resolve-Path -LiteralPath $Backend).Path +$enabled=if ($DisableCompat) { 'False' } else { 'True' } +$env:DXVK_CONFIG="d3d9.gtaSaCompat = $enabled; d3d9.gtaSaCompatDiagnostics = False; dxvk.hud = 0" +if (-not $Benchmark) { $env:DXVK_CONFIG+='; d3d9.maxFrameRate = 30; d3d9.maxFrameLatency = 1' } +$env:DXVK_HUD='0' +$env:DXVK_LOG_PATH=$OutputDirectory +$tag=if ($Benchmark) { "benchmark-$Collection" } elseif ($DisableCompat) { 'no-compat' } else { 'compat' } +$stdout=Join-Path $OutputDirectory "probe-$tag-$PID.stdout.log" +$stderr=Join-Path $OutputDirectory "probe-$tag-$PID.stderr.log" +$arguments=@("`"$backendPath`"") +if ($Benchmark) { $arguments+=@('--benchmark',$Collection) } elseif ($PresentOnly) { $arguments+=@('--present') } +$process=Start-Process -FilePath $exe -ArgumentList $arguments -WorkingDirectory $OutputDirectory ` + -WindowStyle Hidden -PassThru -RedirectStandardOutput $stdout -RedirectStandardError $stderr +try { + if (-not $process.WaitForExit(60000)) { + $process.Kill(); $process.WaitForExit() + Write-Output "FAIL probe watchdog exceeded 60000 ms; stdout=$stdout stderr=$stderr" + $result=124 + } else { $result=$process.ExitCode } +} finally { + if (Test-Path -LiteralPath $stdout) { Get-Content -LiteralPath $stdout } + if (Test-Path -LiteralPath $stderr) { Get-Content -LiteralPath $stderr } + $process.Dispose() +} +exit $result diff --git a/tests/retirement-cs-probe.cpp b/tests/retirement-cs-probe.cpp new file mode 100644 index 0000000..1c25989 --- /dev/null +++ b/tests/retirement-cs-probe.cpp @@ -0,0 +1,66 @@ +// Combined control: a real first bulk release races the CS readback consumed by +// LockRect. Every OS retirement and the GPU drain remain inside totalNs. +#define main UploadMappingLegacyMain +#include "upload-mapping-probe.cpp" +#undef main +#include + +static std::atomic completionFile{nullptr}; +static HANDLE completionEvent; +static bool captureFile; +static HANDLE capturedFile; +static decltype(&CloseHandle) realClose; +static LPVOID WINAPI ObserveMap(HANDLE f,DWORD a,DWORD h,DWORD l,SIZE_T size) { + auto p=originalMap(f,a,h,l,size); + if(GetCurrentThreadId()==ownerThread && captureFile && p)capturedFile=f; + return p; +} +static BOOL WINAPI ObserveClose(HANDLE f) { + auto result=realClose(f); + if(result && f==completionFile.load())SetEvent(completionEvent); + return result; +} +static unsigned long long Now() {LARGE_INTEGER q{};QueryPerformanceCounter(&q);return q.QuadPart;} +static unsigned long long frequency; +static unsigned long long Ns(unsigned long long ticks) {return ticks/frequency*1000000000ull+ticks%frequency*1000000000ull/frequency;} + +static void Run(IDirect3DDevice9* d) { + IDirect3DSurface9 *rt=nullptr,*copy=nullptr;IDirect3DTexture9 *marker=nullptr,*gpu=nullptr; + CHECK(d->GetRenderTarget(0,&rt)==S_OK); + CHECK(d->CreateOffscreenPlainSurface(32,32,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,©,nullptr)==S_OK); + // Prime the readback conversion before installing the final pool-file marker. + CHECK(d->GetRenderTargetData(rt,copy)==S_OK);Flush(d); + captureFile=true;capturedFile=nullptr; + CHECK(d->CreateTexture(2,2,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&marker,nullptr)==S_OK); + captureFile=false;CHECK(capturedFile);completionFile=capturedFile;ResetEvent(completionEvent); + D3DLOCKED_RECT mark{};CHECK(marker->LockRect(0,&mark,nullptr,0)==S_OK);if(mark.pBits)*static_cast(mark.pBits)=0xff123456;CHECK(marker->UnlockRect(0)==S_OK); + std::vector sources; + BenchmarkThreadAffinity affinity;CHECK(affinity.valid());ULONG64 c0=0,c1=0;CHECK(QueryThreadCycleTime(GetCurrentThread(),&c0));auto start=Now(); + for(unsigned i=0;i<96;++i){IDirect3DTexture9* p=nullptr;CHECK(d->CreateTexture(512,512,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&p,nullptr)==S_OK);if(p){Fill(p,3);sources.push_back(p);}} + CHECK(sources.size()==96);CHECK(d->CreateTexture(512,512,1,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&gpu,nullptr)==S_OK); + if(gpu && !sources.empty()){CHECK(d->UpdateTexture(sources.back(),gpu)==S_OK);Pixels(d,gpu,3);} + CHECK(d->Clear(0,nullptr,D3DCLEAR_TARGET,0xff987654,1,0)==S_OK); + CHECK(d->GetRenderTargetData(rt,copy)==S_OK); + CHECK(d->Clear(0,nullptr,D3DCLEAR_TARGET,0xff112233,1,0)==S_OK); + auto release=Now();for(auto p:sources)p->Release();sources.clear();if(gpu)gpu->Release();auto released=Now(); + D3DLOCKED_RECT locked{};CHECK(copy->LockRect(&locked,nullptr,D3DLOCK_READONLY)==S_OK);auto lockedAt=Now(); + bool pixels=locked.pBits!=nullptr; + if(locked.pBits){for(unsigned y=0;y<32;++y)for(unsigned x=0;x<32;++x)pixels&=reinterpret_cast(static_cast(locked.pBits)+y*locked.Pitch)[x]==0xff987654;CHECK(copy->UnlockRect()==S_OK);} + CHECK(pixels);copy->Release();rt->Release();marker->Release(); + CHECK(WaitForSingleObject(completionEvent,10000)==WAIT_OBJECT_0);completionFile=nullptr;Flush(d);auto end=Now();CHECK(QueryThreadCycleTime(GetCurrentThread(),&c1)); + std::printf("{\"type\":\"retirement-cs-benchmark\",\"valid\":%s,\"pixelsValid\":%s,\"textures\":96,\"totalNs\":%llu,\"mainThreadCycles\":%llu,\"releaseSubmitNs\":%llu,\"lockNs\":%llu,\"releaseAndLockNs\":%llu,\"completeReleaseAndDrainNs\":%llu,\"completion\":\"actual-last-file-close-and-gpu-query\"}\n",failures?"false":"true",pixels?"true":"false",Ns(end-start),c1-c0,Ns(released-release),Ns(lockedAt-released),Ns(lockedAt-release),Ns(end-release)); +} +int main(int argc,char** argv) { + std::setvbuf(stdout,nullptr,_IONBF,0);if(argc!=2)return 2;ownerThread=GetCurrentThreadId();LARGE_INTEGER f{};QueryPerformanceFrequency(&f);frequency=f.QuadPart; + SetEnvironmentVariableA("DXVK_CONFIG","d3d9.textureMemory=100;d3d9.presentInterval=0;d3d9.gtaSaCompat=False;dxvk.hud=");SetEnvironmentVariableA("DXVK_CONFIG_FILE","retirement-cs-no-external.conf");SetEnvironmentVariableA("DXVK_HUD","0");SetEnvironmentVariableA("DXVK_LOG_LEVEL","error"); + completionEvent=CreateEventA(nullptr,TRUE,FALSE,nullptr);CHECK(completionEvent);auto lib=LoadLibraryA(argv[1]);CHECK(lib);if(!lib)return 1; + { + ImportHook mh,ch;CHECK(mh.install(lib,"MapViewOfFile",reinterpret_cast(&ObserveMap)));originalMap=reinterpret_cast(mh.original);CHECK(ch.install(lib,"CloseHandle",reinterpret_cast(&ObserveClose)));realClose=reinterpret_cast(ch.original); + using Factory=IDirect3D9* (WINAPI*)(UINT);auto make=reinterpret_cast(GetProcAddress(lib,"Direct3DCreate9"));CHECK(make);auto factory=make(D3D_SDK_VERSION);CHECK(factory); + HWND w=CreateWindowA("STATIC","retirement-cs",WS_POPUP,0,0,64,64,nullptr,nullptr,GetModuleHandleA(nullptr),nullptr);CHECK(w); + D3DPRESENT_PARAMETERS pp{};pp.BackBufferWidth=pp.BackBufferHeight=32;pp.BackBufferFormat=D3DFMT_A8R8G8B8;pp.BackBufferCount=1;pp.SwapEffect=D3DSWAPEFFECT_DISCARD;pp.Windowed=TRUE;pp.hDeviceWindow=w;pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; + IDirect3DDevice9* d=nullptr;CHECK(factory->CreateDevice(0,D3DDEVTYPE_HAL,w,D3DCREATE_HARDWARE_VERTEXPROCESSING,&pp,&d)==S_OK); + if(d){Run(d);CHECK(d->Reset(&pp)==S_OK);CHECK(d->Release()==0);}factory->Release();DestroyWindow(w); + } + FreeLibrary(lib);CloseHandle(completionEvent);std::printf("checks=%u failures=%u\n",checks,failures);return failures?1:0; +} diff --git a/tests/retirement-cs-test.ps1 b/tests/retirement-cs-test.ps1 new file mode 100644 index 0000000..5e7a274 --- /dev/null +++ b/tests/retirement-cs-test.ps1 @@ -0,0 +1,19 @@ +param([string]$Backend,[string]$OutputDirectory,[string]$LlvmMingwBin,[switch]$BuildOnly) +$ErrorActionPreference='Stop' +$root=Split-Path -Parent $PSScriptRoot +if(!$Backend){$Backend=Join-Path $root 'out/build/dxvk-x86/src/d3d9/d3d9.dll'} +if(!$OutputDirectory){$OutputDirectory=Join-Path $root 'out/tests/retirement-cs'} +New-Item -ItemType Directory -Force -Path $OutputDirectory|Out-Null +$OutputDirectory=(Resolve-Path -LiteralPath $OutputDirectory).Path +. (Join-Path $root 'tools/lib/process-runner.ps1') +. (Join-Path $root 'tools/lib/toolchain-discovery.ps1') +$toolchain=Get-RenderStackToolchain -RepoRoot $root -Component Dxvk -LlvmMingwBin $LlvmMingwBin +$probe=Join-Path $OutputDirectory 'retirement-cs-probe.exe' +& $toolchain.LlvmMingw.CompilerPath '-std=c++17' '-O2' '-static' (Join-Path $PSScriptRoot 'retirement-cs-probe.cpp') '-o' $probe '-luuid' '-luser32' +if($LASTEXITCODE){throw "Retirement/CS probe compilation failed ($LASTEXITCODE)"} +if($BuildOnly){return} +$stdout=Join-Path $OutputDirectory 'stdout.log';$stderr=Join-Path $OutputDirectory 'stderr.log';$env:DXVK_LOG_PATH=$OutputDirectory +$p=Start-Process -FilePath $probe -ArgumentList ('"'+(Resolve-Path -LiteralPath $Backend).Path+'"') -WorkingDirectory $OutputDirectory -WindowStyle Hidden -PassThru -RedirectStandardOutput $stdout -RedirectStandardError $stderr +try{if(!$p.WaitForExit(120000)){$p.Kill();$p.WaitForExit();$result=124}else{$result=$p.ExitCode}} +finally{Get-Content -LiteralPath $stdout;Get-Content -LiteralPath $stderr;$p.Dispose()} +exit $result diff --git a/tests/tail-mapping-benchmark-test.py b/tests/tail-mapping-benchmark-test.py new file mode 100644 index 0000000..9f14fe8 --- /dev/null +++ b/tests/tail-mapping-benchmark-test.py @@ -0,0 +1,162 @@ +"""Reject invalid evidence before a timing comparison can be accepted.""" +import copy +import importlib.util +from pathlib import Path +import unittest +import tempfile + +spec = importlib.util.spec_from_file_location('benchmark', Path(__file__).with_name('tail-mapping-benchmark.py')) +benchmark = importlib.util.module_from_spec(spec) +spec.loader.exec_module(benchmark) + + +def runtime_row(label='A'): + row = {metric: 10 for metric in benchmark.RUNTIME_METRICS} + row.update(type='event-stream-benchmark', valid=True, mode='normal', operations=100, + totalMainNs=30, startCycles=3, stopCycles=5, totalMainThreadCycles=18, + successfulPresents=10, failedPresents=0, dropped=0, unavailableMask=18, + reportComplete=True, reportRequested=True, workersFinished=True, + storageMetadataPresent=True, storageComplete=True, storageErrors=0, storageUnread=0, + snapshotEvents=10, snapshotUnread=0, snapshotReportFinished=True, + suite='ab', case='normal', pair=0, label=label) + return row + + +class EvidenceTests(unittest.TestCase): + def test_full_main_timing_cannot_omit_completion_callback(self): + row = runtime_row('B') + benchmark.validate_row(row, 'runtime', 'normal', candidate=True) + for field in ('totalMainNs', 'totalMainThreadCycles'): + invalid = dict(row, **{field: row[field]-1}) + with self.assertRaisesRegex(ValueError, 'omits or duplicates'): + benchmark.validate_row(invalid, 'runtime', 'normal', candidate=True) + + def test_zero_observations_remain_and_zero_denominator_has_no_percentage(self): + a, b = runtime_row(), runtime_row('B') + a['mainThreadCycles'] = b['mainThreadCycles'] = 0 + result = benchmark.summarize([a, b], 'runtime') + cycles = next(s for s in result['summary'] if s['metric'] == 'mainThreadCycles') + self.assertEqual((cycles['aMedian'], cycles['bMedian'], cycles['zeroAPairs'], cycles['zeroBPairs']), (0, 0, 1, 1)) + self.assertIsNone(cycles['changeMedianPercent']) + + def test_equal_work_is_required(self): + a, b = runtime_row(), runtime_row('B') + b['operations'] += 1 + with self.assertRaisesRegex(ValueError, 'different work counts'): + benchmark.summarize([a, b], 'runtime') + + def test_pairing_rejects_missing_and_duplicate_samples(self): + for rows in ([runtime_row()], [runtime_row(), runtime_row(), runtime_row('B')]): + with self.assertRaisesRegex(ValueError, 'unpaired|duplicate'): + benchmark.summarize(rows, 'runtime') + + def test_pairwise_changes_are_not_ratio_of_pooled_means(self): + a, b = runtime_row(), runtime_row('B') + a['workPresentDrainNs'], b['workPresentDrainNs'] = 100, 50 + a2, b2 = copy.deepcopy(a), copy.deepcopy(b) + a2.update(pair=1, workPresentDrainNs=1000) + b2.update(pair=1, workPresentDrainNs=1000) + summary = benchmark.summarize([a, b, a2, b2], 'runtime')['summary'] + item = next(s for s in summary if s['metric'] == 'workPresentDrainNs') + self.assertEqual(item['changeMedianPercent'], -25) + self.assertEqual(item['fasterPairs'], 1) + + def test_expected_baseline_truncation_is_explicit_but_candidate_must_be_complete(self): + row = runtime_row() + row.update(mode='high', events=65536, dropped=123, reportComplete=True, + lateEvent=False, lateFrameOverlaps=0) + benchmark.validate_row(row, 'runtime', 'high', candidate=False) + with self.assertRaisesRegex(ValueError, 'incomplete'): + benchmark.validate_row(row, 'runtime', 'high', candidate=True) + row.update(events=80000, snapshotEvents=80000, dropped=0, lateEvent=True, lateFrameOverlaps=1) + benchmark.validate_row(row, 'runtime', 'high', candidate=True) + + def test_lossless_count_without_tail_association_is_not_accepted(self): + row = runtime_row() + row.update(mode='high', events=80000, lateEvent=True, lateFrameOverlaps=0) + with self.assertRaisesRegex(ValueError, 'tail and frame'): + benchmark.validate_row(row, 'runtime', 'high', candidate=True) + + def test_invalid_finite_and_failure_values_are_rejected(self): + for value in (float('nan'), float('inf'), -1, True): + row = runtime_row() + row['p99Ns'] = value + with self.assertRaises(ValueError): + benchmark.validate_row(row, 'runtime', 'normal', candidate=True) + for field, value in [('valid', False), ('failedPresents', 1), ('unavailableMask', 1), ('reportComplete', False)]: + row = runtime_row() + row[field] = value + with self.assertRaises(ValueError): + benchmark.validate_row(row, 'runtime', 'normal', candidate=True) + + def test_mapping_requires_pixels_lifetime_and_same_budget(self): + row = {metric: 1 for metric in benchmark.MAPPING_METRICS} + row.update(type='mapping-budget-benchmark', workload='hot', iterations=64, + pixelsValid=True, failedPresents=0, mappedAfterReleaseBytes=0, valid=True, + queryValid=True, affinityScope='main-thread-only-lowest-allowed', + residencyPass='separate-untimed', + suite='ab', case='hot', pair=0, label='A', budgetMiB=100) + benchmark.validate_row(row, 'mapping', 'hot', candidate=True) + for field, value in [('pixelsValid', False), ('queryValid', False), + ('releaseSubmitNs', 2), + ('mappedAfterReleaseBytes', 65536), ('affinityScope', ''), + ('residencyPass', '')]: + invalid = dict(row, **{field: value}) + with self.assertRaises(ValueError): + benchmark.validate_row(invalid, 'mapping', 'hot', candidate=True) + b = dict(row, label='B', budgetMiB=128) + with self.assertRaisesRegex(ValueError, 'different mapping budgets'): + benchmark.summarize([row, b], 'mapping') + + def test_claimed_complete_cannot_replace_missing_or_partial_report(self): + row = runtime_row('B') + row['events'] = 1 + text = ('# measurement captureId=1 status=complete unavailableMask=18\n' + 'telemetry available=1 active=0 captureId=1 recorded=1 backendDropped=0 hostDropped=0 ' + 'invalidCaptureEvents=0 inFlight=0 inFlightAtStop=0 lateDroppedAtRead=0 remaining=0 error=""\n' + 'telemetryEvent index=0 captureId=1\n' + 'telemetryStorage complete=1 received=1 accepted=1 written=1 reported=1 omitted=0 unread=0 errors=0x0\n' + 'contextSummary available=0 reason=disabled\n' + 'reportEnd captureId=1 completed=1\n') + with tempfile.TemporaryDirectory() as directory: + path = Path(directory)/'report.log' + path.write_text(text, encoding='utf-8') + self.assertEqual(benchmark.validate_runtime_report(path, row, True)['verifiedReportEvents'], 1) + invalid_reports = ['', text.replace('status=complete', 'status=pending'), + text.replace('telemetryEvent index=0 captureId=1\n', ''), + text.replace('errors=0x0', 'errors=0x4'), + text.replace('reportEnd captureId=1 completed=1\n', ''), + text.replace('reportEnd captureId=1', 'reportEnd captureId=2'), + text[:text.index('telemetryStorage ')]] + for report in invalid_reports: + path.write_text(report, encoding='utf-8') + with self.assertRaises(ValueError): + benchmark.validate_runtime_report(path, row, True) + + def test_legacy_full_capture_context_tail_and_expected_missing_journal(self): + row = runtime_row('A') + row['events'] = 1 + text = ('# measurement captureId=2 status=complete-partial unavailableMask=16\n' + 'telemetry available=1 active=0 captureId=2 recorded=1 backendDropped=0 hostDropped=0 ' + 'invalidCaptureEvents=0 inFlight=0 inFlightAtStop=0 lateDroppedAtRead=0 remaining=0 error=""\n' + 'telemetryEvent index=0 captureId=2\n' + 'contextSummary available=1\n' + 'contextCounts excludedPolls=1 candidatePolls=0\n' + 'contextRange kind=excluded-steady-state polls=1\n') + with tempfile.TemporaryDirectory() as directory: + path = Path(directory)/'legacy.log' + path.write_text(text, encoding='utf-8') + result = benchmark.validate_runtime_report(path, row, False, allowed_unavailable=16) + self.assertEqual(result['verifiedReportStatus'], 'complete-partial') + with self.assertRaisesRegex(ValueError, 'finalization record'): + benchmark.validate_runtime_report(path, row, True, allowed_unavailable=16) + for bad in (text.replace('unavailableMask=16', 'unavailableMask=18'), + text.replace('contextCounts excludedPolls=1 candidatePolls=0\n', ''), + text.replace('status=complete-partial', 'status=pending')): + path.write_text(bad, encoding='utf-8') + with self.assertRaises(ValueError): + benchmark.validate_runtime_report(path, row, False, allowed_unavailable=16) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/tail-mapping-benchmark.py b/tests/tail-mapping-benchmark.py new file mode 100644 index 0000000..a7b0f38 --- /dev/null +++ b/tests/tail-mapping-benchmark.py @@ -0,0 +1,311 @@ +"""Repeated fixed-binary version A/B and A/A for F8 storage and mapping policy. + +Each sample is a fresh process, run sequentially. Timings are fixture results; +the runner does not estimate game FPS from these workloads. +""" +from __future__ import annotations + +import argparse +from datetime import datetime, timezone +import hashlib +import json +import math +import os +from pathlib import Path +import re +import statistics +import subprocess + + +def digest(path: Path) -> str: + with path.open('rb') as stream: + return hashlib.file_digest(stream, 'sha256').hexdigest() + + +def write_json(path: Path, value) -> None: + path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + '\n', encoding='utf-8') + + +def finite_number(row, key, positive=False): + value = row[key] + if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value): + raise ValueError(f'invalid {key}: {value!r}') + if value < 0 or (positive and value == 0): + raise ValueError(f'invalid {key}: {value!r}') + return value + + +RUNTIME_METRICS = ('workPresentDrainNs', 'mainThreadCycles', 'p95Ns', 'p99Ns', 'maxNs', + 'maxPresentNs', 'startPresentNs', 'completionInterruptNs', + 'totalMainNs', 'totalMainThreadCycles', 'reportWaitNs', 'events', 'dropped') +MAPPING_METRICS = ('totalNs', 'mainThreadCycles', 'p95Ns', 'p99Ns', 'maxNs', + 'presentNs', 'drainNs', 'releaseNs', 'releaseSubmitNs', 'mapCalls', 'unmapCalls', 'mapNs', 'unmapNs', + 'maxUnmapBurst', 'maxUnmapBurstNs', 'operationMaxUnmapBurst', + 'operationMaxUnmapBurstNs', 'cleanupUnmapCalls', 'cleanupUnmapNs', + 'mappedPeakBytes', 'mappedEndBytes') + + +def validate_row(row, kind, case, candidate): + expected = 'event-stream-benchmark' if kind == 'runtime' else 'mapping-budget-benchmark' + if row.get('type') != expected or row.get('valid') is not True: + raise ValueError('fixture did not complete a valid real workload') + if row.get('mode' if kind == 'runtime' else 'workload') != case: + raise ValueError('fixture workload differs from requested case') + for metric in RUNTIME_METRICS if kind == 'runtime' else MAPPING_METRICS: + finite_number(row, metric) + if kind == 'runtime': + finite_number(row, 'operations', positive=True) + finite_number(row, 'workPresentDrainNs', positive=True) + for metric in ('startCycles', 'stopCycles'): + finite_number(row, metric) + if row['totalMainNs'] != row['startPresentNs'] + row['workPresentDrainNs'] + row['completionInterruptNs']: + raise ValueError('main timing total omits or duplicates a measured callback') + if row['totalMainThreadCycles'] != row['startCycles'] + row['mainThreadCycles'] + row['stopCycles']: + raise ValueError('main cycle total omits or duplicates a measured callback') + finite_number(row, 'successfulPresents', positive=True) + if row['failedPresents'] != 0: + raise ValueError('failed Present in measurement') + if row.get('workersFinished') is not True: + raise ValueError('capture workers have not finished') + if case == 'inactive': + if row['events'] != 0 or row['dropped'] != 0 or row.get('reportRequested') is not False: + raise ValueError('inactive F8 unexpectedly collected events') + elif candidate or case != 'high': + # This fixture isolates storage: CPU sampling and journal attribution + # are intentionally disabled (bits 2 and 16). Other failures remain fatal. + if row['dropped'] != 0 or row['unavailableMask'] & ~18 or row['reportComplete'] is not True or row.get('reportRequested') is not True: + raise ValueError('incomplete active F8 capture') + if row['events'] <= 0: + raise ValueError('active F8 report contains no events') + if case == 'high' and (row['events'] <= 65536 or not row['lateEvent'] or row['lateFrameOverlaps'] <= 0): + raise ValueError('candidate did not retain high-volume tail and frame overlap') + if candidate and (row.get('storageMetadataPresent') is not True or row.get('storageComplete') is not True or + row.get('storageErrors') != 0 or row.get('storageUnread') != 0 or + row.get('snapshotEvents') != row['events'] or row.get('snapshotUnread') != 0 or + row.get('snapshotReportFinished') is not True): + raise ValueError('candidate final snapshot or storage is incomplete') + else: + finite_number(row, 'iterations', positive=True) + finite_number(row, 'budgetMiB', positive=True) + finite_number(row, 'totalNs', positive=True) + if row['pixelsValid'] is not True or row.get('queryValid') is not True or row['failedPresents'] != 0 or row['mappedAfterReleaseBytes'] != 0: + raise ValueError('mapping pixel, Present, or lifetime evidence failed') + if row.get('affinityScope') != 'main-thread-only-lowest-allowed' or row.get('residencyPass') != 'separate-untimed': + raise ValueError('mapping measurement scope is missing or unexpected') + if row['releaseSubmitNs'] > row['releaseNs']: + raise ValueError('release completion cannot precede the submitting thread') + + +def validate_runtime_report(path, row, candidate, allowed_unavailable=18): + """Check the report itself, not just a fixture's completion booleans.""" + header = telemetry = storage = None + last_line = '' + context_summary = context_counts = False + records = 0 + def fields(line): + return dict(re.findall(r'(\w+)=("[^"]*"|\S+)', line)) + with path.open(encoding='utf-8-sig') as stream: + for line in stream: + if line.strip(): + last_line = line.strip() + context_summary |= line.startswith('contextSummary ') + context_counts |= line.startswith('contextCounts ') + if line.startswith('# measurement '): + if header is not None: + raise ValueError('duplicate capture header') + header = fields(line) + elif line.startswith('telemetry available='): + if telemetry is not None: + raise ValueError('duplicate telemetry header') + telemetry = fields(line) + elif line.startswith('telemetryStorage '): + if storage is not None: + raise ValueError('duplicate storage footer') + storage = fields(line) + elif line.startswith('telemetryEvent '): + records += 1 + if header is None or header.get('status') not in ('complete', 'complete-partial', 'interrupted') or telemetry is None: + raise ValueError('missing or unfinished report') + if int(telemetry['available']) != 1 or int(telemetry['active']) != 0: + raise ValueError('report telemetry is unavailable or still active') + if header['captureId'] != telemetry['captureId'] or int(header['captureId']) < 1: + raise ValueError('report capture identity mismatch') + if candidate: + terminal = fields(last_line) + if not last_line.startswith('reportEnd ') or terminal.get('completed') != '1' or terminal.get('captureId') != header['captureId']: + raise ValueError('report finalization record is missing or mismatched') + else: + if not last_line.startswith(('contextSummary ', 'contextCounts ', 'contextRange ', 'error=', 'reportEnd ')): + raise ValueError('legacy report terminal record is missing') + if last_line.startswith(('contextCounts ', 'contextRange ')) and not context_summary: + raise ValueError('legacy context summary is missing') + if last_line.startswith('contextRange ') and not context_counts: + raise ValueError('legacy context counts are missing') + if records != row['events'] or records != int(telemetry['recorded']): + raise ValueError('report event records differ from claimed counts') + expected_complete = candidate or row['mode'] != 'high' + if expected_complete: + if int(header['unavailableMask']) & ~allowed_unavailable or telemetry.get('error') != '""': + raise ValueError('report contains an unavailable/error marker') + for key in ('backendDropped', 'hostDropped', 'invalidCaptureEvents', 'inFlight', + 'inFlightAtStop', 'lateDroppedAtRead', 'remaining'): + if int(telemetry[key]) != 0: + raise ValueError(f'incomplete report: {key}') + if candidate and storage is None: + raise ValueError('candidate report lacks final storage footer') + if storage is not None: + if int(storage['complete']) != 1 or int(storage['errors'], 0) != 0: + raise ValueError('storage footer is incomplete') + if int(storage['omitted']) != 0 or int(storage['unread']) != 0: + raise ValueError('storage events were omitted or not read') + if any(int(storage[key]) != records for key in ('received', 'accepted', 'written', 'reported')): + raise ValueError('lossless storage counters do not conserve records') + return {'reportSha256': digest(path), 'verifiedReportEvents': records, + 'verifiedReportStatus': header['status'], 'verifiedTerminalRecord': last_line, + 'verifiedStorageComplete': int(storage['complete']) == 1 if storage else None} + + +def summarize(rows, kind): + metrics = RUNTIME_METRICS if kind == 'runtime' else MAPPING_METRICS + grouped = {} + for row in rows: + key = (row['suite'], row['case'], row['pair']) + pair = grouped.setdefault(key, {}) + if row['label'] in pair: + raise ValueError(f'duplicate sample: {key}/{row["label"]}') + pair[row['label']] = row + paired, summary = [], [] + for (suite, case, index), pair in sorted(grouped.items()): + if set(pair) != {'A', 'B'}: + raise ValueError(f'unpaired sample: {suite}/{case}/{index}') + a, b = pair['A'], pair['B'] + count = 'operations' if kind == 'runtime' else 'iterations' + if a[count] != b[count]: + raise ValueError('A/B generated different work counts') + if kind == 'mapping' and a['budgetMiB'] != b['budgetMiB']: + raise ValueError('A/B used different mapping budgets') + for metric in metrics: + av, bv = finite_number(a, metric), finite_number(b, metric) + paired.append({'suite': suite, 'case': case, 'pair': index, 'metric': metric, + 'a': av, 'b': bv, 'delta': bv-av, + 'changePercent': 100*(bv/av-1) if av else None}) + keys = sorted({(p['suite'], p['case'], p['metric']) for p in paired}) + for suite, case, metric in keys: + values = [p for p in paired if (p['suite'], p['case'], p['metric']) == (suite, case, metric)] + changes = [p['changePercent'] for p in values if p['changePercent'] is not None] + summary.append({'suite': suite, 'case': case, 'metric': metric, 'pairs': len(values), + 'aMedian': statistics.median(p['a'] for p in values), + 'bMedian': statistics.median(p['b'] for p in values), + 'deltaMedian': statistics.median(p['delta'] for p in values), + 'changeMedianPercent': statistics.median(changes) if len(changes) == len(values) else None, + 'changeMinPercent': min(changes) if changes else None, + 'changeMaxPercent': max(changes) if changes else None, + 'fasterPairs': sum(p['b'] < p['a'] for p in values), + 'zeroAPairs': sum(p['a'] == 0 for p in values), + 'zeroBPairs': sum(p['b'] == 0 for p in values)}) + return {'kind': kind, 'rows': len(rows), 'summary': summary, 'paired': paired, + 'interpretation': 'Sequential fresh-process fixture version comparison; A/A measures noise. ' + 'Totals include final GPU drain. Report writing is reported separately. ' + 'Quantiles are per-process workload samples; no game FPS claim.'} + + +def run(args): + output = args.output.resolve() + output.mkdir(parents=True, exist_ok=False) + paths = {name: getattr(args, name).resolve() for name in ('a_probe', 'b_probe', 'a_backend', 'b_backend')} + binaries = {name: {'path': str(path), 'size': path.stat().st_size, 'sha256': digest(path)} + for name, path in paths.items()} + if args.kind == 'runtime' and binaries['a_backend']['sha256'] != binaries['b_backend']['sha256']: + raise ValueError('runtime storage comparison must use the same backend') + if args.kind == 'mapping' and binaries['a_probe']['sha256'] != binaries['b_probe']['sha256']: + raise ValueError('mapping comparison must use the same fixture executable') + cases = args.cases or (['inactive', 'normal', 'high'] if args.kind == 'runtime' + else ['below', 'hot', 'cold', 'over', 'create', 'unused']) + metadata = {'kind': args.kind, 'binaries': binaries, 'cases': cases, 'pairs': args.pairs, + 'aaPairs': args.aa_pairs, 'budgetMiB': args.budget, 'iterations': args.iterations, + 'startedUtc': datetime.now(timezone.utc).isoformat(), 'status': 'running', + 'ordering': 'alternating AB/BA, rotating cases; interleaved baseline A/A', + 'scheduling': 'sequential processes; runner does not change CPU affinity or priority'} + write_json(output / 'metadata.json', metadata) + rows = [] + try: + for index in range(max(args.pairs, args.aa_pairs)): + suites = ['ab', 'aa'] if index % 2 == 0 else ['aa', 'ab'] + for suite in suites: + if index >= (args.pairs if suite == 'ab' else args.aa_pairs): + continue + order = 'AB' if index % 2 == 0 else 'BA' + for shift in range(len(cases)): + case = cases[(shift+index) % len(cases)] + for label in order: + candidate = suite == 'ab' and label == 'B' + variant = 'b' if candidate else 'a' + name = f'{suite}-{index:02}-{case}-{label}' + working = output / name + working.mkdir() + command = [str(paths[variant+'_probe'])] + if args.kind == 'runtime': + command += ['--event-stream-benchmark', str(paths[variant+'_backend']), case] + else: + command += [str(paths[variant+'_backend']), 'bench', str(args.budget), case, str(args.iterations)] + env = os.environ.copy() + env.update({'DXVK_LOG_PATH': str(working), 'TEMP': str(working), 'TMP': str(working)}) + write_json(working / 'command.json', command) + with (working/'stdout.log').open('w', encoding='utf-8') as stdout, (working/'stderr.log').open('w', encoding='utf-8') as stderr: + completed = subprocess.run(command, cwd=working, env=env, stdout=stdout, stderr=stderr, + timeout=args.timeout, check=False) + if completed.returncode: + raise RuntimeError(f'{name} exited {completed.returncode}; see raw logs') + parsed = [] + for line in (working/'stdout.log').read_text(encoding='utf-8', errors='replace').splitlines(): + if line.startswith('{'): + parsed.append(json.loads(line)) + expected = 'event-stream-benchmark' if args.kind == 'runtime' else 'mapping-budget-benchmark' + selected = [row for row in parsed if row.get('type') == expected] + if len(selected) != 1: + raise ValueError(f'{name}: expected one {expected} row, got {len(selected)}') + row = selected[0] + validate_row(row, args.kind, case, candidate) + if args.kind == 'mapping' and (row['iterations'] != args.iterations or row['budgetMiB'] != args.budget): + raise ValueError('mapping fixture did not use the requested work count and budget') + if args.kind == 'runtime' and case != 'inactive': + report = Path(row['reportPath']) + if not report.is_absolute(): + report = working / report + row['reportPath'] = str(report.resolve()) + row.update(validate_runtime_report(report, row, candidate)) + row.update({'suite': suite, 'case': case, 'pair': index, 'label': label, + 'order': order, 'candidate': candidate, 'directory': str(working)}) + rows.append(row) + with (output/'samples.jsonl').open('a', encoding='utf-8') as stream: + stream.write(json.dumps(row, ensure_ascii=False) + '\n') + print(f'{name}: complete', flush=True) + for name, path in paths.items(): + if digest(path) != binaries[name]['sha256']: + raise RuntimeError(f'binary changed during benchmark: {path}') + write_json(output/'summary.json', summarize(rows, args.kind)) + metadata['status'] = 'passed' + except BaseException as error: + metadata['status'], metadata['error'] = 'failed', str(error) + raise + finally: + metadata['rows'] = len(rows) + metadata['endedUtc'] = datetime.now(timezone.utc).isoformat() + write_json(output/'metadata.json', metadata) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--kind', required=True, choices=('runtime', 'mapping')) + for option in ('a-probe', 'b-probe', 'a-backend', 'b-backend', 'output'): + parser.add_argument('--'+option, required=True, type=Path) + parser.add_argument('--pairs', type=int, default=6) + parser.add_argument('--aa-pairs', type=int, default=3) + parser.add_argument('--cases', nargs='+') + parser.add_argument('--budget', type=int, default=100) + parser.add_argument('--iterations', type=int, default=512) + parser.add_argument('--timeout', type=int, default=300) + parsed = parser.parse_args() + if not (1 <= parsed.pairs <= 30 and 1 <= parsed.aa_pairs <= 30): + parser.error('pair counts must be between 1 and 30') + run(parsed) diff --git a/tools/analyze-cs-waits.py b/tools/analyze-cs-waits.py new file mode 100644 index 0000000..6930c59 --- /dev/null +++ b/tools/analyze-cs-waits.py @@ -0,0 +1,123 @@ +"""Correlate recorded CS waits with CS chunks and driver spans, without adding nested time. + +Intervals are wall-clock observations. Missing/filtered spans stay unobserved; +driver time may include CPU work, scheduling and driver-internal waits. +""" +import argparse +import bisect +import hashlib +import json +from pathlib import Path +import re + +HIGH_PRIORITY = 1 << 11 +DRIVER_KINDS = {14: 'command-allocate', 15: 'command-begin', 16: 'command-reset'} + + +def duration_union(intervals): + end = total = 0 + for lo, hi in sorted(intervals): + total += max(0, hi-max(lo, end)) + end = max(end, hi) + return total + + +class Intervals: + def __init__(self, rows): + self.rows = sorted(rows, key=lambda e: e['beginNs']) + self.starts = [e['beginNs'] for e in self.rows] + self.ends = [] + end = 0 + for e in self.rows: + end = max(end, e['endNs']) + self.ends.append(end) + + def overlap(self, lo, hi): + first = bisect.bisect_right(self.ends, lo) + last = bisect.bisect_left(self.starts, hi) + return (e for e in self.rows[first:last] if e['endNs'] > lo) + + +def analyze(events, minimum_ns=0): + chunks = Intervals(e for e in events if e['kind'] == 13) + drivers = Intervals(e for e in events if e['kind'] in DRIVER_KINDS) + result = [] + for wait in events: + if wait['kind'] != 5 or wait['endNs']-wait['beginNs'] < minimum_ns: + continue + lo, hi = wait['beginNs'], wait['endNs'] + work, high, driver, target = [], [], [], False + driver_kinds = {name: [] for name in DRIVER_KINDS.values()} + sequence_rows = [] + for chunk in chunks.overlap(lo, hi): + if chunk.get('captureId') != wait.get('captureId') or chunk['thread'] == wait['thread']: + continue + same_queue = bool(chunk['flags'] & HIGH_PRIORITY) == bool(wait['flags'] & HIGH_PRIORITY) + if same_queue and chunk['identity'] > wait['identity']: + continue + a, b = max(lo, chunk['beginNs']), min(hi, chunk['endNs']) + work.append((a, b)) + if chunk['flags'] & HIGH_PRIORITY: + high.append((a, b)) + target |= same_queue and chunk['identity'] == wait['identity'] and bool(wait['identity']) + sequence_rows.append(dict(sequence=chunk['identity'], highPriority=bool(chunk['flags'] & HIGH_PRIORITY), thread=chunk['thread'], overlapNs=b-a)) + for call in drivers.overlap(a, b): + if call.get('captureId') != wait.get('captureId') or call['thread'] != chunk['thread']: + continue + span = max(a, call['beginNs']), min(b, call['endNs']) + driver.append(span) + driver_kinds[DRIVER_KINDS[call['kind']]].append(span) + observed = duration_union(work) + result.append(dict(beginNs=lo, endNs=hi, durationNs=hi-lo, thread=wait['thread'], + captureId=wait.get('captureId'), targetSequence=wait['identity'], + highPriority=bool(wait['flags'] & HIGH_PRIORITY), targetObserved=target, + csObservedNs=observed, highPriorityNs=duration_union(high), + driverWithinCsNs=duration_union(driver), + driverKindsNs={k: duration_union(v) for k, v in driver_kinds.items()}, + unobservedNs=hi-lo-observed, chunks=sequence_rows)) + return sorted(result, key=lambda e: e['durationNs'], reverse=True) + + +def read_report(path): + events, frames, metadata = [], [], {} + fields = re.compile(r'(\w+)=([^\s]+)') + with path.open(encoding='utf-8-sig', errors='strict') as stream: + for line in stream: + if line.startswith('telemetryEvent '): + row = dict(fields.findall(line)) + event = {k: int(row[k], 0) for k in ('kind', 'beginNs', 'endNs', 'identity', 'flags', 'captureId')} + event['thread'] = int(row.get('thread', row.get('threadId')), 0) + if event['endNs'] < event['beginNs']: + raise ValueError('reversed telemetry interval') + events.append(event) + elif line.startswith('frame '): + row = dict(fields.findall(line)) + frames.append({k: int(row[k], 0) for k in ('id', 'beginNs', 'endNs')}) + elif line.startswith(('telemetry available=', 'telemetryStorage ', 'reportEnd ')): + metadata[line.split()[0]] = dict(fields.findall(line)) + return events, frames, metadata + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('report', type=Path) + parser.add_argument('--output', required=True, type=Path) + parser.add_argument('--minimum-ms', type=float, default=1) + args = parser.parse_args() + events, frames, metadata = read_report(args.report) + rows = analyze(events, int(args.minimum_ms*1e6)) + frame_index = Intervals(frames) + for row in rows: + row['frames'] = [f['id'] for f in frame_index.overlap(row['beginNs'], row['endNs'])] + with args.report.open('rb') as stream: + sha = hashlib.file_digest(stream, 'sha256').hexdigest() + output = dict(source=str(args.report.resolve()), sha256=sha, + semantics='clipped wall-clock unions; nested spans are not additive; missing spans remain unobserved', + metadata=metadata, events=len(events), waits=rows) + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(output, indent=2)+'\n', encoding='utf-8') + print(json.dumps(dict(events=len(events), waits=len(rows), output=str(args.output)))) + + +if __name__ == '__main__': + main() diff --git a/tools/test.ps1 b/tools/test.ps1 index eac2cef..8cd803d 100644 --- a/tools/test.ps1 +++ b/tools/test.ps1 @@ -560,8 +560,15 @@ function Add-MeasurementGates { if ($AllowNonV18MsBuild) { $nativeTools += '-AllowNonV18MsBuild' } $symbolTools = @('-Python', $pythonPath, '-LlvmMingwBin', $toolchain.LlvmMingw.BinPath) $backendTools = @('-LlvmMingwBin', $toolchain.LlvmMingw.BinPath) + $mappingTools = $backendTools + @('-Ninja', $toolchain.Ninja.Path, '-Glslang', $toolchain.Glslang.Path) + [void](Invoke-Gate -Name 'tail-mapping-benchmark-evidence' -Category 'measurement-offline' ` + -FilePath $pythonPath -Arguments @((Join-Path $root 'tests/tail-mapping-benchmark-test.py')) -Required $true) + [void](Invoke-Gate -Name 'cs-wait-analysis' -Category 'measurement-offline' ` + -FilePath $pythonPath -Arguments @((Join-Path $root 'tests/cs-wait-analysis-test.py')) -Required $true) foreach ($entry in @( @{ Name='measurement-symbols'; Script='measurement-symbols-test.ps1'; Args=$symbolTools }, + @{ Name='measurement-coverage-summary'; Script='measurement-coverage-summary-test.ps1'; Args=@() }, + @{ Name='mapping-lru-exceptions'; Script='mapping-toolchain-forwarding-test.ps1'; Args=(@('-OutputDirectory',(Join-Path $root 'out/perf/20261114-f8-tail-mapping/pipeline-lru')) + $mappingTools) }, @{ Name='measurement-symbols-msvc'; Script='measurement-symbols-msvc-test.ps1'; Args=($nativeTools + $symbolTools) }, @{ Name='journal-attribution-offline'; Script='journal-attribution-test.ps1'; Args=(@('-EvidenceName','pipeline-offline') + $nativeTools) } )) { @@ -570,6 +577,8 @@ function Add-MeasurementGates { } foreach ($entry in @( @{ Name='measurement-backend'; Script='measurement-backend-test.ps1'; Args=(@('-Backend',$Backend) + $backendTools) }, + @{ Name='measurement-telemetry-native'; Script='measurement-telemetry-test.ps1'; Args=(@('-Backend',$Backend,'-OutputDirectory',(Join-Path $root 'out/perf/20261112-measurement-coverage/pipeline-telemetry')) + $backendTools) }, + @{ Name='measurement-telemetry-no-compat'; Script='measurement-telemetry-test.ps1'; Args=(@('-Backend',$Backend,'-DisableCompat','-OutputDirectory',(Join-Path $root 'out/perf/20261112-measurement-coverage/pipeline-telemetry-no-compat')) + $backendTools) }, @{ Name='thread-journal-native'; Script='thread-journal-test.ps1'; Args=(@('-Backend',$Backend,'-OutputDirectory',(Join-Path $root 'out/perf/20261031-thread-journal/pipeline-native')) + $backendTools) }, @{ Name='thread-journal-mixed'; Script='thread-journal-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','check-software','-OutputDirectory',(Join-Path $root 'out/perf/20261031-binding-dirty-capture/pipeline-mixed')) + $backendTools) }, @{ Name='thread-journal-no-compat'; Script='thread-journal-test.ps1'; Args=(@('-Backend',$Backend,'-DisableCompat','-OutputDirectory',(Join-Path $root 'out/perf/20261031-thread-journal/pipeline-no-compat')) + $backendTools) }, @@ -578,12 +587,34 @@ function Add-MeasurementGates { @{ Name='upload-mapping-pressure'; Script='upload-mapping-test.ps1'; Args=(@('-Backend',$Backend) + $backendTools) }, @{ Name='upload-mapping-ordinary'; Script='upload-mapping-test.ps1'; Args=(@('-Backend',$Backend,'-BudgetMiB','100','-OutputDirectory',(Join-Path $root 'out/perf/20261108-upload-state/native-ordinary')) + $backendTools) }, @{ Name='upload-mapping-single'; Script='upload-mapping-test.ps1'; Args=(@('-Backend',$Backend,'-Levels','1','-OutputDirectory',(Join-Path $root 'out/perf/20261108-upload-state/native-single')) + $backendTools) }, + @{ Name='mapping-budget-100'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','regression','-BudgetMiB','100','-OutputDirectory',(Join-Path $root 'out/perf/20261114-f8-tail-mapping/pipeline-budget-100')) + $mappingTools) }, + @{ Name='mapping-budget-1'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','regression','-BudgetMiB','1','-OutputDirectory',(Join-Path $root 'out/perf/20261114-f8-tail-mapping/pipeline-budget-1')) + $mappingTools) }, + @{ Name='mapping-budget-disabled'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','regression','-BudgetMiB','0','-OutputDirectory',(Join-Path $root 'out/perf/20261114-f8-tail-mapping/pipeline-budget-0')) + $mappingTools) }, + @{ Name='mapping-initializer-100'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','initializer','-BudgetMiB','100','-OutputDirectory',(Join-Path $root 'out/perf/20261114-f8-tail-mapping/pipeline-initializer-100')) + $mappingTools) }, + @{ Name='mapping-initializer-1'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','initializer','-BudgetMiB','1','-OutputDirectory',(Join-Path $root 'out/perf/20261114-f8-tail-mapping/pipeline-initializer-1')) + $mappingTools) }, + @{ Name='mapping-initializer-disabled'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','initializer','-BudgetMiB','0','-OutputDirectory',(Join-Path $root 'out/perf/20261114-f8-tail-mapping/pipeline-initializer-0')) + $mappingTools) }, + @{ Name='mapping-fresh-zero'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','fresh-zero','-OutputDirectory',(Join-Path $root 'out/tests/texture-init/fresh-zero')) + $mappingTools) }, + @{ Name='mapping-retirement-lifetime'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','retirement','-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/lifetime')) + $mappingTools) }, + @{ Name='mapping-retirement-fallback'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','retirement-failure','-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/fallback')) + $mappingTools) }, + @{ Name='cs-wait-driver-correlation'; Script='lockrect-pool-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','correlation','-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/cs-wait')) + $backendTools) }, + @{ Name='cs-wait-allocation-correlation'; Script='lockrect-pool-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','correlation-allocate','-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/cs-allocate')) + $backendTools) }, + @{ Name='mapping-retirement-pressure'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','retirement-pressure','-BudgetMiB','10','-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/pressure')) + $mappingTools) }, + @{ Name='mapping-retirement-map-retry'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','retirement-retry','-BudgetMiB','10','-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/map-retry')) + $mappingTools) }, + @{ Name='mapping-retirement-teardown'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','retirement-teardown','-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/teardown')) + $mappingTools) }, + @{ Name='mapping-retirement-capacity'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','retirement-cap','-BudgetMiB','200','-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/capacity')) + $mappingTools) }, + @{ Name='mapping-retirement-cs-overlap'; Script='retirement-cs-test.ps1'; Args=(@('-Backend',$Backend,'-OutputDirectory',(Join-Path $root 'out/tests/mapping-retirement/cs-overlap')) + $backendTools) }, + @{ Name='mapping-unused-admission'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','unused-admission','-OutputDirectory',(Join-Path $root 'out/tests/texture-init/unused-admission')) + $mappingTools) }, + @{ Name='mapping-allocation-retry'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','allocation-failure','-OutputDirectory',(Join-Path $root 'out/tests/texture-init/allocation-retry')) + $mappingTools) }, + @{ Name='mapping-view-retry'; Script='mapping-budget-test.ps1'; Args=(@('-Backend',$Backend,'-Mode','mapping-failure','-BudgetMiB','4','-OutputDirectory',(Join-Path $root 'out/tests/texture-init/view-retry')) + $mappingTools) }, + @{ Name='lockrect-pool-runtime'; Script='lockrect-pool-test.ps1'; Args=(@('-Backend',$Backend,'-OutputDirectory',(Join-Path $root 'out/tests/lockrect-pool')) + $backendTools) }, @{ Name='patch-retry-native'; Script='patch-retry-test.ps1'; Args=(@('-Backend',$Backend) + $backendTools) }, @{ Name='measurement-backend-unavailable'; Script='measurement-backend-test.ps1'; Args=(@('-Backend',$Backend,'-DisableCompat') + $backendTools) }, @{ Name='measurement-hud-render'; Script='measurement-backend-test.ps1'; Args=(@('-Backend',$Backend,'-RenderHud') + $backendTools) }, @{ Name='measurement-hud-full'; Script='measurement-hud-full-test.ps1'; Args=(@('-Backend',$Backend,'-Python',$pythonPath) + $backendTools) }, @{ Name='measurement-runtime-local'; Script='measurement-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-local') + $nativeTools) }, @{ Name='measurement-runtime-native'; Script='measurement-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-native','-Backend',$Backend) + $nativeTools) }, + @{ Name='measurement-telemetry-gap-status'; Script='measurement-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-telemetry-gap','-Backend',$Backend,'-TelemetryGapOnly') + $nativeTools) }, + @{ Name='measurement-runtime-event-cap'; Script='measurement-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-event-cap','-Backend',$Backend,'-EventStreamMode','cap') + $nativeTools) }, @{ Name='journal-attribution-native'; Script='journal-attribution-test.ps1'; Args=(@('-EvidenceName','pipeline-native','-Device','-Backend',$Backend) + $nativeTools) } @{ Name='bridge-lifecycle-measurement'; Script='bridge-lifecycle-regression-test.ps1'; Args=@('-Backend',$Backend,'-Measurement') } @{ Name='statering-system'; Script='statering-runtime-test.ps1'; Args=(@('-EvidenceName','pipeline-system') + $nativeTools) }