Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 43 additions & 30 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<u64>(bytes) + 511) & ~u64{511};
const auto note_reserved = [snapshot](u64 reserved) {
if (snapshot) return;
InterlockedAdd64(&upload_ring_reserved_bytes, static_cast<LONG64>(reserved));
InterlockedIncrement(&upload_ring_reservations);
};

auto& chunks = snapshot ? snapshot_chunks : upload_chunks;
static unsigned actives[2] = {0, 0};
Expand All @@ -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<LONG64>(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;
Expand All @@ -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<unsigned char*>(base), chunk_bytes, 0});
total_bytes += chunk_bytes;
if (!snapshot) InterlockedAdd64(&upload_ring_capacity_bytes, static_cast<LONG64>(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<unsigned>(chunks.size() - 1));
} catch (const std::bad_alloc&) {
refusal = SnapshotRefusal::bookkeeping_failed;
created->Unmap(0, nullptr);
if (budget) budget->end_use();
created->Release();
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<unsigned long long>(upload_ring_reserved_bytes) >> 20, upload_ring_reservations, upload_ring_refusals,
static_cast<unsigned long long>(upload_ring_capacity_bytes) >> 20);
static_cast<unsigned long long>(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);
Expand Down
12 changes: 11 additions & 1 deletion src/startup_stubs/memory_srv_census.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class MemorySrvCensus {
++shape_count_;
} else {
++more_shape_calls_;
++more_shape_reasons_[reason];
}
}
ReleaseSRWLockExclusive(&lock_);
Expand All @@ -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<unsigned long long>(total_));
for (int r = 0; r <= memory_srv_reason_count && n > 0 && n < static_cast<int>(sizeof(line)); ++r)
Expand All @@ -154,6 +155,14 @@ class MemorySrvCensus {
static_cast<unsigned long long>(s.calls));
emit(static_cast<const char*>(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<int>(sizeof(line)); ++r)
if (more_shape_reasons_[r])
n += std::snprintf(line + n, sizeof(line) - n, " %s=%llu", memory_srv_reasons[r],
static_cast<unsigned long long>(more_shape_reasons_[r]));
emit(static_cast<const char*>(line));
}
ReleaseSRWLockExclusive(&lock_);
return true;
}
Expand All @@ -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;
Expand Down
55 changes: 53 additions & 2 deletions src/startup_stubs/upload_reservation.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <limits>

struct ID3D12Resource;
Expand Down Expand Up @@ -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<unsigned long long, static_cast<unsigned>(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<unsigned>(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<unsigned>(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 <class Queues>
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);
}
1 change: 1 addition & 0 deletions tests/cmake/startup_snapshot_pool_totals.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_test(NAME startup_snapshot_pool_totals COMMAND upload_reservation_check --snapshot-pool-totals)
21 changes: 18 additions & 3 deletions tests/startup_memory_srv_census_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "
Expand All @@ -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));
Expand Down
Loading
Loading