From b45c0ad3492269a2d0849598df5be9292a22f43d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 1 Dec 2026 09:08:45 +0000 Subject: [PATCH 1/3] budget: split upload memory by creator The x2w-budget upload column stays the total. Columns beside it attribute that total to the ring, device slots 27 and 127, the slot 70 fallback, slot 148 pages, linear staging, the Mesh86 slot map, and every other upload heap. AI-assisted (Cursor) Co-authored-by: SimSalabimse --- CMakeLists.txt | 8 + docs/startup-runtime.md | 16 +- src/startup_stubs/d3d12_memory_srv.inl | 3 +- src/startup_stubs/d3d12_xs_device.cpp | 23 +- src/startup_stubs/linear_buffer_backing.h | 3 +- src/startup_stubs/list_upload_pages.h | 2 +- src/startup_stubs/mesh86_cache.inl | 29 ++- src/startup_stubs/resource_budget.h | 32 ++- src/startup_stubs/upload_budget.h | 72 +++++++ tests/startup_resource_budget.cpp | 242 +++++++++++++++++++++- tests/startup_triage_run_check.py | 30 +++ tests/upload_budget_check.cpp | 96 +++++++++ tools/startup/triage_run.py | 47 ++++- 13 files changed, 577 insertions(+), 26 deletions(-) create mode 100644 src/startup_stubs/upload_budget.h create mode 100644 tests/upload_budget_check.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c1744fd2e3..2c54a5f31e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1560,6 +1560,10 @@ foreach(backend IN ITEMS warp hardware) set_tests_properties(${case}_inline_control PROPERTIES PASS_REGULAR_EXPRESSION "FAIL: inline-control missed Release left buffer above baseline") endforeach() + add_test(NAME startup_resource_budget_creators_${backend} COMMAND startup_resource_budget ${backend} creators) + set_tests_properties(startup_resource_budget_creators_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 + PASS_REGULAR_EXPRESSION "upload creators passed" + ENVIRONMENT "X2W_D3D12_BUDGET=1;X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=0;X2W_D3D12_LINEAR_BUFFERS=copy") endforeach() # X2W_D3D12_RELEASE_RETIRED=1 frees the desktop object of a committed 2D texture only when no recorded list, no submission @@ -2484,6 +2488,10 @@ add_executable(upload_reservation_check tests/upload_reservation_check.cpp) target_include_directories(upload_reservation_check PRIVATE src) target_compile_features(upload_reservation_check PRIVATE cxx_std_17) add_test(NAME upload_reservation COMMAND upload_reservation_check) +add_executable(upload_budget_check tests/upload_budget_check.cpp) +target_include_directories(upload_budget_check PRIVATE src) +target_compile_features(upload_budget_check PRIVATE cxx_std_17) +add_test(NAME upload_budget COMMAND upload_budget_check) # The same slot driven through the stand-in on a desktop device: one refusal line per refused case, and the # forwarded calls counted by a pipeline-statistics query on the desktop list. add_executable(startup_list_dispatch_native tests/startup_list_dispatch_native.cpp) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 976e2f80ee..c296bbd455 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1480,7 +1480,9 @@ per-kind counter: the highest count for a signature kind, bucket and reason, wit A line whose reason is not `bucket/code` with a known bucket is ignored. The GPU Memory Budget section uses complete `x2w-budget` heartbeat lines from -`X2W_D3D12_BUDGET=1`. It counts samples above the local DXGI budget and prints +`X2W_D3D12_BUDGET=1`. A line that also carries the upload creator columns is +still a complete sample. A line with only some of those columns is ignored. +It counts samples above the local DXGI budget and prints the sample with the highest usage-to-budget ratio, plus the sample with the most retained texture bytes. Each row keeps the usage, budget and texture counters from that same heartbeat. Samples with an unavailable or zero local budget @@ -1993,7 +1995,17 @@ come from the desktop allocation info for the created description. A slot 133 texture or buffer with committed backing counts in its committed category. Each opened or created heap uses its desktop heap size once, even when several placements share it. Placed resource sizes describe the objects occupying -heaps, so adding both columns would count the backing twice. +heaps, so adding both columns would count the backing twice. The `upload` +column stays the total of every upload-heap object. Seven columns next to it +name the creator: `upload_ring` (the draw upload ring and the title-input +snapshot pool), `upload_slot27` (device slots 27 and 127), `upload_slot70` +(the unplaced memory-SRV upload fallback), `upload_slot148` (list upload +pages, including a draw that takes a page from the same allocator), +`upload_linear` (linear-buffer staging), `upload_mesh86` (the Mesh86 texture +slot map) and `upload_other` (every other upload-heap object, including device +slot 76). Those seven add up to `upload` in both the live and the retained +halves. A log from before the split has no creator columns, and `upload` is +the same total. Final guest release moves a wrapped allocation to the retained column when the shim still owns the desktop reference. An explicit heap stays live while diff --git a/src/startup_stubs/d3d12_memory_srv.inl b/src/startup_stubs/d3d12_memory_srv.inl index 57663f42ab..ce49ecedb7 100644 --- a/src/startup_stubs/d3d12_memory_srv.inl +++ b/src/startup_stubs/d3d12_memory_srv.inl @@ -446,7 +446,8 @@ Wrapper* unplaced_memory_buffer(Wrapper* device, u64 address, u64 span_bytes, co w->own().backing_budget = backing_budget; if (backing_budget) backing_budget->begin_use(); } else { - w->own().budget = x2w_budget::committed(desktop, native_resource.Get(), D3D12_HEAP_TYPE_UPLOAD); + w->own().budget = x2w_budget::committed(desktop, native_resource.Get(), D3D12_HEAP_TYPE_UPLOAD, + x2w_budget::upload_slot70); } unplaced_buffers.push_back(UnplacedBufferEntry{device, memory.base, page_start, page_span, is_upload, exact, native_resource, w}); diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 6a852b1847..6bca9c4a74 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -1390,7 +1390,8 @@ __declspec(noinline) u64 create_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d, if (SUCCEEDED(child->own().real->QueryInterface(IID_PPV_ARGS(&resource)))) { if constexpr (N == 27) { const auto* props = reinterpret_cast(a); - child->own().budget = x2w_budget::committed(device, resource.Get(), props->Type); + child->own().budget = x2w_budget::committed(device, resource.Get(), props->Type, + x2w_budget::upload_slot27); } else { child->own().budget = x2w_budget::resource(device, resource.Get(), x2w_budget::placed); if (auto* heap = as_wrapper(reinterpret_cast(a))) { @@ -3391,7 +3392,7 @@ UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes, bool sna if (SUCCEEDED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &buffer, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&created)))) { refusal = SnapshotRefusal::map_failed; - auto* budget = x2w_budget::committed(device, created, D3D12_HEAP_TYPE_UPLOAD); + auto* budget = x2w_budget::committed(device, created, D3D12_HEAP_TYPE_UPLOAD, x2w_budget::upload_ring); if (SUCCEEDED(created->Map(0, nullptr, &base))) { try { chunks.push_back(UploadRingChunk{created, static_cast(base), chunk_bytes, 0}); @@ -8449,7 +8450,8 @@ HRESULT create_committed(int slot, Wrapper* self, const D3D12_HEAP_PROPERTIES* p resource->Release(); return E_OUTOFMEMORY; } - w->own().budget = x2w_budget::committed(device_of(self), resource, props->Type); + w->own().budget = x2w_budget::committed(device_of(self), resource, props->Type, + slot == 127 ? x2w_budget::upload_slot27 : x2w_budget::upload_other); w->own().title_memory = title_memory; retire_set_class(w, *props, flags, copy, title_memory); note_resource(w, desc, info.SizeInBytes != ~0ull ? info.SizeInBytes : 0); @@ -8704,7 +8706,7 @@ void report_budget(ID3D12CommandQueue* queue) sample(DXGI_MEMORY_SEGMENT_GROUP_LOCAL, local); sample(DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, nonlocal); const auto counts = x2w_budget::snapshot(); - char text[640]{}; + char text[2048]{}; std::size_t used = 0; for (int i = 0; i < x2w_budget::category_count; ++i) { const auto& active = counts.live[i]; @@ -8713,6 +8715,13 @@ void report_budget(ID3D12CommandQueue* queue) active.bytes, active.objects, held.bytes, held.objects); if (n < 0 || static_cast(n) >= sizeof(text) - used) break; used += n; + // The upload total stays where it is. The creator columns that follow it add up to that total. + if (i == x2w_budget::upload) { + const int extra = x2w_budget::append_upload_creator_fields(text + used, sizeof(text) - used, + counts.upload_live, counts.upload_retained); + if (extra < 0) break; + used += static_cast(extra); + } } // System commit and the physical-page pool behind it: a run that ends on a commit limit (error 1455) shows // whether the pool (sections, slots) or the resources above grew (#1199). @@ -9383,6 +9392,12 @@ extern "C" __declspec(dllexport) BOOL x2w_d3d12_budget_snapshot(x2w_budget::Snap return TRUE; } +// The slot 86 texture-map upload, without a mesh bind. The budget fixture charges one object this way. +extern "C" __declspec(dllexport) HRESULT x2w_budget_mesh86_upload_for_test(ID3D12Device* device, ID3D12Resource** out) +{ + return create_mesh86_map_upload(device, 256, out); +} + // Called by the XG stand-in after it filled texture memory at a title address. extern "C" __declspec(dllexport) void x2w_d3d12_texture_written(void* base, unsigned long long bytes) { diff --git a/src/startup_stubs/linear_buffer_backing.h b/src/startup_stubs/linear_buffer_backing.h index 18c9a9278e..26983f7645 100644 --- a/src/startup_stubs/linear_buffer_backing.h +++ b/src/startup_stubs/linear_buffer_backing.h @@ -1155,7 +1155,8 @@ class LinearBuffers { nullptr, IID_PPV_ARGS(&staging->buffer)); staging->bytes = size; // The pool's staging buffers live for the process: counted as upload memory (X2W_D3D12_BUDGET). - if (SUCCEEDED(hr)) x2w_budget::committed(internal_device_, staging->buffer, D3D12_HEAP_TYPE_UPLOAD); + if (SUCCEEDED(hr)) x2w_budget::committed(internal_device_, staging->buffer, D3D12_HEAP_TYPE_UPLOAD, + x2w_budget::upload_linear); void* mapped = nullptr; const D3D12_RANGE none{0, 0}; if (SUCCEEDED(hr)) hr = staging->buffer->Map(0, &none, &mapped); diff --git a/src/startup_stubs/list_upload_pages.h b/src/startup_stubs/list_upload_pages.h index d435e4c69b..db158ae71e 100644 --- a/src/startup_stubs/list_upload_pages.h +++ b/src/startup_stubs/list_upload_pages.h @@ -34,7 +34,7 @@ inline HRESULT make_page(ID3D12Device* device, UINT64 bytes, Page& page, UploadF if (failure) *failure = {"CreateCommittedResource", hr, bytes}; return hr; } - x2w_budget::committed(device, page.resource.Get(), D3D12_HEAP_TYPE_UPLOAD); + x2w_budget::committed(device, page.resource.Get(), D3D12_HEAP_TYPE_UPLOAD, x2w_budget::upload_slot148); D3D12_RANGE no_reads{}; void* mapped = nullptr; hr = page.resource->Map(0, &no_reads, &mapped); diff --git a/src/startup_stubs/mesh86_cache.inl b/src/startup_stubs/mesh86_cache.inl index 1696db4c34..6c19d31624 100644 --- a/src/startup_stubs/mesh86_cache.inl +++ b/src/startup_stubs/mesh86_cache.inl @@ -419,6 +419,24 @@ ID3D12PipelineState* try_cached_mesh86(const D3D12_PIPELINE_STATE_STREAM_DESC& d } } // namespace +// The slot 86 texture-map upload. bind_mesh86_texture_map uses it, and the budget fixture calls it +// through x2w_budget_mesh86_upload_for_test. +HRESULT create_mesh86_map_upload(ID3D12Device* device, UINT64 map_bytes, ID3D12Resource** out) +{ + if (out) *out = nullptr; + if (!out || !device || !map_bytes) return E_INVALIDARG; + const D3D12_HEAP_PROPERTIES upload{D3D12_HEAP_TYPE_UPLOAD}; + const D3D12_RESOURCE_DESC buffer{D3D12_RESOURCE_DIMENSION_BUFFER, 0, map_bytes, 1, 1, 1, DXGI_FORMAT_UNKNOWN, + {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_NONE}; + ID3D12Resource* created = nullptr; + const HRESULT hr = device->CreateCommittedResource(&upload, D3D12_HEAP_FLAG_NONE, &buffer, + D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&created)); + if (FAILED(hr)) return hr; + x2w_budget::committed(device, created, D3D12_HEAP_TYPE_UPLOAD, x2w_budget::upload_mesh86); + *out = created; + return S_OK; +} + const char* bind_mesh86_texture_map(Wrapper* self, Pass& pass, ID3D12GraphicsCommandList* list) try { const auto& pso = pass.indirect_graphics; @@ -480,14 +498,11 @@ const char* bind_mesh86_texture_map(Wrapper* self, Pass& pass, ID3D12GraphicsCom physical.push_back({texture, view}); } const UINT64 map_bytes = UINT64{records.size()} * sizeof(records.front()); - const D3D12_HEAP_PROPERTIES upload{D3D12_HEAP_TYPE_UPLOAD}; - const D3D12_RESOURCE_DESC buffer{D3D12_RESOURCE_DIMENSION_BUFFER, 0, map_bytes, 1, 1, 1, DXGI_FORMAT_UNKNOWN, - {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_NONE}; - x2w_startup::ComPtr map; - if (FAILED(device->CreateCommittedResource(&upload, D3D12_HEAP_FLAG_NONE, &buffer, - D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&map)))) + ID3D12Resource* created = nullptr; + if (FAILED(create_mesh86_map_upload(device, map_bytes, &created))) return "slot86 Texture1D slot map allocation failed"; - x2w_budget::committed(device, map.Get(), D3D12_HEAP_TYPE_UPLOAD); + x2w_startup::ComPtr map; + map.Attach(created); void* mapped = nullptr; const D3D12_RANGE empty{0, 0}; if (FAILED(map->Map(0, &empty, &mapped))) return "slot86 Texture1D slot map upload failed"; diff --git a/src/startup_stubs/resource_budget.h b/src/startup_stubs/resource_budget.h index 6d6217b40a..c3b4d6074f 100644 --- a/src/startup_stubs/resource_budget.h +++ b/src/startup_stubs/resource_budget.h @@ -1,5 +1,7 @@ #pragma once +#include "upload_budget.h" + #include #include #include @@ -15,6 +17,8 @@ struct Counts { struct Snapshot { Counts live[category_count], retained[category_count]; std::uint64_t unmeasured; + UploadTally upload_live[upload_creator_count]{}; + UploadTally upload_retained[upload_creator_count]{}; }; inline bool enabled() @@ -32,6 +36,7 @@ struct Counter { inline SRWLOCK lock = SRWLOCK_INIT; inline Counter live[category_count], retained[category_count]; inline LONG64 unmeasured{}; +inline Counter upload_live[upload_creator_count], upload_retained[upload_creator_count]; inline Snapshot snapshot() { @@ -41,6 +46,12 @@ inline Snapshot snapshot() result.live[i] = {static_cast(live[i].bytes), static_cast(live[i].objects)}; result.retained[i] = {static_cast(retained[i].bytes), static_cast(retained[i].objects)}; } + for (int i = 0; i < upload_creator_count; ++i) { + result.upload_live[i] = {static_cast(upload_live[i].bytes), + static_cast(upload_live[i].objects)}; + result.upload_retained[i] = {static_cast(upload_retained[i].bytes), + static_cast(upload_retained[i].objects)}; + } result.unmeasured = static_cast(InterlockedCompareExchange64(&unmeasured, 0, 0)); ReleaseSRWLockShared(&lock); return result; @@ -50,7 +61,8 @@ inline Snapshot snapshot() // to retained while the shim still owns the desktop object. The ticket holds no object reference. class Ticket final : public IUnknown { public: - Ticket(Category category, LONG64 bytes, UINT users) : category_(category), bytes_(bytes), users_(users) + Ticket(Category category, LONG64 bytes, UINT users, UploadCreator creator = upload_other) + : category_(category), bytes_(bytes), users_(users), creator_(creator) { AcquireSRWLockExclusive(&lock); adjust(users_ ? live : retained, 1); @@ -108,21 +120,28 @@ class Ticket final : public IUnknown { { InterlockedAdd64(&counters[category_].bytes, bytes_ * delta); InterlockedAdd64(&counters[category_].objects, delta); + if (category_ != upload) return; + Counter* parts = counters == live ? upload_live : upload_retained; + InterlockedAdd64(&parts[creator_].bytes, bytes_ * delta); + InterlockedAdd64(&parts[creator_].objects, delta); } LONG refs_{1}; Category category_; LONG64 bytes_; UINT users_; + UploadCreator creator_; }; -inline Ticket* attach(ID3D12Object* object, Category category, UINT64 bytes, UINT users = 1) +inline Ticket* attach(ID3D12Object* object, Category category, UINT64 bytes, UINT users = 1, + UploadCreator creator = upload_other) { if (!bytes || bytes > INT64_MAX) { InterlockedIncrement64(&unmeasured); return nullptr; } + if (category != upload) creator = upload_other; constexpr GUID key{0x28213cf4, 0x25cf, 0x42be, {0x80, 0xf1, 0xc5, 0x9e, 0x97, 0xf7, 0x20, 0xd1}}; - auto* ticket = new (std::nothrow) Ticket(category, static_cast(bytes), users); + auto* ticket = new (std::nothrow) Ticket(category, static_cast(bytes), users, creator); if (ticket) { const HRESULT hr = object->SetPrivateDataInterface(key, ticket); ticket->Release(); @@ -149,13 +168,16 @@ inline Ticket* resource(ID3D12Device* device, ID3D12Resource* resource, Category return attach(resource, category, device->GetResourceAllocationInfo(0, 1, &desc).SizeInBytes); } -inline Ticket* committed(ID3D12Device* device, ID3D12Resource* resource, D3D12_HEAP_TYPE type) +// creator selects the upload column. Other heap types ignore it and stay on their own category. +inline Ticket* committed(ID3D12Device* device, ID3D12Resource* resource, D3D12_HEAP_TYPE type, + UploadCreator creator = upload_other) { if (!enabled()) return nullptr; const auto desc = resource->GetDesc(); const auto category = type == D3D12_HEAP_TYPE_UPLOAD ? upload : type == D3D12_HEAP_TYPE_READBACK ? readback : desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? buffer : texture; - return attach(resource, category, device->GetResourceAllocationInfo(0, 1, &desc).SizeInBytes); + return attach(resource, category, device->GetResourceAllocationInfo(0, 1, &desc).SizeInBytes, 1, + category == upload ? creator : upload_other); } inline Ticket* backing_heap(ID3D12Heap* object, UINT users = 1) diff --git a/src/startup_stubs/upload_budget.h b/src/startup_stubs/upload_budget.h new file mode 100644 index 0000000000..64436eac67 --- /dev/null +++ b/src/startup_stubs/upload_budget.h @@ -0,0 +1,72 @@ +#pragma once + +// Upload-heap columns of the D3D12 resource budget. No Windows or D3D12 types: the +// counters in resource_budget.h charge one of these for every UPLOAD object, and the +// log prints them beside the existing upload total. The columns add up to that total. + +#include +#include +#include + +namespace x2w_budget { + +enum UploadCreator { + upload_ring = 0, // draw upload ring and the title-input snapshot pool + upload_slot27, // guest committed resources, device slots 27 and 127 + upload_slot70, // unplaced memory-SRV upload fallback + upload_slot148, // list upload pages + upload_linear, // linear-buffer staging + upload_mesh86, // Mesh86 texture slot map + upload_other, // every other upload-heap object + upload_creator_count +}; + +inline constexpr const char* upload_creator_names[] = { + "upload_ring", "upload_slot27", "upload_slot70", "upload_slot148", + "upload_linear", "upload_mesh86", "upload_other"}; + +static_assert(sizeof(upload_creator_names) / sizeof(upload_creator_names[0]) == upload_creator_count, + "one log name per upload creator"); +static_assert(upload_other + 1 == upload_creator_count, "upload_other is the default column"); + +struct UploadTally { + std::uint64_t bytes = 0; + std::uint64_t objects = 0; +}; + +inline UploadTally upload_sum(const UploadTally* parts, int count = upload_creator_count) +{ + UploadTally total{}; + for (int i = 0; i < count; ++i) { + total.bytes += parts[i].bytes; + total.objects += parts[i].objects; + } + return total; +} + +// True when the creator columns are the same objects and bytes as the upload total. +inline bool upload_parts_match(UploadTally total, const UploadTally* parts, int count = upload_creator_count) +{ + const UploadTally sum = upload_sum(parts, count); + return sum.bytes == total.bytes && sum.objects == total.objects; +} + +// Append " name=live-bytes/live-objects+retained-bytes/retained-objects" for every creator. +// Returns the number of bytes written, or -1 when the buffer cannot hold every column. +inline int append_upload_creator_fields(char* out, std::size_t cap, const UploadTally* live, const UploadTally* retained) +{ + std::size_t used = 0; + for (int i = 0; i < upload_creator_count; ++i) { + if (used >= cap) return -1; + const int n = std::snprintf(out + used, cap - used, " %s=%llu/%llu+%llu/%llu", upload_creator_names[i], + static_cast(live[i].bytes), + static_cast(live[i].objects), + static_cast(retained[i].bytes), + static_cast(retained[i].objects)); + if (n < 0 || static_cast(n) >= cap - used) return -1; + used += static_cast(n); + } + return static_cast(used); +} + +} diff --git a/tests/startup_resource_budget.cpp b/tests/startup_resource_budget.cpp index 1e0a0963ca..d0829fb0a0 100644 --- a/tests/startup_resource_budget.cpp +++ b/tests/startup_resource_budget.cpp @@ -1,17 +1,21 @@ #include "startup_stubs/resource_budget.h" +#include "startup_stubs/upload_reservation.h" #include #include #include #include +#include #include #include #include +#include using Microsoft::WRL::ComPtr; extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); extern "C" __declspec(dllimport) BOOL x2w_d3d12_budget_snapshot(x2w_budget::Snapshot*, SIZE_T); +extern "C" __declspec(dllimport) HRESULT x2w_budget_mesh86_upload_for_test(ID3D12Device*, ID3D12Resource**); namespace { using namespace x2w_budget; @@ -59,6 +63,7 @@ bool snap(Snapshot& counts) } bool same(Counts a, Counts b) { return a.bytes == b.bytes && a.objects == b.objects; } +bool same_tally(UploadTally a, UploadTally b) { return a.bytes == b.bytes && a.objects == b.objects; } bool matches(const Snapshot& expected, const Snapshot& actual, const char* stage) { @@ -72,6 +77,25 @@ bool matches(const Snapshot& expected, const Snapshot& actual, const char* stage pass = false; } } + for (unsigned i = 0; i < upload_creator_count; ++i) { + if (!same_tally(expected.upload_live[i], actual.upload_live[i]) || + !same_tally(expected.upload_retained[i], actual.upload_retained[i])) { + const auto a = actual.upload_live[i], b = expected.upload_live[i]; + const auto c = actual.upload_retained[i], d = expected.upload_retained[i]; + std::printf("FAIL: %s %s live=%llu/%llu expected=%llu/%llu retained=%llu/%llu expected=%llu/%llu\n", + stage, upload_creator_names[i], a.bytes, a.objects, b.bytes, b.objects, c.bytes, c.objects, + d.bytes, d.objects); + pass = false; + } + } + const auto live_ok = upload_parts_match(UploadTally{actual.live[upload].bytes, actual.live[upload].objects}, + actual.upload_live); + const auto retained_ok = upload_parts_match( + UploadTally{actual.retained[upload].bytes, actual.retained[upload].objects}, actual.upload_retained); + if (!live_ok || !retained_ok) { + std::printf("FAIL: %s upload columns do not add up to upload\n", stage); + pass = false; + } return pass; } @@ -102,12 +126,21 @@ int check(IUnknown* guest, ID3D12Device* device, ID3D12InfoQueue* debug, bool co Snapshot baseline{}; if (!snap(baseline)) return 4; Snapshot active = baseline, retired = baseline; - const auto measure = [&](Category category, UINT64 bytes, bool guest_owned = true) { + const auto measure = [&](Category category, UINT64 bytes, bool guest_owned = true, + UploadCreator creator = upload_other) { active.live[category].bytes += bytes; ++active.live[category].objects; + if (category == upload) { + active.upload_live[creator].bytes += bytes; + ++active.upload_live[creator].objects; + } if (guest_owned) { retired.retained[category].bytes += bytes; ++retired.retained[category].objects; + if (category == upload) { + retired.upload_retained[creator].bytes += bytes; + ++retired.upload_retained[creator].objects; + } } }; const auto resource_bytes = [&](IUnknown* child) { @@ -214,7 +247,7 @@ int check(IUnknown* guest, ID3D12Device* device, ID3D12InfoQueue* debug, bool co if (!a || !c || a == c) return 4; a[0] = 1; c[0] = 2; - measure(upload, allocation_size(device, buffer_desc(page)), false); + measure(upload, allocation_size(device, buffer_desc(page)), false, upload_slot148); Snapshot actual{}; if (!snap(actual) || !matches(active, actual, "allocated")) return 5; @@ -224,6 +257,12 @@ int check(IUnknown* guest, ID3D12Device* device, ID3D12InfoQueue* debug, bool co if (!bytes || !objects) return 5; std::printf("allocated %s bytes=%llu objects=%llu\n", names[i], bytes, objects); } + for (unsigned i = 0; i < upload_creator_count; ++i) { + const auto bytes = active.upload_live[i].bytes - baseline.upload_live[i].bytes; + const auto objects = active.upload_live[i].objects - baseline.upload_live[i].objects; + if (bytes || objects) + std::printf("allocated %s bytes=%llu objects=%llu\n", upload_creator_names[i], bytes, objects); + } if (!require(invoke(list.Get(), 9), "list close")) return 4; IUnknown* lists[] = {list.Get()}; @@ -245,6 +284,7 @@ int check(IUnknown* guest, ID3D12Device* device, ID3D12InfoQueue* debug, bool co buffer_heap.Reset(); if (!snap(actual)) return 5; active.live[upload] = baseline.live[upload]; + active.upload_live[upload_slot148] = baseline.upload_live[upload_slot148]; if (!matches(active, actual, "allocator reset and shared heap released")) return 5; const auto first_bytes = resource_bytes(first.Get()); first.Reset(); @@ -284,12 +324,205 @@ int check(IUnknown* guest, ID3D12Device* device, ID3D12InfoQueue* debug, bool co std::puts("all live categories returned to baseline"); return 0; } + +bool env_is(const char* name, const char* expected) +{ + char value[16]{}; + const DWORD n = GetEnvironmentVariableA(name, value, sizeof(value)); + return n && n < sizeof(value) && std::strcmp(value, expected) == 0; +} + +struct MemoryView { + D3D12_SHADER_RESOURCE_VIEW_DESC view; + std::uint64_t address; +}; +static_assert(offsetof(MemoryView, address) == 0x28, "slot 70 reads the address at 0x28"); +static_assert(0x104 % 12 != 0, "the slot 70 address must not be a whole number of 12-byte elements"); + +// One object on each upload column. Slot 76 is the column that is not a named creator: it must +// land in upload_other and leave upload_slot27 unchanged. +int check_creators(IUnknown* guest, ID3D12Device* device) +{ + if (!env_is("X2W_D3D12_RENDER", "1") || !env_is("X2W_D3D12_LIST_NOOP", "1") || + !env_is("X2W_D3D12_LINEAR_BUFFERS", "copy")) { + std::puts("FAIL: creators mode needs X2W_D3D12_RENDER=1, X2W_D3D12_LIST_NOOP=1 and X2W_D3D12_LINEAR_BUFFERS=copy"); + return 2; + } + constexpr auto allocation_size = [](ID3D12Device* d, const D3D12_RESOURCE_DESC& desc) { + return d->GetResourceAllocationInfo(0, 1, &desc).SizeInBytes; + }; + const auto resource_bytes = [&](IUnknown* child) { + const auto native = desktop_object(guest, device, child); + return native ? allocation_size(device, native->GetDesc()) : UINT64{0}; + }; + Snapshot baseline{}; + if (!snap(baseline)) return 4; + Snapshot previous = baseline; + const auto expect_delta = [&](UploadCreator creator, UINT64 bytes, const char* stage) { + Snapshot now{}; + if (!snap(now)) return false; + bool pass = now.unmeasured == previous.unmeasured; + const auto upload_bytes = now.live[upload].bytes - previous.live[upload].bytes; + const auto upload_objects = now.live[upload].objects - previous.live[upload].objects; + const auto part_bytes = now.upload_live[creator].bytes - previous.upload_live[creator].bytes; + const auto part_objects = now.upload_live[creator].objects - previous.upload_live[creator].objects; + if (upload_bytes != bytes || upload_objects != 1 || part_bytes != bytes || part_objects != 1) pass = false; + if (!same(now.retained[upload], previous.retained[upload])) pass = false; + for (unsigned i = 0; i < upload_creator_count; ++i) { + if (!same_tally(now.upload_retained[i], previous.upload_retained[i])) pass = false; + if (i != static_cast(creator) && !same_tally(now.upload_live[i], previous.upload_live[i])) + pass = false; + } + if (!upload_parts_match(UploadTally{now.live[upload].bytes, now.live[upload].objects}, now.upload_live) || + !upload_parts_match(UploadTally{now.retained[upload].bytes, now.retained[upload].objects}, + now.upload_retained)) + pass = false; + if (!pass) { + std::printf("FAIL: %s %s upload delta=%llu/%llu column delta=%llu/%llu expected bytes=%llu\n", stage, + upload_creator_names[creator], upload_bytes, upload_objects, part_bytes, part_objects, bytes); + return false; + } + std::printf("%s %s bytes=%llu objects=1\n", stage, upload_creator_names[creator], bytes); + previous = now; + return true; + }; + const auto expect_same_upload = [&](const char* stage) { + Snapshot now{}; + if (!snap(now)) return false; + bool pass = same(now.live[upload], previous.live[upload]) && same(now.retained[upload], previous.retained[upload]); + for (unsigned i = 0; i < upload_creator_count && pass; ++i) + pass = same_tally(now.upload_live[i], previous.upload_live[i]) && + same_tally(now.upload_retained[i], previous.upload_retained[i]); + if (!pass) std::printf("FAIL: %s moved an upload column\n", stage); + else previous = now; + return pass; + }; + + const D3D12_HEAP_PROPERTIES upload_heap{D3D12_HEAP_TYPE_UPLOAD}; + const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}; + const GUID& resource_iid = __uuidof(ID3D12Resource); + const auto slot27 = buffer_desc(4096); + const auto slot127 = buffer_desc(8192); + const auto slot76 = buffer_desc(2048); + ComPtr created27, created127, created76; + if (!require(invoke(guest, 27, &upload_heap, D3D12_HEAP_FLAG_NONE, &slot27, D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, &resource_iid, created27.GetAddressOf()), "slot 27 upload")) return 4; + if (!expect_delta(upload_slot27, resource_bytes(created27.Get()), "slot 27")) return 5; + if (!require(invoke(guest, 127, &upload_heap, D3D12_HEAP_FLAG_NONE, &slot127, D3D12_RESOURCE_STATE_COMMON, + nullptr, nullptr, 0u, nullptr, &resource_iid, created127.GetAddressOf()), "slot 127 upload")) + return 4; + if (!expect_delta(upload_slot27, resource_bytes(created127.Get()), "slot 127")) return 5; + if (!require(invoke(guest, 76, &upload_heap, D3D12_HEAP_FLAG_NONE, &slot76, D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, &resource_iid, created76.GetAddressOf()), "slot 76 upload")) return 4; + if (!expect_delta(upload_other, resource_bytes(created76.Get()), "slot 76")) return 5; + + struct HeapRequest { + D3D12_DESCRIPTOR_HEAP_DESC desc; + std::uint64_t address; + std::uint64_t bytes; + } heap_request{{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}, 0, 0}; + ComPtr heap; + if (!require(invoke(guest, 14, &heap_request, &__uuidof(ID3D12DescriptorHeap), heap.GetAddressOf()), "slot 14 heap")) + return 4; + const auto cpu = invoke(heap.Get(), 9, static_cast(nullptr)); + if (!cpu) { + std::puts("FAIL: descriptor heap CPU handle"); + return 4; + } + auto* view_memory = static_cast(VirtualAlloc(nullptr, static_cast(page), + MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + if (!view_memory) return 4; + const D3D12_RESOURCE_DESC view_desc = buffer_desc(256); + MemoryView memory_view{}; + memory_view.view.Format = DXGI_FORMAT_UNKNOWN; + memory_view.view.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; + memory_view.view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + memory_view.view.Buffer.NumElements = 1; + memory_view.view.Buffer.StructureByteStride = 12; + memory_view.address = reinterpret_cast(view_memory) + 0x104; + invoke(guest, 70, &view_desc, &memory_view, cpu); + if (!expect_delta(upload_slot70, allocation_size(device, buffer_desc(12)), "slot 70")) return 5; + + const auto type = D3D12_COMMAND_LIST_TYPE_DIRECT; + const D3D12_COMMAND_QUEUE_DESC queue_desc{}; + ComPtr queue, allocator, list; + if (!require(invoke(guest, 8, &queue_desc, &__uuidof(ID3D12CommandQueue), queue.GetAddressOf()), "queue") + || !require(invoke(guest, 9, type, &__uuidof(ID3D12CommandAllocator), allocator.GetAddressOf()), "allocator") + || !require(invoke(guest, 73, &type, allocator.Get(), nullptr, &__uuidof(ID3D12GraphicsCommandList), + list.GetAddressOf()), "list") + || !require(invoke(list.Get(), 139, allocator.Get(), nullptr), "list reset")) return 4; + auto* uploaded = invoke(list.Get(), 148, UINT64{0x280}, UINT64{0x10}); + if (!uploaded) { + std::puts("FAIL: slot 148 allocation"); + return 4; + } + uploaded[0] = 1; + if (!expect_delta(upload_slot148, allocation_size(device, buffer_desc(page)), "slot 148")) return 5; + + std::vector texels(64u * 64u * 4u, 1); + const D3D12_RESOURCE_DESC texture{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 64, 64, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, + {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE}; + ComPtr source, dest; + if (!require(invoke(guest, 133, texels.data(), &texture, D3D12_RESOURCE_STATE_COMMON, nullptr, &resource_iid, + source.GetAddressOf()), "slot 133 texture") + || !require(invoke(guest, 127, &default_heap, D3D12_HEAP_FLAG_NONE, &texture, D3D12_RESOURCE_STATE_COMMON, + nullptr, nullptr, 0u, nullptr, &resource_iid, dest.GetAddressOf()), "slot 127 texture")) + return 4; + if (!expect_same_upload("textures before the ring")) return 5; + invoke(list.Get(), 17, dest.Get(), source.Get()); + if (!expect_delta(upload_ring, initial_upload_chunk_bytes, "upload ring")) return 5; + + auto* linear_memory = static_cast(VirtualAlloc(nullptr, static_cast(page), + MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + if (!linear_memory) return 4; + const auto linear_desc = buffer_desc(256); + ComPtr linear; + if (!require(invoke(guest, 133, linear_memory + 16, &linear_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, + &resource_iid, linear.GetAddressOf()), "slot 133 linear buffer")) return 4; + if (!expect_same_upload("linear buffer before submit")) return 5; + if (!require(invoke(list.Get(), 9), "list close")) return 4; + IUnknown* lists[] = {list.Get()}; + invoke(queue.Get(), 10, 1u, lists); + if (!expect_delta(upload_linear, allocation_size(device, buffer_desc(65536)), "linear staging")) return 5; + + ComPtr mesh_map; + if (!require(x2w_budget_mesh86_upload_for_test(device, mesh_map.GetAddressOf()), "mesh86 upload")) return 4; + if (!expect_delta(upload_mesh86, allocation_size(device, buffer_desc(256)), "mesh86 map")) return 5; + + Snapshot done{}; + if (!snap(done)) return 4; + const std::uint64_t objects[upload_creator_count] = {1, 2, 1, 1, 1, 1, 1}; + static_assert(upload_ring == 0 && upload_slot27 == 1 && upload_slot70 == 2 && upload_slot148 == 3 && + upload_linear == 4 && upload_mesh86 == 5 && upload_other == 6, + "creator column order"); + for (unsigned i = 0; i < upload_creator_count; ++i) { + const auto got = done.upload_live[i].objects - baseline.upload_live[i].objects; + std::printf("column %s bytes=%llu objects=%llu\n", upload_creator_names[i], + done.upload_live[i].bytes - baseline.upload_live[i].bytes, got); + if (got != objects[i]) { + std::printf("FAIL: %s objects=%llu expected=%llu\n", upload_creator_names[i], got, objects[i]); + return 5; + } + } + if (!upload_parts_match(UploadTally{done.live[upload].bytes, done.live[upload].objects}, done.upload_live) || + !upload_parts_match(UploadTally{done.retained[upload].bytes, done.retained[upload].objects}, + done.upload_retained)) { + std::puts("FAIL: creator columns do not add up to upload"); + return 5; + } + VirtualFree(view_memory, 0, MEM_RELEASE); + VirtualFree(linear_memory, 0, MEM_RELEASE); + std::puts("upload creators passed"); + return 0; +} } int main(int argc, char** argv) { + const bool control = argc == 3 && std::strcmp(argv[2], "inline-control") == 0; + const bool creators = argc == 3 && std::strcmp(argv[2], "creators") == 0; if (argc < 2 || argc > 3 || (std::strcmp(argv[1], "warp") && std::strcmp(argv[1], "hardware")) - || (argc == 3 && std::strcmp(argv[2], "inline-control"))) return 2; + || (argc == 3 && !control && !creators)) return 2; ComPtr factory; if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return 3; const auto adapter = pick_adapter(factory.Get(), std::strcmp(argv[1], "warp") == 0); @@ -325,5 +558,6 @@ int main(int argc, char** argv) std::printf("adapter=%ls driver=%llu.%llu.%llu.%llu debug_layer=%s\n", desc.Description, driver >> 48, (driver >> 32) & 0xffff, (driver >> 16) & 0xffff, driver & 0xffff, debug ? "enabled" : "off"); - return check(guest.Get(), device.Get(), debug.Get(), argc == 3); + return creators ? check_creators(guest.Get(), device.Get()) + : check(guest.Get(), device.Get(), debug.Get(), control); } diff --git a/tests/startup_triage_run_check.py b/tests/startup_triage_run_check.py index c9ab0ff510..d0cb5faacb 100644 --- a/tests/startup_triage_run_check.py +++ b/tests/startup_triage_run_check.py @@ -875,6 +875,36 @@ def sample(local, texture, commit=""): if "Highest local usage-to-budget ratio" in report or "Most retained texture bytes" not in report: failures.append("an unavailable DXGI budget hid the retained-texture sample or produced a ratio") + # 10+20+5+8+9+7+11 = 70 live bytes, 1+2+1+1+1+0+1 = 7 live objects, + # 1+0+0+1+0+1+1 = 4 retained bytes, 1+0+0+0+0+1+0 = 2 retained objects. + parts = ((10, 1, 1, 1), (20, 2, 0, 0), (5, 1, 0, 0), (8, 1, 1, 0), + (9, 1, 0, 0), (7, 0, 1, 1), (11, 1, 1, 0)) + columns = " " + " ".join( + f"{name}={live_bytes}/{live_objects}+{retained_bytes}/{retained_objects}" + for name, (live_bytes, live_objects, retained_bytes, retained_objects) + in zip(triage_run.UPLOAD_CREATORS, parts)) + balanced = sample("1000/1000", "1/1+0/0").replace("upload=0/0+0/0", "upload=70/7+4/2" + columns) + unbalanced = balanced.replace("upload_ring=10/1+1/1", "upload_ring=11/1+1/1", 1) + partial = balanced.replace(" upload_other=11/1+1/0", "", 1) + if triage_run.upload_columns_balanced(sample("1000/1000", "1/1+0/0")) is not None: + failures.append("a budget line without creator columns was not accepted as the old shape") + if triage_run.RE_GPU_BUDGET.fullmatch(balanced) is None or triage_run.upload_columns_balanced(balanced) is not True: + failures.append("a balanced upload-creator line did not match") + if triage_run.RE_GPU_BUDGET.fullmatch(unbalanced) is None or triage_run.upload_columns_balanced(unbalanced) is not False: + failures.append("an unbalanced upload-creator line was accepted as adding up") + if triage_run.RE_GPU_BUDGET.fullmatch(partial) is not None or triage_run.upload_columns_balanced(partial) is not False: + failures.append("a budget line with only some creator columns matched") + log.write_text(balanced + "\n", encoding="utf-8") + report = triage_run.triage_run(log) + if "### GPU Memory Budget" not in report or "Complete samples: `1`" not in report: + failures.append("a balanced upload-creator line was not a complete budget sample") + log.write_text(unbalanced + "\n", encoding="utf-8") + if "Complete samples: `1`" not in triage_run.triage_run(log): + failures.append("an unbalanced creator line stopped being a complete sample") + log.write_text(partial + "\n", encoding="utf-8") + if "### GPU Memory Budget" in triage_run.triage_run(log): + failures.append("a partial creator line produced a budget section") + def main() -> int: failures: list[str] = [] diff --git a/tests/upload_budget_check.cpp b/tests/upload_budget_check.cpp new file mode 100644 index 0000000000..105834a8c2 --- /dev/null +++ b/tests/upload_budget_check.cpp @@ -0,0 +1,96 @@ +// Upload-creator columns without a device: each column, their sum, and the log text. +#include "startup_stubs/upload_budget.h" + +#include +#include +#include + +namespace { + +int failures = 0; + +void expect(bool ok, const char* what) +{ + if (ok) return; + std::fprintf(stderr, "FAIL: %s\n", what); + ++failures; +} + +void check_sum() +{ + x2w_budget::UploadTally live[x2w_budget::upload_creator_count]{}; + x2w_budget::UploadTally retained[x2w_budget::upload_creator_count]{}; + x2w_budget::UploadTally live_total{}; + x2w_budget::UploadTally retained_total{}; + for (int i = 0; i < x2w_budget::upload_creator_count; ++i) { + live[i] = {1000u + static_cast(i) * 3u, static_cast(i + 1)}; + retained[i] = {10u + static_cast(i), static_cast(i)}; + live_total.bytes += live[i].bytes; + live_total.objects += live[i].objects; + retained_total.bytes += retained[i].bytes; + retained_total.objects += retained[i].objects; + } + expect(x2w_budget::upload_parts_match(live_total, live), "live columns add up"); + expect(x2w_budget::upload_parts_match(retained_total, retained), "retained columns add up"); + expect(x2w_budget::upload_sum(live).bytes == live_total.bytes, "live byte sum"); + expect(x2w_budget::upload_sum(retained).objects == retained_total.objects, "retained object sum"); + + x2w_budget::UploadTally broken[x2w_budget::upload_creator_count]; + for (int i = 0; i < x2w_budget::upload_creator_count; ++i) broken[i] = live[i]; + broken[x2w_budget::upload_other].bytes += 1; + expect(!x2w_budget::upload_parts_match(live_total, broken), "one extra byte must fail the sum"); + broken[x2w_budget::upload_other].bytes -= 1; + broken[x2w_budget::upload_ring].objects += 1; + expect(!x2w_budget::upload_parts_match(live_total, broken), "one extra object must fail the sum"); + + char text[1024]; + const int written = x2w_budget::append_upload_creator_fields(text, sizeof(text), live, retained); + expect(written > 0, "creator columns fit"); + if (written > 0) { + expect(text[written] == '\0', "formatter writes a terminator"); + int seen = 0; + for (int i = 0; i < x2w_budget::upload_creator_count; ++i) { + char piece[96]; + std::snprintf(piece, sizeof(piece), " %s=%llu/%llu+%llu/%llu", x2w_budget::upload_creator_names[i], + static_cast(live[i].bytes), + static_cast(live[i].objects), + static_cast(retained[i].bytes), + static_cast(retained[i].objects)); + const char* found = std::strstr(text, piece); + expect(found != nullptr, x2w_budget::upload_creator_names[i]); + if (found) { + ++seen; + expect(std::strstr(found + 1, piece) == nullptr, "creator column is printed once"); + } + } + expect(seen == x2w_budget::upload_creator_count, "every creator column is printed"); + expect(std::strncmp(text, " upload_ring=", 13) == 0, "upload_ring is the first column"); + expect(std::strstr(text, " upload=") == nullptr, "the formatter does not rename the upload total"); + } + expect(x2w_budget::append_upload_creator_fields(text, 8, live, retained) < 0, "a short buffer is refused"); +} + +void check_names() +{ + expect(x2w_budget::upload_other + 1 == x2w_budget::upload_creator_count, "upload_other is the last column"); + for (int i = 0; i < x2w_budget::upload_creator_count; ++i) { + expect(x2w_budget::upload_creator_names[i][0] != '\0', "creator name is not empty"); + for (int j = i + 1; j < x2w_budget::upload_creator_count; ++j) + expect(std::strcmp(x2w_budget::upload_creator_names[i], x2w_budget::upload_creator_names[j]) != 0, + "creator names are distinct"); + } +} + +} + +int main() +{ + check_names(); + check_sum(); + if (failures) { + std::fprintf(stderr, "%d upload budget check(s) failed\n", failures); + return 1; + } + std::puts("upload budget checks passed"); + return 0; +} diff --git a/tools/startup/triage_run.py b/tools/startup/triage_run.py index 055cb037c4..205f37811d 100644 --- a/tools/startup/triage_run.py +++ b/tools/startup/triage_run.py @@ -306,18 +306,63 @@ BUDGET_DECIMAL = r"[0-9]{1,20}" BUDGET_SEGMENT = rf"(?:{BUDGET_DECIMAL}/{BUDGET_DECIMAL}|n/a)" BUDGET_CATEGORY = rf"{BUDGET_DECIMAL}/{BUDGET_DECIMAL}\+{BUDGET_DECIMAL}/{BUDGET_DECIMAL}" +# Printed after upload= and before readback=. Older lines omit the whole group. +UPLOAD_CREATORS = ( + "upload_ring", + "upload_slot27", + "upload_slot70", + "upload_slot148", + "upload_linear", + "upload_mesh86", + "upload_other", +) +_UPLOAD_CREATOR_FIELDS = "".join(rf" {name}={BUDGET_CATEGORY}" for name in UPLOAD_CREATORS) RE_GPU_BUDGET = re.compile( rf"x2w-budget local=(?:(?P{BUDGET_DECIMAL})/(?P{BUDGET_DECIMAL})|n/a)" rf" nonlocal={BUDGET_SEGMENT} placed={BUDGET_CATEGORY}" rf" texture=(?P{BUDGET_DECIMAL})/{BUDGET_DECIMAL}" rf"\+(?P{BUDGET_DECIMAL})/(?P{BUDGET_DECIMAL})" - + "".join(rf" {name}={BUDGET_CATEGORY}" for name in ("buffer", "upload", "readback", "heap")) + + "".join(rf" {name}={BUDGET_CATEGORY}" for name in ("buffer", "upload")) + + rf"(?:{_UPLOAD_CREATOR_FIELDS})?" + + "".join(rf" {name}={BUDGET_CATEGORY}" for name in ("readback", "heap")) + rf" unmeasured={BUDGET_DECIMAL}" # Added with the system commit and pool counters (absent in older logs): commit=/ avail= in MiB. + rf"(?: commit=(?:{BUDGET_DECIMAL}/{BUDGET_DECIMAL}|n/a) avail=(?:{BUDGET_DECIMAL}|n/a)" + rf" sections=(?:{BUDGET_DECIMAL}|n/a) slabs=(?:{BUDGET_DECIMAL}|n/a) slots=(?:{BUDGET_DECIMAL}/{BUDGET_DECIMAL}|n/a)" + rf" job_peak=(?:{BUDGET_DECIMAL}|n/a) job_limit=(?:{BUDGET_DECIMAL}|none|n/a))?" ) + + +def upload_columns_balanced(line: str) -> bool | None: + """None when `line` is a budget heartbeat with no creator columns. + + True when every creator column is present and each of its four counts adds up to `upload`. + False when the line is not a complete heartbeat, a creator column is missing, or the sums differ. + """ + if not RE_GPU_BUDGET.fullmatch(line): + return False + if " upload_ring=" not in line: + return None + + def component(name: str) -> tuple[int, int, int, int] | None: + found = re.search(rf"(?:^| ){name}=([0-9]+)/([0-9]+)\+([0-9]+)/([0-9]+)(?: |$)", line) + if not found: + return None + return tuple(int(part) for part in found.groups()) + + total = component("upload") + if total is None: + return False + sums = [0, 0, 0, 0] + for name in UPLOAD_CREATORS: + part = component(name) + if part is None: + return False + for index, value in enumerate(part): + sums[index] += value + return tuple(sums) == total + + OTHER = "other (text omitted)" RE_DEC = re.compile(r"[0-9]{1,10}\Z") RE_HEX = re.compile(r"0x[0-9a-fA-F]{1,8}\Z") From f921aa4a13fc7045794ea61495352d0833094299 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 1 Dec 2026 14:40:57 +0000 Subject: [PATCH 2/3] budget: exercise creator accounting in PR checks Build and run the portable upload budget check and the WARP creator fixture in the pull request preset. Verify that releasing slot 27 moves its aggregate and creator charge from live to retained. AI-assisted (Cursor) Co-authored-by: SimSalabimse --- CMakePresets.json | 4 ++-- tests/startup_resource_budget.cpp | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 6bd4804f08..0416244a1b 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -34,7 +34,7 @@ "xmem_shim_test_controls", "startup_stub_xcrdapi", "startup_stub_xcrdapi_break_window_control", "xmem_run_break_readers_check", "xmem_run_break_readers_control", "xmem_cross_call_merge_check", "xmem_placeholder_split_check", "xmem_map_split_first_check", "startup_dstorage_break_window_check", - "xmem_page_runs_device_check", "linear_buffer_backing_check", + "xmem_page_runs_device_check", "linear_buffer_backing_check", "upload_budget_check", "startup_resource_budget", "bcpack_formats_check", "startup_stub_consumer", "startup_inline_retire", "startup_resource_retire_title", "startup_table_lock", "native_compute_ir_dump", "native_pixel_ir_dump", "native_store_replay", "native_raw_buffer_view_check" ] @@ -50,7 +50,7 @@ "displayName": "Windows pull request smoke tests", "filter": { "include": { - "name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_mesh57_real_shape|startup_mesh57_runtime(_[a-z]+)?_warp|native_fixed_mesh57_published_pair(_refusal)?_warp|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_(dispatch|constants|count)_inline_warp|startup_inline_retire_(on|control)_warp|startup_resource_retire_title_inline_warp_debug0|startup_table_lock_(control_)?warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_indirect_mesh_cache_heap(_wide)?_warp|runtime_mesh86_cache_cli_check|startup_shader_cache_key|startup_present_readback|startup_present_planes(_control)?|startup_present_composite_(warp|overlay_removed_control_warp)|startup_mesh_title_snapshot_warp|startup_list_copy_(warp|title_snapshot_warp|validation(_[a-z_]+_control)?)|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|startup_stage_package|startup_compute_packed_d16_x_uav_reference|native_.*_(ir|dxil|controls)|native_direct_mesh|startup_triage_run)$" + "name": "^(bcpack_all_formats(_golden_control)?|startup_bcpack_storage|desktop_root_signature|era_heap_(allocation|failure)|xmem_shim_mapped_pages|cpu_aot_fuzz_smoke|cpu_archive_texture_gpu_warp|dual_uav_warp|spirv_.*_warp|static_exe_intake|descriptor_heap_paging(_swapped)?_warp|startup_launch|startup_mesh57_real_shape|startup_mesh57_runtime(_[a-z]+)?_warp|native_fixed_mesh57_published_pair(_refusal)?_warp|startup_tools_lint|startup_gfx2hlsl_cvt_off_f32_i4_controls|startup_gfx2hlsl_sad_hi_msad_controls|startup_indirect_(dispatch|count_split|draw(_indexed)?|mesh(_xmem_arguments(_skip_wait)?)?|counts_control)_warp|startup_indirect_(dispatch|constants|count)_inline_warp|startup_inline_retire_(on|control)_warp|startup_resource_retire_title_inline_warp_debug0|startup_resource_budget_creators_warp|startup_table_lock_(control_)?warp|startup_indirect_mesh_lease_warp_dual(-trace(-disabled)?)?|startup_indirect_mesh_cache_heap(_wide)?_warp|runtime_mesh86_cache_cli_check|startup_shader_cache_key|startup_present_readback|startup_present_planes(_control)?|startup_present_composite_(warp|overlay_removed_control_warp)|startup_mesh_title_snapshot_warp|startup_list_copy_(warp|title_snapshot_warp|validation(_[a-z_]+_control)?)|xmem_(run_break_readers|cross_call_merge|placeholder_split|map_split_first)(_[a-z_]+)?|startup_dstorage_break_window(_[a-z_]+)?|xmem_page_runs_device(_join)?_warp|linear_buffer_backing_share_warp|upload_budget|startup_stage_package|startup_compute_packed_d16_x_uav_reference|native_.*_(ir|dxil|controls)|native_direct_mesh|startup_triage_run)$" } } }, diff --git a/tests/startup_resource_budget.cpp b/tests/startup_resource_budget.cpp index d0829fb0a0..ef372864e2 100644 --- a/tests/startup_resource_budget.cpp +++ b/tests/startup_resource_budget.cpp @@ -407,7 +407,8 @@ int check_creators(IUnknown* guest, ID3D12Device* device) ComPtr created27, created127, created76; if (!require(invoke(guest, 27, &upload_heap, D3D12_HEAP_FLAG_NONE, &slot27, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, &resource_iid, created27.GetAddressOf()), "slot 27 upload")) return 4; - if (!expect_delta(upload_slot27, resource_bytes(created27.Get()), "slot 27")) return 5; + const UINT64 created27_bytes = resource_bytes(created27.Get()); + if (!expect_delta(upload_slot27, created27_bytes, "slot 27")) return 5; if (!require(invoke(guest, 127, &upload_heap, D3D12_HEAP_FLAG_NONE, &slot127, D3D12_RESOURCE_STATE_COMMON, nullptr, nullptr, 0u, nullptr, &resource_iid, created127.GetAddressOf()), "slot 127 upload")) return 4; @@ -510,6 +511,23 @@ int check_creators(IUnknown* guest, ID3D12Device* device) std::puts("FAIL: creator columns do not add up to upload"); return 5; } + + // Releasing a guest wrapper moves its desktop allocation to retained. Check the aggregate and + // its creator column together; matches also proves that every other creator stayed unchanged. + Snapshot retired27 = done; + retired27.live[upload].bytes -= created27_bytes; + --retired27.live[upload].objects; + retired27.retained[upload].bytes += created27_bytes; + ++retired27.retained[upload].objects; + retired27.upload_live[upload_slot27].bytes -= created27_bytes; + --retired27.upload_live[upload_slot27].objects; + retired27.upload_retained[upload_slot27].bytes += created27_bytes; + ++retired27.upload_retained[upload_slot27].objects; + created27.Reset(); + Snapshot after_retire{}; + if (!snap(after_retire) || !matches(retired27, after_retire, "slot 27 release")) return 5; + std::printf("slot 27 release moved %llu bytes and one object from live to retained\n", created27_bytes); + VirtualFree(view_memory, 0, MEM_RELEASE); VirtualFree(linear_memory, 0, MEM_RELEASE); std::puts("upload creators passed"); From 2638480a53a58931f58d9bd9d0b5c3f33822dbb2 Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Wed, 2 Dec 2026 16:55:58 +0800 Subject: [PATCH 3/3] tests: account for the Mesh86 upload budget test export --- tests/standin_exports_check.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/standin_exports_check.py b/tests/standin_exports_check.py index 2d8c733941..f0569d600c 100644 --- a/tests/standin_exports_check.py +++ b/tests/standin_exports_check.py @@ -116,7 +116,7 @@ def test_real_catalogs(): expected = set(modules[:8]) | {"xgameruntime.dll"} check(set(catalogs) == expected, f"load_catalogs keys {sorted(catalogs)} != {sorted(expected)}") - counts = {"xmem": 54, "d3d12_xs": 79, "acphal": 10, "chad": 5, + counts = {"xmem": 54, "d3d12_xs": 80, "acphal": 10, "chad": 5, "xapu": 6, "xcrdapi": 3, "xg_xs": 2, "gameinput": 2} for stem, path in DEFAULT_DEFS.items(): catalog = catalogs[stem + ".dll"] @@ -155,8 +155,13 @@ def test_real_catalogs(): == Counter({"trap": 9, "implemented": 1}), "acphal status counts differ") check(Counter(e.status for e in catalogs["d3d12_xs.dll"].entries.values()) - == Counter({"implemented": 35, "unmeasured": 44}), + == Counter({"implemented": 35, "unmeasured": 45}), "d3d12_xs status counts differ") + mesh86_upload = catalogs["d3d12_xs.dll"].lookup("x2w_budget_mesh86_upload_for_test") + check(mesh86_upload.status == "unmeasured" and + mesh86_upload.source == _direct_export_source( + "src/startup_stubs/d3d12_xs_device.cpp", "x2w_budget_mesh86_upload_for_test"), + "d3d12_xs Mesh86 upload test export evidence") check(Counter(e.status for e in catalogs["xgameruntime.dll"].entries.values()) == Counter({"implemented": 38}), "xgameruntime status counts differ")