diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 53921d6d51..064770e9a0 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -2765,6 +2765,7 @@ ID3D12PipelineState* build_stream_pipeline(const D3D12_PIPELINE_STATE_STREAM_DES volatile LONG64 upload_ring_reserved_bytes, upload_ring_capacity_bytes; volatile LONG upload_ring_reservations, upload_ring_refusals; +SnapshotPoolTotals snapshot_pool_totals; // allocator_lock // snapshot = true takes the reservation from the title input snapshot pool (128 MiB, chunks numbered after the ring's). UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes, bool snapshot = false) @@ -2773,13 +2774,16 @@ UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes, bool sna const unsigned base = snapshot ? snapshot_chunk_base : 0; const u64 max_alloc_bytes = max_pool_bytes / 4; UploadReservation reservation; - if (bytes == 0 || bytes > max_alloc_bytes) return reservation; + auto* snapshot_totals = snapshot ? &snapshot_pool_totals : nullptr; + if (bytes == 0 || bytes > max_alloc_bytes) { + if (snapshot_totals) { + AcquireSRWLockExclusive(&allocator_lock); + snapshot_totals->note_refusal(SnapshotRefusal::invalid_size); + ReleaseSRWLockExclusive(&allocator_lock); + } + return reservation; + } const u64 rounded = (static_cast(bytes) + 511) & ~u64{511}; - const auto note_reserved = [snapshot](u64 reserved) { - if (snapshot) return; - InterlockedAdd64(&upload_ring_reserved_bytes, static_cast(reserved)); - InterlockedIncrement(&upload_ring_reservations); - }; auto& chunks = snapshot ? snapshot_chunks : upload_chunks; static unsigned actives[2] = {0, 0}; @@ -2791,26 +2795,30 @@ UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes, bool sna const auto finish = [&](unsigned index) { active = index; chunks[index].hold(list->own().upload_chunks, base + index); + if (!snapshot) { + InterlockedAdd64(&upload_ring_reserved_bytes, static_cast(rounded)); + InterlockedIncrement(&upload_ring_reservations); + } ReleaseSRWLockExclusive(&allocator_lock); return reservation; }; if (!chunks.empty()) { - reservation = reserve_upload_chunk(chunks[active], rounded); - if (reservation.mapped) { - note_reserved(rounded); - return finish(active); - } + reservation = reserve_upload_chunk(chunks[active], rounded, snapshot_totals); + if (reservation.mapped) return finish(active); } const bool grow_released_ring = !snapshot && ring_release_at_submit(); const u64 budget_bytes = grow_released_ring ? max_released_upload_ring_bytes : max_pool_bytes; const u64 remaining = total_bytes < budget_bytes ? budget_bytes - total_bytes : 0; + auto refusal = SnapshotRefusal::no_unused_capacity; if (rounded <= remaining) { + refusal = SnapshotRefusal::no_device; u64 chunk_bytes = chunks.empty() || grow_released_ring ? initial_upload_chunk_bytes : chunks.back().capacity * 2; if (chunk_bytes < rounded) chunk_bytes = rounded; if (chunk_bytes > remaining) chunk_bytes = remaining; auto* device = the_device ? device_of(the_device) : nullptr; if (device) { + refusal = SnapshotRefusal::create_failed; const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_UPLOAD}; D3D12_RESOURCE_DESC buffer{}; buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; @@ -2824,16 +2832,17 @@ UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes, bool sna void* base = nullptr; 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); if (SUCCEEDED(created->Map(0, nullptr, &base))) { try { chunks.push_back(UploadRingChunk{created, static_cast(base), chunk_bytes, 0}); total_bytes += chunk_bytes; if (!snapshot) InterlockedAdd64(&upload_ring_capacity_bytes, static_cast(chunk_bytes)); - reservation = reserve_upload_chunk(chunks.back(), rounded); - note_reserved(rounded); + reservation = reserve_upload_chunk(chunks.back(), rounded, snapshot_totals); return finish(static_cast(chunks.size() - 1)); } catch (const std::bad_alloc&) { + refusal = SnapshotRefusal::bookkeeping_failed; created->Unmap(0, nullptr); if (budget) budget->end_use(); created->Release(); @@ -2847,27 +2856,25 @@ UploadReservation reserve_upload_ring(Wrapper* list, std::size_t bytes, bool sna } // Allocation failed or the cap was reached. Older unused tails are still safe. for (unsigned index = 0; index < chunks.size(); ++index) { - reservation = reserve_upload_chunk(chunks[index], rounded); - if (reservation.mapped) { - note_reserved(rounded); - return finish(index); - } + reservation = reserve_upload_chunk(chunks[index], rounded, snapshot_totals); + if (reservation.mapped) return finish(index); } for (unsigned index = 0; index < chunks.size(); ++index) { - reservation = reuse_upload_chunk(chunks[index], base + index, rounded, queue_submits); - if (reservation.mapped) { - note_reserved(rounded); - return finish(index); - } + reservation = reuse_upload_chunk(chunks[index], base + index, rounded, queue_submits, snapshot_totals); + if (reservation.mapped) return finish(index); } + const bool log_snapshot_refusal = snapshot_totals && snapshot_totals->note_refusal(refusal); ReleaseSRWLockExclusive(&allocator_lock); - if (!snapshot) InterlockedIncrement(&upload_ring_refusals); - static LONG refused[2]; - if (first_calls(refused[snapshot ? 1 : 0], 8)) - log(snapshot ? "x2w-draw snapshot pool reservation refused: no unused capacity" - : "x2w-draw upload ring reservation refused: no unused capacity"); + if (log_snapshot_refusal) + log("x2w-draw snapshot pool reservation refused: no unused capacity"); + if (!snapshot) { + InterlockedIncrement(&upload_ring_refusals); + static LONG refused; + if (first_calls(refused, 8)) + log("x2w-draw upload ring reservation refused: no unused capacity"); + } return reservation; } @@ -7841,9 +7848,15 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa log("x2w-draw pso recipes totals: loaded %ld, precreated %ld, mismatches %ld, fallbacks %ld", pso_recipes_loaded, pso_recipes_precreated, pso_recipe_mismatches, pso_recipe_fallbacks); if (render_mode() && frame % 300 == 0) { - log("x2w-render upload ring: reserved %llu MiB in %ld reservations, refusals %ld, capacity %llu MiB", + AcquireSRWLockShared(&allocator_lock); + const auto snapshots = snapshot_pool_totals; + const auto snapshot_count = snapshot_chunks.size(); + ReleaseSRWLockShared(&allocator_lock); + char snapshot_summary[512]; + snapshots.report(snapshot_summary, sizeof(snapshot_summary), snapshot_count); + log("x2w-render upload ring: reserved %llu MiB in %ld reservations, refusals %ld, capacity %llu MiB; %s", static_cast(upload_ring_reserved_bytes) >> 20, upload_ring_reservations, upload_ring_refusals, - static_cast(upload_ring_capacity_bytes) >> 20); + static_cast(upload_ring_capacity_bytes) >> 20, snapshot_summary); log("x2w-render compute dispatches: translated %ld, refused %ld, no translation %ld, not modelled %ld, builder gate refused %ld; placed inputs from a GPU copy %ld, from a title snapshot %ld (%ld KiB)", compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled, compute_builder_gate_refusals, compute_inputs_gpu_copied, compute_inputs_snapshot, compute_inputs_snapshot_kib); diff --git a/src/startup_stubs/memory_srv_census.h b/src/startup_stubs/memory_srv_census.h index 037e4a11f6..75d4e92aca 100644 --- a/src/startup_stubs/memory_srv_census.h +++ b/src/startup_stubs/memory_srv_census.h @@ -115,6 +115,7 @@ class MemorySrvCensus { ++shape_count_; } else { ++more_shape_calls_; + ++more_shape_reasons_[reason]; } } ReleaseSRWLockExclusive(&lock_); @@ -131,7 +132,7 @@ class MemorySrvCensus { ReleaseSRWLockExclusive(&lock_); return false; } - char line[1024]; + char line[1534]; int n = std::snprintf(line, sizeof(line), "x2w-d3d12 memory SRV refusals (slot 70): total %llu;", static_cast(total_)); for (int r = 0; r <= memory_srv_reason_count && n > 0 && n < static_cast(sizeof(line)); ++r) @@ -154,6 +155,14 @@ class MemorySrvCensus { static_cast(s.calls)); emit(static_cast(line)); } + if (more_shape_calls_) { + n = std::snprintf(line, sizeof(line), "x2w-d3d12 memory SRV refusals past the shape table (slot 70):"); + for (int r = 0; r < memory_srv_reason_count && n > 0 && n < static_cast(sizeof(line)); ++r) + if (more_shape_reasons_[r]) + n += std::snprintf(line + n, sizeof(line) - n, " %s=%llu", memory_srv_reasons[r], + static_cast(more_shape_reasons_[r])); + emit(static_cast(line)); + } ReleaseSRWLockExclusive(&lock_); return true; } @@ -166,6 +175,7 @@ class MemorySrvCensus { } SRWLOCK lock_ = SRWLOCK_INIT; std::uint64_t reasons_[memory_srv_reason_count + 1] = {}; + std::uint64_t more_shape_reasons_[memory_srv_reason_count] = {}; std::uint64_t total_ = 0, reported_ = 0, more_shape_calls_ = 0; RefusedShape shapes_[max_shapes] = {}; unsigned shape_count_ = 0; diff --git a/src/startup_stubs/upload_reservation.h b/src/startup_stubs/upload_reservation.h index 121bbd6cf3..88dbeade3a 100644 --- a/src/startup_stubs/upload_reservation.h +++ b/src/startup_stubs/upload_reservation.h @@ -1,7 +1,9 @@ #pragma once #include +#include #include +#include #include struct ID3D12Resource; @@ -119,23 +121,72 @@ struct UploadReservation { std::uint64_t generation = 0; }; +enum class SnapshotRefusal { + invalid_size, no_unused_capacity, no_device, create_failed, map_failed, bookkeeping_failed, count +}; + +// Protected by the allocator lock. Used bytes are the allocated prefixes of all chunks, +// cleared per chunk when it is recycled. Reservations and refusals are cumulative. +struct SnapshotPoolTotals { + unsigned long long used_bytes = 0, high_water_bytes = 0, reserved_bytes = 0, reservations = 0; + unsigned long long logged_refusals = 0; + std::array(SnapshotRefusal::count)> refusals{}; + + void note_reserved(std::uint64_t rounded, std::uint64_t recycled = 0) + { + used_bytes = used_bytes - recycled + rounded; + if (used_bytes > high_water_bytes) high_water_bytes = used_bytes; + reserved_bytes += rounded; + ++reservations; + } + + bool note_refusal(SnapshotRefusal reason) + { + ++refusals[static_cast(reason)]; + if (reason == SnapshotRefusal::invalid_size || logged_refusals == 8) return false; + ++logged_refusals; + return true; + } + + void report(char* line, std::size_t size, std::size_t chunks) const + { + std::snprintf(line, size, + "snapshot pool: used %llu bytes, high-water %llu bytes, chunks %zu, reserved %llu bytes in %llu reservations, " + "refusals (invalid_size %llu, no_unused_capacity %llu, no_device %llu, create_failed %llu, map_failed %llu, bookkeeping_failed %llu)", + used_bytes, high_water_bytes, chunks, reserved_bytes, reservations, + refused(SnapshotRefusal::invalid_size), refused(SnapshotRefusal::no_unused_capacity), + refused(SnapshotRefusal::no_device), refused(SnapshotRefusal::create_failed), + refused(SnapshotRefusal::map_failed), refused(SnapshotRefusal::bookkeeping_failed)); + } + + unsigned long long refused(SnapshotRefusal reason) const + { + return refusals[static_cast(reason)]; + } +}; + // Caller holds the lock. A successful reservation owns previously unused bytes. -inline UploadReservation reserve_upload_chunk(UploadRingChunk& chunk, std::uint64_t rounded) +inline UploadReservation reserve_upload_chunk(UploadRingChunk& chunk, std::uint64_t rounded, + SnapshotPoolTotals* totals = nullptr) { if (chunk.used > chunk.capacity || rounded > chunk.capacity - chunk.used) return {}; const auto start = chunk.used; chunk.used += rounded; + if (totals) totals->note_reserved(rounded); return {chunk.mapped + start, start, chunk.resource, chunk.generation}; } // Called only after the unused tails and the capacity budget have been tried. template -UploadReservation reuse_upload_chunk(UploadRingChunk& chunk, unsigned index, std::uint64_t rounded, const Queues& queues) +UploadReservation reuse_upload_chunk(UploadRingChunk& chunk, unsigned index, std::uint64_t rounded, const Queues& queues, + SnapshotPoolTotals* totals = nullptr) { if (rounded > chunk.capacity || chunk.recordings) return {}; for (const auto& queue : queues) if (!queue.second.completed(index)) return {}; + const auto recycled = chunk.used; chunk.used = 0; ++chunk.generation; + if (totals) totals->note_reserved(rounded, recycled); return reserve_upload_chunk(chunk, rounded); } diff --git a/tests/cmake/startup_snapshot_pool_totals.cmake b/tests/cmake/startup_snapshot_pool_totals.cmake new file mode 100644 index 0000000000..59e175f4d2 --- /dev/null +++ b/tests/cmake/startup_snapshot_pool_totals.cmake @@ -0,0 +1 @@ +add_test(NAME startup_snapshot_pool_totals COMMAND upload_reservation_check --snapshot-pool-totals) diff --git a/tests/startup_memory_srv_census_check.cpp b/tests/startup_memory_srv_census_check.cpp index e0fdcbfbb9..6e52f350d9 100644 --- a/tests/startup_memory_srv_census_check.cpp +++ b/tests/startup_memory_srv_census_check.cpp @@ -97,7 +97,8 @@ int main() if (w == 4) check("ninth shape is not", !detailed); } auto report = lines(srv, true); - check("report has a summary and a line per kept shape", report.size() == 1 + MemorySrvCensus::max_shapes); + check("report has a summary, a line per kept shape and an overflow breakdown", + report.size() == 2 + MemorySrvCensus::max_shapes); check("summary", !report.empty() && report[0] == "x2w-d3d12 memory SRV refusals (slot 70): total 138; null_destination 1, unreadable_resource 0, unreadable_view 0, " "null_address 1, unsupported_description 133, destination_outside_view_heaps 0, gpu_written_resource 0, " @@ -107,14 +108,28 @@ int main() check("first shape line", report.size() > 1 && report[1] == "x2w-d3d12 memory SRV refused shape 1: reason=unsupported_description dim=3 fmt=71 size=64x64x1 mips=7 layout=0x0 " "flags=0x0 calls=2"); + constexpr char overflow_prefix[] = "x2w-d3d12 memory SRV refusals past the shape table (slot 70):"; + check("first overflow breakdown", !report.empty() && + report.back() == std::string(overflow_prefix) + " unsupported_description=5"); check("unchanged periodic report is skipped", lines(srv, true).empty()); srv.note("unsupported_description", &a); const auto changed = lines(srv, true); - check("periodic report lists only the shapes counted since", changed.size() == 2 && changed[1].find("shape 1:") != std::string::npos); + check("periodic report lists only the shapes counted since", changed.size() == 3 && changed[1].find("shape 1:") != std::string::npos); check("a forced report lists every shape", lines(srv, false).size() == report.size()); check("forced report still prints", lines(srv, false).size() == report.size()); srv.note("null_address", nullptr); - check("forced report does not hide the next change", lines(srv, false).size() == report.size() && lines(srv, true).size() == 1); + check("forced report does not hide the next change", lines(srv, false).size() == report.size() && lines(srv, true).size() == 2); + check("new shape after capacity is not detailed", !srv.note("buffer_unavailable", &c)); + check("repeating an overflowed shape is not detailed", !srv.note("buffer_unavailable", &c)); + check("shape-less refusal with a description is not detailed", !srv.note("null_address", &c)); + const auto overflow = lines(srv, true); + check("overflow-only changes are reported by reason", overflow.size() == 2 && + overflow[0].find("total 143;") != std::string::npos && + overflow[0].find("null_address 3,") != std::string::npos && + overflow[0].find("buffer_unavailable 3,") != std::string::npos && + overflow[0].find("calls past the shape table 7)") != std::string::npos && + overflow[1] == std::string(overflow_prefix) + " unsupported_description=5 buffer_unavailable=2"); + check("unchanged overflow report is skipped", lines(srv, true).empty()); TokenNullCensus tokens; check("first pipeline is detailed", tokens.note(0x1000000000000001ull)); diff --git a/tests/upload_reservation_check.cpp b/tests/upload_reservation_check.cpp index 0eaa3c6661..d6ca48cc50 100644 --- a/tests/upload_reservation_check.cpp +++ b/tests/upload_reservation_check.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -91,6 +92,38 @@ void check_snapshot_pool() check(again.mapped == storage && again.generation == 1, "a finished, unheld snapshot chunk is reused"); } +void check_snapshot_pool_totals() +{ + unsigned char storage[2][4 * kAlign]; + UploadRingChunk chunks[] = {{nullptr, storage[0], sizeof(storage[0])}, {nullptr, storage[1], sizeof(storage[1])}}; + SnapshotPoolTotals totals; + check(reserve_upload_chunk(chunks[0], kAlign, &totals).mapped == storage[0], "first chunk"); + check(reserve_upload_chunk(chunks[0], 3 * kAlign, &totals).mapped == storage[0] + kAlign, "same chunk tail"); + check(reserve_upload_chunk(chunks[1], 2 * kAlign, &totals).mapped == storage[1], "second chunk"); + check(!reserve_upload_chunk(chunks[0], kAlign, &totals).mapped, "full chunk has no tail"); + TestFence fence; + std::map> queues{{0, {&fence, 1}}}; + queues.at(0).submitted(UploadChunkMask{1} << snapshot_chunk_base, true); + check(!reuse_upload_chunk(chunks[0], snapshot_chunk_base, kAlign, queues, &totals).mapped, "in-flight chunk"); + fence.value = 1; + check(reuse_upload_chunk(chunks[0], snapshot_chunk_base, kAlign, queues, &totals).mapped == storage[0], "recycle first chunk"); + + check(!totals.note_refusal(SnapshotRefusal::invalid_size), "invalid size stays silent"); + for (int i = 0; i < 10; ++i) + check(totals.note_refusal(SnapshotRefusal::no_unused_capacity) == (i < 8), "first eight capacity details"); + check(!totals.note_refusal(SnapshotRefusal::no_device), "detail cap shared across causes"); + check(!totals.note_refusal(SnapshotRefusal::create_failed), "create failure after cap"); + check(!totals.note_refusal(SnapshotRefusal::map_failed), "map failure after cap"); + check(!totals.note_refusal(SnapshotRefusal::map_failed), "repeat map failure after cap"); + check(!totals.note_refusal(SnapshotRefusal::bookkeeping_failed), "bookkeeping failure after cap"); + + char line[512]; + totals.report(line, sizeof(line), 2); + check(!std::strcmp(line, "snapshot pool: used 1536 bytes, high-water 3072 bytes, chunks 2, reserved 3584 bytes in 4 reservations, " + "refusals (invalid_size 1, no_unused_capacity 10, no_device 1, create_failed 1, map_failed 2, bookkeeping_failed 1)"), + "summary retains the peak and every refusal, and subtracts the recycled bytes"); +} + // X2W_D3D12_RING_RELEASE_AT_SUBMIT=1 bookkeeping: a closed list stops holding its chunks once its submit is fenced, // the fence alone then guards the chunks, and a later submit of the same list before its Reset fences the intact // chunks again and reports the reused ones. @@ -182,8 +215,12 @@ void check_snapshot_release_at_submit() } // namespace -int main() +int main(int argc, char** argv) { + if (argc == 2 && !std::strcmp(argv[1], "--snapshot-pool-totals")) { + check_snapshot_pool_totals(); + return 0; + } unsigned char storage_a[4 * kAlign]; unsigned char storage_b[8 * kAlign]; std::vector chunks;