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
25 changes: 24 additions & 1 deletion runtime/shims/d3d12_xs_list_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,21 @@ class Pump {
}
}

// A gate that stays shut says why, at most once a minute (report_due): counts and seconds only, no address.
void note_held(leonida::list_value_progress::Hold why, std::uint64_t now_ms) {
namespace lvp = leonida::list_value_progress;
if (gates_.empty()) {
held_report_ms_ = 0; // nothing held: the next hold starts its own minute
return;
}
const std::uint64_t oldest = gates_.front().held_ms;
if (!lvp::report_due(oldest, now_ms, held_report_ms_)) return;
held_report_ms_ = now_ms;
std::fprintf(stderr, "[rt] d3d12_xs: a list value gate has been held %llu s (%zu held): %s\n",
static_cast<unsigned long long>((now_ms - oldest) / 1000), gates_.size(),
lvp::hold_text(why));
}

// The progress rules over what is in flight (src/list_value_progress.h): the index of the gate that goes, noted
// and logged, or gates_.size(). Called with mutex_ held, after a pass that opened nothing.
std::size_t choose_release(bool landed_any) {
Expand All @@ -1169,6 +1184,7 @@ class Pump {
if (activity.releasing || activity.title_submitting) {
last_progress_ms_ = now_ms;
last_progress_at_ = now;
note_held(activity.releasing ? lvp::Hold::releasing : lvp::Hold::submitting, now_ms);
return gates_.size(); // the cheap answer first
}
activity.presents = presents_.load(std::memory_order_relaxed);
Expand All @@ -1193,6 +1209,7 @@ class Pump {
last_progress_ms_ = now_ms;
last_progress_at_ = now;
if (done >= wg.value && wake_) SetEvent(wake_);
note_held(lvp::Hold::write_running, now_ms);
return gates_.size();
}
groups.push_back({wg.queue.get(), wg.after_gate, false,
Expand Down Expand Up @@ -1221,6 +1238,7 @@ class Pump {
last_progress_ms_ = now_ms;
last_progress_at_ = now;
if (done >= g.after && wake_) SetEvent(wake_);
note_held(lvp::Hold::action_running, now_ms);
return gates_.size();
}
}
Expand All @@ -1229,14 +1247,18 @@ class Pump {
g.presents_at_hold, g.held_ms});
}
const lvp::Decision decision = lvp::decide(groups, view, activity, console_value_met);
if (decision.reason == lvp::Reason::hold || decision.gate >= gates_.size()) return gates_.size();
if (decision.reason == lvp::Reason::hold || decision.gate >= gates_.size()) {
note_held(lvp::hold_reason(groups, view, activity), now_ms);
return gates_.size();
}
// Gate ONLY Reason::present on a short high-resolution quiet window after the last observed progress (leaving
// list_value_progress::decide() pure for unit tests) so sub-ms races from deferred CPU Fence::Signal
// callbacks or post-PresentX CPU writes settle before releasing a gate unmet. Using Clock::now() (QPC) instead
// of GetTickCount64() avoids paying a full 15.625 ms Windows system tick per unmet gate.
constexpr auto kPresentQuiet = std::chrono::milliseconds(2);
if (decision.reason == lvp::Reason::present && last_progress_at_ != Clock::time_point{} &&
now - last_progress_at_ < kPresentQuiet) {
note_held(lvp::Hold::settling, now_ms);
return gates_.size();
}
const Reason reason = gates_[decision.gate].queue->written->GetCompletedValue() == UINT64_MAX
Expand Down Expand Up @@ -1526,6 +1548,7 @@ class Pump {
unsigned logged_met_ = 0;
std::uint64_t logged_released_ = 0;
std::uint64_t last_progress_ms_ = 0; // guarded by mutex_: latest tick progress was observed
std::uint64_t held_report_ms_ = 0; // guarded by mutex_: when the held-gate line last printed
Clock::time_point last_progress_at_{}; // guarded by mutex_: high-res QPC progress timestamp
Clock::time_point last_lost_check_{}; // pump thread only (mutex_): throttled device-loss poll timestamp
std::atomic<std::uint64_t> presents_{0}; // the title's PresentX calls so far
Expand Down
49 changes: 49 additions & 0 deletions runtime/src/list_value_progress.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,55 @@ inline bool can_progress(std::span<const PendingWriteGroup> writes, std::span<co
return false;
}

// Diagnostics: a gate held past kHeldReportMs says why, at most once per kHeldReportMs. None of this is read by
// decide(): the release policy above is unchanged.
inline constexpr std::uint64_t kHeldReportMs = 60'000;

// What keeps a held gate shut right now: the condition can_progress() found, the short settle a present release
// waits out (in the shim), or, when nothing can progress, the quiet window that has not opened.
enum class Hold : std::uint8_t {
releasing, // can_progress(): the releaser has gates to signal or is inside a Signal
submitting, // can_progress(): a title ExecuteCommandLists is in progress
write_running, // can_progress(): a write group is running on the GPU
action_running, // can_progress(): an action is running on the GPU
settling, // a present is queued behind it and the last progress has not settled
title_calling, // nothing can progress: the title made a queue call inside kStalledMs
young, // nothing can progress and the title is quiet: no candidate has waited kStalledMs yet
};

inline const char* hold_text(Hold hold) {
switch (hold) {
case Hold::releasing: return "the releaser has gates to signal or is inside a Signal";
case Hold::submitting: return "a title ExecuteCommandLists is in progress";
case Hold::write_running: return "a write group is running on the GPU";
case Hold::action_running: return "an action is running on the GPU";
case Hold::settling: return "a present is queued behind it and the last progress has not settled";
case Hold::title_calling: return "the title is still making queue calls";
default: return "no candidate gate has waited out the quiet window yet";
}
}

// The conditions can_progress() reads, in its order, then the quiet window: why a held gate stayed shut.
inline Hold hold_reason(std::span<const PendingWriteGroup> writes, std::span<const PendingGate> gates,
const Activity& activity) {
if (activity.releasing) return Hold::releasing;
if (activity.title_submitting) return Hold::submitting;
for (const PendingWriteGroup& w : writes)
if (w.running) return Hold::write_running;
for (const PendingGate& g : gates)
if (g.is_action && g.action_running) return Hold::action_running;
if (activity.now_ms < activity.title_call_ms || activity.now_ms - activity.title_call_ms < kStalledMs)
return Hold::title_calling;
return Hold::young;
}

// One line once the oldest gate has stood kHeldReportMs, then at most one per kHeldReportMs (a `last_report_ms` of
// 0 is "nothing said yet").
inline bool report_due(std::uint64_t oldest_ms, std::uint64_t now_ms, std::uint64_t last_report_ms) {
if (!oldest_ms || now_ms < oldest_ms || now_ms - oldest_ms < kHeldReportMs) return false;
return !last_report_ms || (now_ms >= last_report_ms && now_ms - last_report_ms >= kHeldReportMs);
}

// Which held gate goes now without its value, if any. `gates` are the pending gates in the order they were held
// (each queue's in rising value); `met(comparison, value, reference, wide)` is console_value_met.
template <class MetFn>
Expand Down
46 changes: 46 additions & 0 deletions runtime/test/list_value_progress_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <chrono>
#include <cstdio>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

Expand Down Expand Up @@ -554,6 +555,51 @@ int main() {
holds.detach(&holder);
}

// 15. The held-gate line names the condition can_progress() found, then the quiet window. Diagnostics only:
// decide() answers the same as before.
{
const PendingGate gates[] = {gate(qa, 1, wait_x, 0, 0)};
Activity a;
a.now_ms = 100'000;
a.title_call_ms = a.now_ms; // the title called just now
CHECK(hold_reason({}, gates, a) == Hold::title_calling);
CHECK(run({}, gates, a).reason == Reason::hold);
a.releasing = true;
CHECK(hold_reason({}, gates, a) == Hold::releasing);
a.releasing = false;
a.title_submitting = true;
CHECK(hold_reason({}, gates, a) == Hold::submitting);
a.title_submitting = false;
const PendingWriteGroup running[] = {group(qb, 0, write_y, true)};
CHECK(hold_reason(running, gates, a) == Hold::write_running);
PendingGate action = gate(qc, 1, {});
action.is_action = true;
action.action_running = true;
const PendingGate with_action[] = {gates[0], action};
CHECK(hold_reason({}, with_action, a) == Hold::action_running);
// The title quiet past kStalledMs and the gate younger than it: the quiet window keeps it, not a call.
a.title_call_ms = a.now_ms - kStalledMs;
const PendingGate young[] = {gate(qa, 1, wait_x, 0, a.now_ms - 1'000)};
CHECK(hold_reason({}, young, a) == Hold::young);
CHECK(run({}, young, a).reason == Reason::hold);
// Every wording is present and distinct.
for (Hold h : {Hold::releasing, Hold::submitting, Hold::write_running, Hold::action_running, Hold::settling,
Hold::title_calling, Hold::young})
CHECK(hold_text(h) && hold_text(h)[0]);
CHECK(std::string(hold_text(Hold::young)) != hold_text(Hold::settling));
}

// 16. One line once the oldest gate is a minute old, then at most one a minute.
{
CHECK(!report_due(0, 100'000, 0)); // nothing held
CHECK(!report_due(99'000, 100'000, 0)); // a second old
CHECK(!report_due(100'000 - (kHeldReportMs - 1), 100'000, 0)); // one ms short of a minute
CHECK(report_due(100'000 - kHeldReportMs, 100'000, 0)); // exactly a minute
CHECK(!report_due(1, 100'000, 99'999)); // said a moment ago
CHECK(report_due(1, 100'000, 100'000 - kHeldReportMs)); // a minute after the last line
CHECK(!report_due(200'000, 150'000, 0)); // a clock that went back
}

std::printf("list_value_progress_test: all checks passed\n");
return 0;
}
Loading