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
35 changes: 35 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,41 @@ FALSE without the switch, like `x2w_d3d12_budget_snapshot`. `startup_resource_re
runs the same steps without the switch. The hardware cases report 77 without a
physical adapter, and the debug-layer cases report 77 where the debug layer is not installed.

## D3D12 view allocation failure

A new view record goes into three containers: the record vector, the vector of
materialization flags and the map from content to id. If one of them cannot
allocate, the record goes into none of them, the process continues and the first
eight failures are logged:

`x2w-d3d12 note_view: out of memory in <view_records|view_materialized|view_ids> -> view of slot <n> not recorded, destination descriptor untracked`

The device's view slots return nothing, so the log line is the only report. The
destination descriptor is left untracked: its shadow entry and its token in
title memory are cleared, and the view it named before loses its reference. A
draw then sees a descriptor without a view, not the earlier one. If the desktop
view was created, its resources leave the retire class of
`X2W_D3D12_RELEASE_RETIRED` and are not released early. Earlier ids stay valid,
and a later view is recorded if its allocations succeed. Records already copied
elsewhere are not touched.

This handles the failure; it does not reduce the memory the records use, and it
does not cover other allocations, such as the map of render and depth targets.

`startup_view_alloc_<records|materialized|ids>_<warp|hardware>` fails the
allocation at one site three times through the fixture-only export
`x2w_d3d12_fail_view_allocations`. The failure is injected, not produced by
exhausting memory. For `view_ids` it is raised before the insertion, so the
fixture relies on the standard guarantee that a failed single insertion leaves
an `unordered_map` unchanged. The `control` variants never inject it and must
fail. The failure is exercised on slot 18 and on a slot 19 view with a counter
resource. For slot 19 only the reference counts are checked: resources that
allow unordered access are never in the retire class.

```powershell
ctest --preset dev --no-tests=error -R '^startup_view_alloc_' --output-on-failure
```

## D3D12 view census

The stand-in keeps one record per distinct view: slot, resource, counter,
Expand Down
75 changes: 72 additions & 3 deletions src/startup_stubs/d3d12_capture.inl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ std::size_t view_census_format(char* out, std::size_t capacity, const char* what
return n < 0 ? 0 : (std::min)(static_cast<std::size_t>(n), capacity - 1);
}

// Fixture only (x2w_d3d12_fail_view_allocations, startup_view_alloc): the next `count` allocations at a site fail as an
// exhausted heap would. The vector sites fail only when the vector has to grow; view_ids fails before any insertion,
// because each new entry allocates.
enum ViewAllocation { view_records_site, view_materialized_site, view_ids_site, view_allocation_sites };
volatile LONG view_allocations_to_fail[view_allocation_sites];

void view_allocation_gate(ViewAllocation site)
{
for (LONG n = view_allocations_to_fail[site]; n > 0; n = view_allocations_to_fail[site])
if (InterlockedCompareExchange(&view_allocations_to_fail[site], n - 1, n) == n) throw std::bad_alloc();
}

// The shadow entry for a desktop CPU descriptor handle (note_lock held), or null.
std::uint32_t* shadow_slot(u64 desktop_handle, std::uint32_t* room = nullptr)
{
Expand Down Expand Up @@ -356,6 +368,62 @@ void note_heap(Wrapper* w, UINT type, UINT count, UINT logical_count, UINT incre
static_cast<std::uint32_t*>(VirtualAlloc(nullptr, (u64{count} + 1) * 4, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE))});
ReleaseSRWLockExclusive(&note_lock);
}

// Room for one more element, with the 1.5x growth push_back would use: reserve(size + 1) would copy on every insert.
template<class T>
void reserve_one_view_entry(std::vector<T>& v, ViewAllocation site)
{
if (v.size() < v.capacity()) return;
view_allocation_gate(site);
v.reserve(v.capacity() + v.capacity() / 2 + 1);
}

// Adds a new record to the three view containers, or to none of them (note_lock held). Everything that can allocate
// comes first: the two reservations change no size, and a failed try_emplace leaves the map unchanged. The two
// push_back calls then have room and cannot fail. Returns the name of the container that could not allocate, or null.
const char* store_view(const ViewRecord& record, std::uint8_t created, u64 hash)
{
const char* growing = "view_records";
try {
reserve_one_view_entry(view_records(), view_records_site);
growing = "view_materialized";
reserve_one_view_entry(view_materialized(), view_materialized_site);
growing = "view_ids";
view_allocation_gate(view_ids_site);
view_ids().try_emplace(hash, record.id);
} catch (const std::bad_alloc&) {
return growing;
}
view_records().push_back(record);
view_materialized().push_back(created);
return nullptr;
}

// A new view could not be recorded. The desktop descriptor already holds it, so the destination must stop naming the
// view it held before: the slot and its title token are cleared, and a draw refuses the descriptor instead of binding
// the old view. Nothing records the new view's use of its resource, so the resource leaves the retire class and is not
// released early. Allocates nothing. Enters with note_lock held and releases it.
void drop_unrecorded_view(const ViewRecord& record, bool materialized, const char* container)
{
if (std::uint32_t* slot_ptr = shadow_slot(record.dest_desktop)) {
const std::uint32_t prev = *slot_ptr;
*slot_ptr = 0;
if (prev && prev - 1 < view_records().size() && view_materialized()[prev - 1])
view_refs_release(view_records()[prev - 1].slot, view_records()[prev - 1].resource,
view_records()[prev - 1].counter);
}
if (materialized) view_refs_leave_class(record.slot, record.resource, record.counter, RetireLeft::view);
ReleaseSRWLockExclusive(&note_lock);
const std::size_t token_bytes = record.slot == 22 ? x2w_startup::title_sampler_stride : 32u;
if (record.dest_title != record.dest_desktop && title_backed(record.dest_title, token_bytes) &&
x2w_local::writable(reinterpret_cast<void*>(record.dest_title), token_bytes))
std::memset(reinterpret_cast<void*>(record.dest_title), 0, token_bytes);
static LONG failed;
if (first_calls(failed, 8))
log("x2w-d3d12 note_view: out of memory in %s -> view of slot %u not recorded, destination descriptor untracked",
container, record.slot);
}

// A view is created: remembered by id; the id is also written into title-owned descriptor memory so
// that copies the title makes there itself can be followed.
void note_view(int slot, u64 resource, u64 counter, const void* desc, std::size_t desc_bytes, u64 dest_title, u64 dest_desktop,
Expand Down Expand Up @@ -407,9 +475,10 @@ void note_view(int slot, u64 resource, u64 counter, const void* desc, std::size_
log("%s (new slot %u)", line, record.slot);
}
}
view_records().push_back(record);
view_materialized().push_back(created);
view_ids()[hash] = record.id;
if (const char* container = store_view(record, created, hash)) {
drop_unrecorded_view(record, materialized, container);
return;
}
++views_distinct;
if (census) {
if (record.slot >= 17 && record.slot <= 22) ++view_census.distinct[record.slot - 17];
Expand Down
29 changes: 29 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5753,6 +5753,35 @@ extern "C" __declspec(dllexport) std::size_t x2w_d3d12_view_census_line(char* ou
return view_census_line(out, capacity);
}

// Fixture only (startup_view_alloc): see view_allocation_gate.
extern "C" __declspec(dllexport) void x2w_d3d12_fail_view_allocations(int site, LONG count)
{
if (site >= 0 && site < view_allocation_sites) InterlockedExchange(&view_allocations_to_fail[site], count);
}

// Fixture probe (startup_view_alloc): the sizes of the three view containers, the id + 1 that the tracked slot of a
// title descriptor handle names (0: none), and whether a resource is still in the retire class.
struct ViewStoreProbe {
unsigned long long records, materialized, ids, records_capacity, materialized_capacity;
unsigned shadow, view_refs, retire_when_idle;
};
extern "C" __declspec(dllexport) void x2w_d3d12_view_probe(unsigned long long title_handle, void* resource, ViewStoreProbe* out)
{
*out = {};
out->shadow = shadow_id(desktop_cpu_handle(title_handle));
AcquireSRWLockShared(&note_lock);
out->records = view_records().size();
out->materialized = view_materialized().size();
out->ids = view_ids().size();
out->records_capacity = view_records().capacity();
out->materialized_capacity = view_materialized().capacity();
ReleaseSRWLockShared(&note_lock);
if (Wrapper* r = as_wrapper(resource); r && r->own().kind == resource_kind) {
out->view_refs = static_cast<unsigned>(InterlockedCompareExchange(&r->own().retire_view_refs, 0, 0));
out->retire_when_idle = r->own().retire_when_idle;
}
}

// Fixture-only: the next `count` wrapper allocations fail as an exhausted heap would (#2653).
extern "C" __declspec(dllexport) void x2w_d3d12_fail_wraps(LONG count)
{
Expand Down
25 changes: 25 additions & 0 deletions tests/cmake/startup_view_alloc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# #1199: when a view container cannot allocate, nothing is recorded, the destination slot is untracked and the process
# keeps running. One test per allocation site. `control` never arms the failure, so the fixture must print FAIL.
add_executable(startup_view_alloc tests/startup_view_alloc.cpp)
target_compile_features(startup_view_alloc PRIVATE cxx_std_17)
target_link_libraries(startup_view_alloc PRIVATE d3d12_xs_shim d3d12 dxgi)
foreach(backend IN ITEMS warp hardware)
foreach(site IN ITEMS records materialized ids)
foreach(mode IN ITEMS on control)
if(mode STREQUAL "on")
set(alloc_test startup_view_alloc_${site}_${backend})
add_test(NAME ${alloc_test} COMMAND startup_view_alloc ${backend} ${site})
else()
set(alloc_test startup_view_alloc_${site}_control_${backend})
add_test(NAME ${alloc_test} COMMAND startup_view_alloc ${backend} ${site} control)
endif()
set(alloc_env "X2W_D3D12_BUDGET=1;X2W_D3D12_RELEASE_RETIRED=1;X2W_D3D12_PERMISSIVE=1;X2W_D3D12_RENDER=1")
list(APPEND alloc_env "X2W_D3D12_DEBUG=0" "X2W_TRAP_LOG=${CMAKE_CURRENT_BINARY_DIR}/${alloc_test}.log")
set_tests_properties(${alloc_test} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "${alloc_env}")
if(mode STREQUAL "control")
set_tests_properties(${alloc_test} PROPERTIES
PASS_REGULAR_EXPRESSION "FAIL: a failed publication changed a view container")
endif()
endforeach()
endforeach()
endforeach()
Loading
Loading