Skip to content
Closed
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
9 changes: 9 additions & 0 deletions docs/d3d12_xs_console_abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ The runtime's model:
behind a gate takes longer: the remap is then deferred to the releaser, which runs it once the queue's write
fence passes the call's point and only then opens a gate the queue's later work waits on, so the GPU sees the
order of the call and the title's thread does not wait (the console's CPU does not either).
- Every 10 s the pump logs its counters (`list values so far`): writes recorded (once per recording), submitted
(each time a list that holds them is executed) and landed, waits, gates and the releaser's work. Recorded but never
submitted is a list the title did not execute, not a write in flight. When three of those lines in a row are the
same while recorded writes have not all landed, the pump logs a stall snapshot once (again only after the counters
moved): each submitted write group still waiting, with the `written` fence value it needs and the value the fence
has reached; each closed gate, with its address, comparison, reference and what memory holds; and from the Vulkan
layer (`src/held_work_report.h`), the CPU waits on fences that are not satisfied and, for each queue that holds
work, what its oldest held operation waits for, both with the highest Signal submitted for that fence. A fence
whose wait has no Signal of that value submitted names what the stall is waiting for. Logging only.

## 6. Root arguments

Expand Down
8 changes: 8 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4602,6 +4602,14 @@ if(TARGET Vulkan::Headers)
target_compile_definitions(clear_color_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
add_test(NAME clear_color COMMAND clear_color_test)
# The stall report of QueueHolds (src/queue_holds.h, src/held_work_report.h); no GPU, no Vulkan.
add_executable(queue_holds_report_test test/queue_holds_report_test.cpp)
find_package(Threads REQUIRED)
target_link_libraries(queue_holds_report_test PRIVATE Threads::Threads)
if(MSVC)
target_compile_options(queue_holds_report_test PRIVATE /W4 /permissive- /utf-8)
endif()
add_test(NAME queue_holds_report COMMAND queue_holds_report_test)
# FramePacer of the Xbox presentation path on a fake clock (src/frame_pacer.h); no GPU, no Vulkan.
add_executable(frame_pacer_test test/frame_pacer_test.cpp)
find_package(Threads REQUIRED)
Expand Down
6 changes: 5 additions & 1 deletion runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3943,6 +3943,7 @@ class ValueSubmission {
group_.clear();
}
if (writes_.empty()) return;
list_values::bump(list_values::counters().writes_submitted, writes_.size());
list_values::QueueValues* values = queue_->values.get();
if (values) {
const std::uint64_t value = ++values->written_value;
Expand Down Expand Up @@ -7000,7 +7001,8 @@ const char* list_tail_slot_name(unsigned slot, unsigned char console_layout) {

// Every counter of list_values::Counters, by name: the stats and their reset stay in step.
#define LEONIDA_LIST_VALUE_COUNTERS(X) \
X(writes_recorded) X(writes_landed) X(writes_failed) X(waits_recorded) X(waits_met_at_submit) \
X(writes_recorded) X(writes_submitted) X(writes_landed) X(writes_failed) X(waits_recorded) \
X(waits_met_at_submit) \
X(waits_relayed) X(gates_held) X(gates_met) X(gates_timed_out) X(cuts) X(cuts_deferred) X(cut_failures) \
X(gates_short) X(demotions) X(late_met) X(bundle_ops) X(releases) X(release_us) X(release_us_max) \
X(pass_us_max) X(release_stalls) X(remaps_now) X(remaps_deferred) X(remaps_failed) X(remap_wait_us_max) \
Expand Down Expand Up @@ -7031,6 +7033,8 @@ void stop_list_values(unsigned ms) { list_values::Pump::get().stop(ms); }

bool list_values_idle(unsigned ms) { return list_values::Pump::get().idle(ms); }

void list_values_snapshot(std::FILE* out) { list_values::Pump::get().snapshot(out); }

unsigned list_proxy_part_count(const void* list) {
if (!is_list_proxy_impl(list)) return 0;
return static_cast<unsigned>(static_cast<const ListProxy*>(list)->parts.size()) + 1;
Expand Down
6 changes: 5 additions & 1 deletion runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@

#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -473,7 +474,7 @@ inline constexpr bool console_value_met(std::uint32_t comparison, std::uint64_t
}
}
struct ListValueStats {
std::uint64_t writes_recorded, writes_landed, writes_failed;
std::uint64_t writes_recorded, writes_submitted, writes_landed, writes_failed; // submitted: per execution
std::uint64_t waits_recorded, waits_met_at_submit, waits_relayed; // relayed: a GPU wait on a write in flight
std::uint64_t gates_held, gates_met, gates_timed_out; // gate: opened from the CPU
std::uint64_t cuts, cuts_deferred, cut_failures; // a list split at a wait after work
Expand All @@ -496,6 +497,9 @@ void set_list_values_enabled(bool on);
void stop_list_values(unsigned ms);
// Test helper: true once every write landed and every gate opened, waiting up to `ms`.
bool list_values_idle(unsigned ms);
// The stall snapshot the pump logs once its counters stood still with writes not landed (unlanded write groups,
// open gates, the D3D12 layer's held work), written to `out` now. Test helper and debugger aid.
void list_values_snapshot(std::FILE* out);
// Test helper: the real lists a list proxy's recording is in (1 + its cuts), 0 for anything else.
unsigned list_proxy_part_count(const void* list);

Expand Down
144 changes: 138 additions & 6 deletions runtime/shims/d3d12_xs_list_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
// a few milliseconds, not the full timeout, per such wait. A wait cut short is watched for the rest of the full
// timeout: met in that time, its address keeps the full timeout from then on. A gate cut short that is met within
// its short timeout gives every wait the full timeout again.
// Stall snapshot: when the 10 s summary has not changed three times in a row while writes are still waiting to land,
// the pump says once which write groups wait for which queue's `written` fence (and how far that fence got), which
// gates are closed on what memory, and what the D3D12 layer holds and why (QueueHolds, src/held_work_report.h).
// Logging only.
// LEONIDA_LIST_VALUES=0 drops both methods (the behaviour before this file).

#include <windows.h>
Expand All @@ -55,6 +59,7 @@
#include <cstring>
#include <deque>
#include <functional>
#include <iterator>
#include <memory>
#include <mutex>
#include <new>
Expand All @@ -63,6 +68,7 @@
#include <vector>

#include "d3d12_xs_device.h" // console_value_met, ListValueStats
#include "../src/held_work_report.h" // leonida::report_held_work: the stall snapshot
#include "../src/list_value_wait_budget.h" // leonida::list_value_wait: work-aware timeout clock & producer graph
#include "../src/title_page_watch.h" // leonida::kernel_writes::store
#include "../src/value_write_sink.h" // leonida::value_sink: the arena and xmem's PAGE_NOACCESS pages
Expand Down Expand Up @@ -161,7 +167,8 @@ struct Write {
};

struct Counters {
std::atomic<std::uint64_t> writes_recorded{0}, writes_landed{0}, writes_failed{0};
// Recorded into a list (once per recording), submitted (each time a list that holds them is executed), landed.
std::atomic<std::uint64_t> writes_recorded{0}, writes_submitted{0}, writes_landed{0}, writes_failed{0};
std::atomic<std::uint64_t> waits_recorded{0}, waits_met_at_submit{0}, waits_relayed{0};
std::atomic<std::uint64_t> gates_held{0}, gates_met{0}, gates_timed_out{0};
std::atomic<std::uint64_t> cuts{0}, cuts_deferred{0}, cut_failures{0};
Expand Down Expand Up @@ -502,6 +509,8 @@ class Pump {
static constexpr std::size_t kMaxStreaks = 256; // addresses whose timeouts are counted (least recently used go)
static constexpr std::size_t kMaxLate = 64; // waits cut short watched for the rest of the full timeout
static constexpr std::uint64_t kStallMs = 1000; // a gate Signal this long is reported (watch_releaser)
static constexpr unsigned kStillSummaries = 3; // 10 s summaries alike before the stall snapshot
static constexpr std::size_t kSnapshotItems = 8; // write groups and gates the snapshot lists

struct WriteGroup {
std::shared_ptr<QueueValues> queue;
Expand Down Expand Up @@ -592,7 +601,9 @@ class Pump {
// Writes wake us through SetEventOnCompletion; the timeout is only a safety net (and, while the
// releaser is inside a Signal, how often watch_releaser looks at it).
const bool releasing = signal_since_ms_.load(std::memory_order_relaxed) != 0;
WaitForSingleObject(wake_, polling ? kPollMs : writes ? 50 : releasing ? 500 : INFINITE);
// With nothing in flight it still looks every 10 s, so the summary and the stall snapshot are said
// when the title stopped submitting altogether.
WaitForSingleObject(wake_, polling ? kPollMs : writes ? 50 : releasing ? 500 : 10000);
}
if (stop_.load(std::memory_order_relaxed)) break;
pass(false);
Expand Down Expand Up @@ -886,7 +897,9 @@ class Pump {
busy_ = false;
}

// One line every 10 s while there is traffic, for boot logs.
// One line every 10 s once list values are in use, for boot logs. When three in a row are the same while recorded
// writes have not all landed, the stall snapshot follows once (snapshot()), and again only after the counters
// moved.
void summarize() {
const auto now = Clock::now();
if (now < next_summary_) return;
Expand All @@ -895,14 +908,31 @@ class Pump {
const auto get = [](const std::atomic<std::uint64_t>& v) {
return static_cast<unsigned long long>(v.load(std::memory_order_relaxed));
};
const std::uint64_t moved[] = {get(c.writes_recorded), get(c.writes_submitted), get(c.writes_landed),
get(c.writes_failed), get(c.waits_recorded), get(c.gates_held),
get(c.gates_met), get(c.gates_timed_out), get(c.releases)};
const bool unlanded = moved[0] > moved[2] + moved[3]; // recorded, not landed (or never submitted)
if (std::equal(std::begin(moved), std::end(moved), std::begin(last_moved_)) && unlanded) {
if (++still_summaries_ >= kStillSummaries && !snapshot_logged_) {
snapshot_logged_ = true;
snapshot(stderr, still_summaries_ * 10);
}
} else {
still_summaries_ = 0;
snapshot_logged_ = false;
std::copy(std::begin(moved), std::end(moved), std::begin(last_moved_));
}
std::fprintf(stderr,
"[rt] d3d12_xs: list values so far: %llu writes recorded, %llu landed, %llu failed; %llu waits "
"recorded, %llu met at submission, %llu relayed; %llu gates held, %llu met, %llu timed out "
"[rt] d3d12_xs: list values so far: %llu writes recorded (once per recording), %llu submitted "
"(once per execution: recorded but never submitted is a list the title did not execute), %llu "
"landed, %llu failed; %llu waits recorded, %llu met at submission, %llu relayed; %llu gates held, %llu met, "
"%llu timed out "
"(%llu short, %llu demotions, %llu met late); %llu cuts (%llu deferred to a query's end, %llu "
"failed), %llu on bundles; releaser %llu signals, %.1f ms, longest %.2f ms, %llu stalls; longest pump pass "
"%.2f ms; %llu page remaps at the call (longest wait %.2f ms), %llu deferred (%llu failed); "
"timestamps %llu from the GPU, %llu from the CPU clock\n",
get(c.writes_recorded), get(c.writes_landed), get(c.writes_failed), get(c.waits_recorded),
get(c.writes_recorded), get(c.writes_submitted), get(c.writes_landed), get(c.writes_failed),
get(c.waits_recorded),
get(c.waits_met_at_submit), get(c.waits_relayed), get(c.gates_held), get(c.gates_met),
get(c.gates_timed_out), get(c.gates_short), get(c.demotions), get(c.late_met), get(c.cuts),
get(c.cuts_deferred), get(c.cut_failures), get(c.bundle_ops), get(c.releases),
Expand All @@ -912,6 +942,104 @@ class Pump {
get(c.remaps_failed), get(c.stamps_gpu), get(c.stamps_cpu));
}

public:
// The stall snapshot: what the writes still waiting to land and the gates still closed wait for, and what the
// D3D12 layer holds (leonida::report_held_work). Said when the counters stood still for `still_s` seconds (0: on
// request, the test). A write group lands once its queue's `written` fence reaches its value: a completed value
// below it means the queue has not run that far, so the queue's held work (the lines after) or its GPU work is
// what to look at. The fences are read with no lock held.
void snapshot(std::FILE* out, unsigned still_s = 0) {
struct GroupView {
std::shared_ptr<QueueValues> queue;
std::uint64_t value;
std::size_t writes;
std::uint64_t first_address;
};
struct GateView {
std::shared_ptr<QueueValues> queue;
std::uint64_t value;
bool started, action;
std::uint64_t after;
double open_s;
std::vector<Wait> waits;
};
std::vector<GroupView> groups;
std::vector<GateView> gates;
std::size_t group_count = 0, gate_count = 0;
const auto now = Clock::now();
{
std::lock_guard lock(mutex_);
group_count = writes_.size();
gate_count = gates_.size();
for (const WriteGroup& group : writes_) {
if (groups.size() == kSnapshotItems) break;
groups.push_back({group.queue, group.value, group.writes.size(),
group.writes.empty() ? 0 : group.writes.front().address});
}
for (const Gate& gate : gates_) {
if (gates.size() == kSnapshotItems) break;
gates.push_back({gate.queue, gate.value, gate.started, static_cast<bool>(gate.action), gate.after,
gate.started ? std::chrono::duration<double>(now - gate.since).count() : 0.0,
gate.waits});
}
}
const Counters& c = counters();
const unsigned long long recorded = c.writes_recorded.load(std::memory_order_relaxed),
submitted = c.writes_submitted.load(std::memory_order_relaxed),
landed = c.writes_landed.load(std::memory_order_relaxed),
failed = c.writes_failed.load(std::memory_order_relaxed);
if (still_s)
std::fprintf(out, "[rt] d3d12_xs: stall snapshot: the list-value counters have not moved for %u s (writes "
"%llu recorded, %llu submitted, %llu landed, %llu failed: recorded but never submitted "
"is a list the title did not execute); %zu write group(s) and %zu gate(s) are "
"waiting:\n",
still_s, recorded, submitted, landed, failed, group_count, gate_count);
else
std::fprintf(out, "[rt] d3d12_xs: stall snapshot: %zu write group(s) and %zu gate(s) are waiting:\n",
group_count, gate_count);
for (const GroupView& group : groups) {
const unsigned long long completed =
group.queue && group.queue->written ? group.queue->written->GetCompletedValue() : 0;
std::fprintf(out, "[rt] d3d12_xs: %zu write(s) (first at 0x%llx) on queue values %p land at its written "
"fence %llu, completed %llu%s\n",
group.writes, static_cast<unsigned long long>(group.first_address),
static_cast<const void*>(group.queue.get()), static_cast<unsigned long long>(group.value),
completed,
completed >= group.value ? " (reached: the pump has not landed them yet)"
: " (the queue has not run that far)");
}
for (const GateView& gate : gates) {
const unsigned long long gate_done =
gate.queue && gate.queue->gate ? gate.queue->gate->GetCompletedValue() : 0;
if (gate.action) {
std::fprintf(out, "[rt] d3d12_xs: an action gate %llu on queue values %p runs behind its written "
"fence %llu (gate fence completed %llu)\n",
static_cast<unsigned long long>(gate.value), static_cast<const void*>(gate.queue.get()),
static_cast<unsigned long long>(gate.after), gate_done);
continue;
}
char state[64] = "behind an earlier gate of its queue";
if (gate.started) std::snprintf(state, sizeof(state), "its time has run for %.1f s", gate.open_s);
std::fprintf(out, "[rt] d3d12_xs: gate %llu on queue values %p (gate fence completed %llu), %s:\n",
static_cast<unsigned long long>(gate.value), static_cast<const void*>(gate.queue.get()),
gate_done, state);
for (const Wait& wait : gate.waits) {
std::uint64_t memory = 0;
const bool readable = read_value(wait.address, wait.wide, memory);
std::fprintf(out,
"[rt] d3d12_xs: wait%u for 0x%llx, comparison %u against 0x%llx; memory %s0x%llx\n",
wait.wide ? 64u : 32u, static_cast<unsigned long long>(wait.address), wait.comparison,
static_cast<unsigned long long>(wait.reference), readable ? "" : "unreadable, ",
static_cast<unsigned long long>(memory));
}
}
if (group_count > groups.size() || gate_count > gates.size())
std::fprintf(out, "[rt] d3d12_xs: (the first %zu of each are shown)\n", kSnapshotItems);
leonida::report_held_work(out);
std::fflush(out);
}

private:
// A timestamp write's value: the GPU's timestamp for the write's place in its list when the GPU wrote one, else
// the CPU's clock now. Flag 1 is in the queue's GetTimestampFrequency ticks; flag 2 (the system timestamp) is a
// QueryPerformanceCounter value, the GPU's timestamp turned into one through the queue's clock calibration (never
Expand Down Expand Up @@ -1137,6 +1265,10 @@ class Pump {
bool busy_ = false; // a pass is landing or handing over what it took off the lists
unsigned logged_met_ = 0;
Clock::time_point next_summary_ = Clock::now() + std::chrono::seconds(10); // the pump thread's only
// summarize()'s: the counters at the last change, 10 s summaries alike since, the snapshot said for this stall.
std::uint64_t last_moved_[9] = {};
unsigned still_summaries_ = 0;
bool snapshot_logged_ = false;
std::mutex release_mutex_; // releases_ and releasing_
std::condition_variable release_ready_;
std::deque<Release> releases_;
Expand Down
Loading
Loading