mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 16:38:38 +00:00
d3d12: a rewritten V# whose range has a current resolved record takes its answer; counters for where V# fixing goes
The heap phase in the 3D opening of one world run: fixing slots 6.4-7.5 ms a submission for about 2,000 changed V# halves; the V# resolution counters there are device-wide (2,904 resolutions a submission against 1,340 changed halves) and say nothing about the heap. A resolved record is fixed again by every release or removal of its range or its copy, so while it stands, a resolution of the same exact range made the same way gives its answer: a rewritten half now takes it from a current record of its range (ConsoleHeapSlotRecords::answer, the resolved records kept by whether they asked for a geometry copy, as the waiting ones), after the changes noted since are applied, as for waiting answers. Nothing else of a resolution is lost: a window's write-back is noted per submission and an unchanged slot is not resolved again either. New per-heap line every log window: halves answered by a resolved record and by a waiting one, halves resolved anew with their time, the classes of those that resolved to nothing (a copy not backed, an arena offset, bit 47, no copy asked, not title memory, past its range, a buffer's, private pages, no room, pool alias, other; geometry_va reports why it gave no copy), the longest refresh with its parts (pages, changes, slots), and the waits for the heap lock, which the heaps phase of the submit counters includes. Test: in the 400 random rounds half the writes take a record's answer, resolved or waiting, each checked against a resolution at that moment (a copy that skips applying the changes noted since fails). Linux ctest 120/120. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -3280,27 +3280,39 @@ struct DeviceState {
|
||||
if (!leonida::texture_memory_readable(reinterpret_cast<std::uintptr_t>(from), size)) return Result::unreadable;
|
||||
return copy_guarded(to, from, size, false) ? Result::copied : Result::interrupted;
|
||||
}
|
||||
// Why geometry_va gave no copy (diagnostics: the classes of V#s that resolve to nothing, fix_vsharp).
|
||||
enum class NoCopy : std::uint8_t {
|
||||
none, no_geometry, not_title, past_range, known_buffer, private_pages, refused, pool_alias, count
|
||||
};
|
||||
// `refused` (when given) is set when a copy was asked for and refused (no room under the cap, or no block): it may
|
||||
// be made once geometry copies are freed (GeometryViews::free_generation).
|
||||
std::uint64_t geometry_va(std::uint64_t guest, std::uint64_t span, bool read_only, bool* refused = nullptr) {
|
||||
if (!geometry.cap() || !span || !guest || !watch_shadow_writes()) return 0;
|
||||
// be made once geometry copies are freed (GeometryViews::free_generation). `why`: the reason for an answer of 0.
|
||||
std::uint64_t geometry_va(std::uint64_t guest, std::uint64_t span, bool read_only, bool* refused = nullptr,
|
||||
NoCopy* why = nullptr) {
|
||||
const auto no = [why](NoCopy reason) {
|
||||
if (why) *why = reason;
|
||||
return std::uint64_t(0);
|
||||
};
|
||||
if (!geometry.cap() || !span || !guest || !watch_shadow_writes()) return no(NoCopy::no_geometry);
|
||||
const auto cpu = leonida::title_gpu_views::to_cpu(guest, span);
|
||||
auto& aliases = leonida::title_va_aliases();
|
||||
const auto alias = aliases.shader_va(cpu, span);
|
||||
if (alias != cpu) return alias;
|
||||
if (cpu == guest) {
|
||||
std::uint64_t lo{}, hi{};
|
||||
if (!aliases.title_memory(guest, lo, hi) || span > hi - guest || context->knows_buffer_va(guest))
|
||||
return 0;
|
||||
if (!aliases.title_memory(guest, lo, hi)) return no(NoCopy::not_title);
|
||||
if (span > hi - guest) return no(NoCopy::past_range);
|
||||
if (context->knows_buffer_va(guest)) return no(NoCopy::known_buffer);
|
||||
if (!read_only && !leonida::title_gpu_views::other_view(cpu)) {
|
||||
MEMORY_BASIC_INFORMATION region{};
|
||||
if (!VirtualQuery(reinterpret_cast<void*>(cpu), ®ion, sizeof region) || region.Type != MEM_MAPPED)
|
||||
return geometry.find(cpu, span);
|
||||
if (!VirtualQuery(reinterpret_cast<void*>(cpu), ®ion, sizeof region) || region.Type != MEM_MAPPED) {
|
||||
const std::uint64_t found = geometry.find(cpu, span);
|
||||
return found ? found : no(NoCopy::private_pages);
|
||||
}
|
||||
}
|
||||
}
|
||||
const std::uint64_t va = geometry.resolve(*this, cpu, span, copy_geometry);
|
||||
if (!va && refused) *refused = true;
|
||||
return va;
|
||||
return va ? va : no(cpu != guest ? NoCopy::pool_alias : NoCopy::refused);
|
||||
}
|
||||
|
||||
// LEONIDA_STREAM_BUFFER_WINDOWS=0: a window may not come from a block outside the arena.
|
||||
@ -4222,7 +4234,7 @@ struct DeviceState {
|
||||
// console heap's slot record is found again when a backing is added there.
|
||||
uint64_t vsharp_address(uint64_t base, uint64_t bytes, leonida::TitleVaAliases::Kind kind = leonida::TitleVaAliases::created_view,
|
||||
bool make_window = true, bool try_geometry = false, uint64_t* refused = nullptr,
|
||||
bool* refused_room = nullptr) {
|
||||
bool* refused_room = nullptr, NoCopy* no_copy = nullptr) {
|
||||
if (refused) *refused = 0;
|
||||
if (refused_room) *refused_room = false;
|
||||
if (!base || !bytes) return 0;
|
||||
@ -4265,7 +4277,7 @@ struct DeviceState {
|
||||
// Read the other way: bit 47 as a sign, for a below-arena buffer's V# the layer wrote itself.
|
||||
if (offset == base && base <= mask && (base & (1ull << 47)) && backed(base | ~mask)) return base | ~mask;
|
||||
if (try_geometry) {
|
||||
to = geometry_va(base, bytes, false, refused_room);
|
||||
to = geometry_va(base, bytes, false, refused_room, no_copy);
|
||||
if (to && encodable(to)) return path(3), to;
|
||||
}
|
||||
uint64_t lo{}, hi{};
|
||||
@ -4327,13 +4339,35 @@ struct DeviceState {
|
||||
uint64_t base = 0, span = 0, target = 0;
|
||||
bool geometry = false; // its resolution asked for a geometry copy
|
||||
};
|
||||
// The classes of the V# halves a console heap resolves to nothing (diagnostics, the heap's log line): what their
|
||||
// base is, as the lookups found it (note_resolution).
|
||||
enum Nothing : unsigned {
|
||||
nothing_refused, // an alias range or window names a copy that is not backed
|
||||
nothing_arena, // an arena offset nothing backs (freed, or never handed out)
|
||||
nothing_negative, // bit 47 set: a below-arena form nothing backs
|
||||
nothing_no_copy, // no geometry copy asked for (no format, a span past the window limit)
|
||||
nothing_not_title, // not the title's memory and not ours: freed or never allocated
|
||||
nothing_past_range, // the title's memory, the span past the end of its range
|
||||
nothing_known_buffer, // the title's memory under a buffer the device has (its V# is the buffer's)
|
||||
nothing_private, // the title's private pages: a heap's V# finds a copy but makes none
|
||||
nothing_room, // a copy refused for room or a block
|
||||
nothing_pool_alias, // the GPU-only view of pool memory whose copy was refused
|
||||
nothing_other, // no geometry copies at all, or the base is 0
|
||||
nothing_classes
|
||||
};
|
||||
static constexpr const char* kNothingNames[nothing_classes] = {
|
||||
"copy not backed", "arena offset", "bit 47", "no copy asked", "not title memory", "past its range",
|
||||
"a buffer's", "private pages", "no room", "pool alias", "other"};
|
||||
struct ConsoleHeap;
|
||||
// `answers` (a console heap whose title-written slot this is): a V# whose range another half of that heap waits
|
||||
// for is answered as that half was (ConsoleHeapSlotRecords::waiting_answer), after the changes made since are
|
||||
// applied (sync_slot_changes), so a waiting answer is never older than a change that concerns it. In the world
|
||||
// 70% of the changed V# halves resolve to nothing, mostly with words seen before, and each such lookup walked the
|
||||
// alias table, the backings, the geometry copies and the title memory (VirtualQuery included) again.
|
||||
VsharpResult fix_vsharp(uint32_t* w, VsharpPath path, VsharpRecord* record = nullptr, ConsoleHeap* answers = nullptr) {
|
||||
// `heap` (a console heap's slot): its counters (resolutions, their time, the classes of V#s that resolve to
|
||||
// nothing). `answers` (a slot the title rewrote): a V# whose exact range another half of that heap has a record
|
||||
// for, made the same way, takes that half's answer (ConsoleHeapSlotRecords::answer) after the changes noted since
|
||||
// are applied (sync_slot_changes): a record is fixed again by every change that could alter its answer, so a
|
||||
// current one gives what resolving again would. In the world 63-70% of the changed V# halves carry words seen
|
||||
// before, and each resolution walked the alias table, the backings, the geometry copies and the title memory
|
||||
// (VirtualQuery included) again.
|
||||
VsharpResult fix_vsharp(uint32_t* w, VsharpPath path, VsharpRecord* record = nullptr, ConsoleHeap* heap = nullptr,
|
||||
bool answers = false) {
|
||||
if ((w[3] >> 30) || !w[2]) return VsharpResult::unchanged; // image, empty slot or raw pointer
|
||||
const uint64_t base = uint64_t(w[0]) | (uint64_t(w[1] & 0xffff) << 32);
|
||||
const uint32_t stride = (w[1] >> 16) & 0x3fffu;
|
||||
@ -4342,15 +4376,25 @@ struct DeviceState {
|
||||
leonida::gfx10::Buffer desc{};
|
||||
const bool copy_geometry = leonida::gfx10::decode(words, desc) &&
|
||||
(path != VsharpPath::scratch || span <= kTitleWindowMax || (desc.format && leonida::gfx10::encode(desc) == words));
|
||||
if (answers && record && answered_by_waiting(*answers, base, span, copy_geometry, *record)) {
|
||||
if (heap && answers && record && answered_by_record(*heap, base, span, copy_geometry, *record)) {
|
||||
if (record->state == leonida::ConsoleHeapSlotRecords::resolved) {
|
||||
const uint64_t to = record->target - arena.address; // mod 2^64, as it was recorded
|
||||
if ((to & ((1ull << 48) - 1)) == base) return VsharpResult::unchanged;
|
||||
w[0] = uint32_t(to);
|
||||
w[1] = (w[1] & 0xffff0000u) | uint32_t((to >> 32) & 0xffff);
|
||||
return VsharpResult::rebased;
|
||||
}
|
||||
note_unbacked_vsharp(base, span, stride, w);
|
||||
w[0] = w[1] = w[2] = 0;
|
||||
return VsharpResult::empty;
|
||||
}
|
||||
uint64_t refused = 0;
|
||||
bool refused_room = false;
|
||||
NoCopy no_copy = NoCopy::none;
|
||||
const auto start = heap ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{};
|
||||
const auto to = vsharp_address(base, span, leonida::TitleVaAliases::table_view,
|
||||
path != VsharpPath::scratch, copy_geometry, &refused, &refused_room);
|
||||
path != VsharpPath::scratch, copy_geometry, &refused, &refused_room, &no_copy);
|
||||
if (heap) note_resolution(*heap, start, base, to, refused, copy_geometry, no_copy);
|
||||
if (record) {
|
||||
// The copy's device address (mod 2^64: a buffer below the arena has a negative offset). A geometry copy
|
||||
// refused for room is asked for again when copies are freed (SlotChange::geometry_room).
|
||||
@ -4744,8 +4788,14 @@ struct DeviceState {
|
||||
std::uint64_t texture_waits = 0, texture_refills = 0;
|
||||
// V# halves answered by a waiting half of the same range (fix_vsharp), and how many of slot_changes this
|
||||
// heap has applied since refresh_console_heaps took the list (sync_slot_changes).
|
||||
std::uint64_t vsharps_answered = 0;
|
||||
std::uint64_t vsharps_answered = 0, vsharps_answered_resolved = 0;
|
||||
std::size_t changes_seen = 0;
|
||||
// Since the last log line: the full resolutions of its V# halves and their time, the classes of those that
|
||||
// resolved to nothing, and the longest refresh with its parts (pages, changes applied, slots fixed).
|
||||
std::uint64_t resolutions = 0;
|
||||
double resolution_ms = 0;
|
||||
std::uint64_t nothing[nothing_classes] = {};
|
||||
double max_ms = 0, max_pages_ms = 0, max_changes_ms = 0, max_fix_ms = 0;
|
||||
// fix_console_slots' budget, for the current refresh, and the slots it nulled since the last log line
|
||||
std::chrono::steady_clock::time_point fix_start{};
|
||||
bool fix_over = false;
|
||||
@ -4872,15 +4922,14 @@ struct DeviceState {
|
||||
// and recorded as waiting: a backing, alias range or title memory added over it fixes the slot again
|
||||
// (apply_slot_changes).
|
||||
vsharp = true;
|
||||
ConsoleHeap* const by = answers ? &heap : nullptr;
|
||||
const VsharpResult result = fix_vsharp(w, path, &halves[0], by);
|
||||
const VsharpResult result = fix_vsharp(w, path, &halves[0], &heap, answers);
|
||||
if (result == VsharpResult::rebased) ++heap.vsharps;
|
||||
// Only the GPU copy changes; the title's bytes (heap.previous) stay as written.
|
||||
if (result == VsharpResult::empty) ++heap.unbacked;
|
||||
// The slot's second V# is a UAV counter (raw, 4 bytes, in the descriptor's upper 16 bytes): a store to it at
|
||||
// heap_base + a raw title address loses the device just as one through the buffer V# does. A counter with
|
||||
// no backing is nulled and waits as the buffer V# does.
|
||||
if (fix_vsharp(w + 4, path, &halves[1], by) == VsharpResult::empty) ++heap.unbacked;
|
||||
if (fix_vsharp(w + 4, path, &halves[1], &heap, answers) == VsharpResult::empty) ++heap.unbacked;
|
||||
} else {
|
||||
leonida::gfx10::ImageWords words;
|
||||
std::memcpy(words.data(), w, sizeof(words));
|
||||
@ -5066,21 +5115,46 @@ struct DeviceState {
|
||||
}
|
||||
apply_slot_changes(heap, slot_change_tail);
|
||||
}
|
||||
// fix_vsharp's answer from a waiting half of the same range in `heap`, current with every change noted so far.
|
||||
bool answered_by_waiting(ConsoleHeap& heap, std::uint64_t base, std::uint64_t span, bool copy_geometry,
|
||||
VsharpRecord& record) {
|
||||
// fix_vsharp's answer from a record of the same range in `heap`, current with every change noted so far.
|
||||
bool answered_by_record(ConsoleHeap& heap, std::uint64_t base, std::uint64_t span, bool copy_geometry,
|
||||
VsharpRecord& record) {
|
||||
sync_slot_changes(heap);
|
||||
leonida::ConsoleHeapSlotRecords::State state{};
|
||||
using Records = leonida::ConsoleHeapSlotRecords;
|
||||
Records::State state{};
|
||||
std::uint64_t target = 0;
|
||||
if (!heap.records.waiting_answer(base, span, copy_geometry, state, target)) return false;
|
||||
if (!heap.records.answer(base, span, copy_geometry, state, target)) return false;
|
||||
// Room for geometry copies is seen once a refresh (refresh_console_heaps): freed since, resolve anew.
|
||||
if (state == leonida::ConsoleHeapSlotRecords::waiting_room && geometry.free_generation() != geometry_room_seen)
|
||||
return false;
|
||||
if (state == Records::waiting_room && geometry.free_generation() != geometry_room_seen) return false;
|
||||
record = {state, base, span, target, copy_geometry};
|
||||
++heap.vsharps_answered;
|
||||
++(state == Records::resolved ? heap.vsharps_answered_resolved : heap.vsharps_answered);
|
||||
return true;
|
||||
}
|
||||
|
||||
// A console heap's full resolution of a V# half (fix_vsharp): counted, timed, and for an answer of nothing, its
|
||||
// class.
|
||||
void note_resolution(ConsoleHeap& heap, std::chrono::steady_clock::time_point start, std::uint64_t base,
|
||||
std::uint64_t to, std::uint64_t refused, bool copy_geometry, NoCopy no_copy) {
|
||||
++heap.resolutions;
|
||||
heap.resolution_ms += std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();
|
||||
if (to) return;
|
||||
Nothing kind = nothing_other;
|
||||
if (refused) kind = nothing_refused;
|
||||
else if (base < arena.size) kind = nothing_arena;
|
||||
else if (base & (1ull << 47)) kind = nothing_negative;
|
||||
else if (!copy_geometry) kind = nothing_no_copy;
|
||||
else
|
||||
switch (no_copy) {
|
||||
case NoCopy::not_title: kind = nothing_not_title; break;
|
||||
case NoCopy::past_range: kind = nothing_past_range; break;
|
||||
case NoCopy::known_buffer: kind = nothing_known_buffer; break;
|
||||
case NoCopy::private_pages: kind = nothing_private; break;
|
||||
case NoCopy::refused: kind = nothing_room; break;
|
||||
case NoCopy::pool_alias: kind = nothing_pool_alias; break;
|
||||
default: break;
|
||||
}
|
||||
++heap.nothing[kind];
|
||||
}
|
||||
|
||||
void note_refused_tsharp(ConsoleHeap& heap, std::size_t offset, const leonida::gfx10::ImageWords& words,
|
||||
const char* why) {
|
||||
unbound_tsharp_tracker().note(heap.console_va, offset, words, why);
|
||||
@ -5111,7 +5185,7 @@ struct DeviceState {
|
||||
heap.fix_count = 0;
|
||||
// Even an unchanged title slot may name a buffer released since the last submission, or wait for a buffer or
|
||||
// texture that came: the slots the changes touch are fixed again, found by their records. First, so the
|
||||
// waiting records the rewritten slots below are answered by are current (answered_by_waiting).
|
||||
// records the rewritten slots below are answered by are current (answered_by_record).
|
||||
if (changes) apply_slot_changes(heap, *changes);
|
||||
const auto t2 = clock::now();
|
||||
// Slots the fix budget deferred at an earlier submission (LEONIDA_CONSOLE_FIX_US; none without it).
|
||||
@ -5122,11 +5196,28 @@ struct DeviceState {
|
||||
heap.pages_ms += ms(t0, t1);
|
||||
heap.retry_ms += ms(t2, t3);
|
||||
heap.fix_ms += ms(t1, t2) + ms(t3, t4);
|
||||
if (ms(t0, t4) > heap.max_ms) {
|
||||
heap.max_ms = ms(t0, t4);
|
||||
heap.max_pages_ms = ms(t0, t1);
|
||||
heap.max_changes_ms = ms(t1, t2);
|
||||
heap.max_fix_ms = ms(t2, t4);
|
||||
}
|
||||
}
|
||||
|
||||
// Before every submission, ahead of the slot-133 shadows.
|
||||
// The time a submission waited for console_heap_mutex (another queue's refresh holds it), since the last log line.
|
||||
double heap_lock_wait_ms = 0, heap_lock_wait_max_ms = 0; // console_heap_mutex
|
||||
std::uint64_t heap_lock_waits = 0;
|
||||
void refresh_console_heaps() {
|
||||
const auto asked = std::chrono::steady_clock::now();
|
||||
std::lock_guard lock(console_heap_mutex);
|
||||
{
|
||||
const double waited =
|
||||
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - asked).count();
|
||||
heap_lock_wait_ms += waited;
|
||||
heap_lock_wait_max_ms = std::max(heap_lock_wait_max_ms, waited);
|
||||
++heap_lock_waits;
|
||||
}
|
||||
const bool heap_pass = heap_gpu && console_heap_gpu_enabled();
|
||||
if (heap_pass) console_heap_gpu_begin();
|
||||
else if (heap_gpu) heap_gpu->drain(context->completed_retire_ticket(), [](int, std::uint32_t, std::uint32_t) {}, [](int) {});
|
||||
@ -5217,6 +5308,33 @@ struct DeviceState {
|
||||
heap->records.textures_waiting(), static_cast<unsigned long long>(heap->texture_waits),
|
||||
static_cast<unsigned long long>(heap->texture_refills),
|
||||
static_cast<unsigned long long>(heap->vsharps_answered));
|
||||
// Where a submission's V# fixing goes: halves answered by a current record of their range (resolved
|
||||
// or waiting) and halves resolved anew with their time; the classes of those that resolved to
|
||||
// nothing; the longest refresh and its parts; the waits for the heap lock (in the heaps phase too).
|
||||
std::string classes;
|
||||
for (unsigned i = 0; i < nothing_classes; ++i)
|
||||
if (heap->nothing[i])
|
||||
classes += (classes.empty() ? "" : ", ") + std::string(kNothingNames[i]) + " " +
|
||||
std::to_string(heap->nothing[i]);
|
||||
std::fprintf(stderr,
|
||||
"[rt] d3d12: console descriptor heap 0x%llx V# halves per submission: %.1f answered by a "
|
||||
"resolved record of their range, %.1f by a waiting one, %.1f resolved anew (%.3f ms, %.2f "
|
||||
"us each); resolved to nothing since the last line: %s; longest refresh %.3f ms (pages "
|
||||
"%.3f, changes %.3f, slots %.3f); heap lock waited %.3f ms on average, %.3f the longest\n",
|
||||
static_cast<unsigned long long>(heap->console_va),
|
||||
double(heap->vsharps_answered_resolved) / count, double(heap->vsharps_answered) / count,
|
||||
double(heap->resolutions) / count, heap->resolution_ms / count,
|
||||
heap->resolutions ? 1000.0 * heap->resolution_ms / double(heap->resolutions) : 0.0,
|
||||
classes.empty() ? "none" : classes.c_str(), heap->max_ms, heap->max_pages_ms,
|
||||
heap->max_changes_ms, heap->max_fix_ms,
|
||||
heap_lock_waits ? heap_lock_wait_ms / double(heap_lock_waits) : 0.0,
|
||||
heap_lock_wait_max_ms);
|
||||
heap_lock_wait_ms = heap_lock_wait_max_ms = 0;
|
||||
heap_lock_waits = 0;
|
||||
heap->vsharps_answered_resolved = heap->resolutions = 0;
|
||||
heap->resolution_ms = 0;
|
||||
for (auto& n : heap->nothing) n = 0;
|
||||
heap->max_ms = heap->max_pages_ms = heap->max_changes_ms = heap->max_fix_ms = 0;
|
||||
for (auto& n : heap->changed) n = 0;
|
||||
heap->texture_waits = heap->texture_refills = heap->vsharps_answered = 0;
|
||||
heap->vsharp_halves = heap->vsharp_repeats = 0;
|
||||
|
||||
@ -109,7 +109,7 @@ public:
|
||||
|
||||
// Half `half` (0: the buffer V#, 1: the UAV counter V#) of `slot` now names [base, base + span) and resolved as
|
||||
// `state` says (none or a V# state), its copy at `target` (0: none; see above). span 0 counts as 1. `geometry`:
|
||||
// its resolution asked for a geometry copy (fix_vsharp's copy_geometry), which waiting_answer tells apart.
|
||||
// its resolution asked for a geometry copy (fix_vsharp's copy_geometry), which answer() tells apart.
|
||||
void set(std::size_t slot, unsigned half, State state, std::uint64_t base, std::uint64_t span,
|
||||
std::uint64_t target, bool geometry = false) {
|
||||
if (slot >= slots_ || half > 1 || is_texture(state)) return;
|
||||
@ -131,18 +131,23 @@ public:
|
||||
place(std::uint32_t(slot * 2), state, 0, address, ranges);
|
||||
}
|
||||
|
||||
// What a V# naming [base, base + span) resolves to while another half of the same range waits: nothing, as for
|
||||
// that half (`state` waiting or waiting_room, `target` its refused copy). Sound because a waiting record is fixed
|
||||
// again by every change that could let it resolve (touched): while one waits, no change since its fix concerns
|
||||
// its range, so the same lookups give the same answer. The caller applies the changes made since first (they may
|
||||
// not be on a record yet). A V# that asks for a geometry copy takes only a half that asked for one too.
|
||||
bool waiting_answer(std::uint64_t base, std::uint64_t span, bool geometry, State& state, std::uint64_t& target) {
|
||||
// What a V# naming [base, base + span) resolves to while another half of the same range has a record made the
|
||||
// same way (`geometry`: both asked for a geometry copy, or neither): the same answer, `state` and `target` as that
|
||||
// half has them. Sound because a record is fixed again by every change that could alter its answer (touched): a
|
||||
// resolved one by every release or removal of its range or its copy, a waiting one also by every addition that
|
||||
// could back it. While it is current, the same lookups give the same answer. The caller applies the changes noted
|
||||
// since first (they may not be on a record yet). A waiting answer is also taken from a half that asked for a copy
|
||||
// when this one does not (it looked further and found nothing).
|
||||
bool answer(std::uint64_t base, std::uint64_t span, bool geometry, State& state, std::uint64_t& target) {
|
||||
if (!span) span = 1;
|
||||
const auto it = indexes_[kSource].by_range.find(range(base, span));
|
||||
if (it == indexes_[kSource].by_range.end()) return false;
|
||||
const Group& g = groups_[it->second];
|
||||
const std::vector<std::uint32_t>* list = !g.keys[2].empty() ? &g.keys[2]
|
||||
: !geometry && !g.keys[1].empty() ? &g.keys[1] : nullptr;
|
||||
const int geo = geometry ? 1 : 0;
|
||||
const std::vector<std::uint32_t>* list =
|
||||
!g.keys[kSettled + geo].empty() ? &g.keys[kSettled + geo]
|
||||
: !g.keys[kWaiting + 1].empty() ? &g.keys[kWaiting + 1]
|
||||
: !geometry && !g.keys[kWaiting].empty() ? &g.keys[kWaiting] : nullptr;
|
||||
if (!list) return false;
|
||||
const Half& h = halves_[list->front()];
|
||||
// One that asks for no copy waits for a backing only, not for geometry room.
|
||||
@ -187,7 +192,8 @@ public:
|
||||
void touched(const SlotChange* changes, std::size_t count, std::vector<std::uint32_t>& out) {
|
||||
out.clear();
|
||||
if (!++epoch_) { // after 2^32 calls: no group may look visited
|
||||
for (auto& g : groups_) g.seen[0] = g.seen[1] = g.seen[2] = 0;
|
||||
for (auto& g : groups_)
|
||||
for (auto& seen : g.seen) seen = 0;
|
||||
epoch_ = 1;
|
||||
}
|
||||
bool room[2] = {};
|
||||
@ -225,6 +231,7 @@ public:
|
||||
private:
|
||||
static constexpr int kSource = 0, kTarget = 1, kTexture = 2;
|
||||
static constexpr std::uint8_t kGeometry = 1; // Half::flags: the resolution asked for a geometry copy
|
||||
static constexpr int kSettled = 0, kWaiting = 2; // Group::keys: + 1 for the records with kGeometry
|
||||
static constexpr std::uint64_t kMaxSpan = (std::uint64_t(1) << 56) - 1;
|
||||
static constexpr unsigned kClasses = 65;
|
||||
|
||||
@ -253,14 +260,16 @@ private:
|
||||
using ByStart = std::multimap<std::uint64_t, std::uint32_t>; // start -> group id
|
||||
struct Group {
|
||||
std::uint64_t start = 0, end = 0;
|
||||
// [0] settled records (resolved, bound), [1] waiting ones, [2] waiting V# halves that asked for a geometry copy
|
||||
std::vector<std::uint32_t> keys[3];
|
||||
// [0] settled records (resolved, bound), [1] settled V# halves that asked for a geometry copy, [2] waiting
|
||||
// records, [3] waiting V# halves that asked for a geometry copy
|
||||
std::vector<std::uint32_t> keys[4];
|
||||
int index = 0;
|
||||
unsigned cls = 0;
|
||||
ByStart::iterator at{}; // in by_start[cls]
|
||||
ByStart::iterator waiting_at{}; // in waiting_by_start, while keys[1] or keys[2] is not empty
|
||||
std::uint32_t seen[3] = {}; // the touched() call that last visited each list (epoch_)
|
||||
bool waits() const { return !keys[1].empty() || !keys[2].empty(); }
|
||||
ByStart::iterator waiting_at{}; // in waiting_by_start, while keys[2] or keys[3] is not empty
|
||||
std::uint32_t seen[4] = {}; // the touched() call that last visited each list (epoch_)
|
||||
bool waits() const { return !keys[kWaiting].empty() || !keys[kWaiting + 1].empty(); }
|
||||
bool empty() const { return keys[kSettled].empty() && keys[kSettled + 1].empty() && !waits(); }
|
||||
};
|
||||
struct Index {
|
||||
std::unordered_map<Range, std::uint32_t, RangeHash> by_range; // -> group id
|
||||
@ -278,7 +287,7 @@ private:
|
||||
return length <= 1 ? 0u : unsigned(64 - std::countl_zero(length - 1));
|
||||
}
|
||||
static int list_of(State state, std::uint8_t flags) {
|
||||
return !is_waiting(state) ? 0 : (flags & kGeometry) ? 2 : 1;
|
||||
return (is_waiting(state) ? kWaiting : kSettled) + ((flags & kGeometry) ? 1 : 0);
|
||||
}
|
||||
// The index group[n] of a record in `state` is in.
|
||||
static int index_of(State state, int n) { return is_texture(state) ? kTexture : n; }
|
||||
@ -371,7 +380,7 @@ private:
|
||||
h.group[n] = it->second + 1;
|
||||
h.place[n] = std::uint32_t(list.size());
|
||||
list.push_back(key);
|
||||
if (list_index && !waited) g.waiting_at = index.waiting_by_start.emplace(g.start, it->second);
|
||||
if (list_index >= kWaiting && !waited) g.waiting_at = index.waiting_by_start.emplace(g.start, it->second);
|
||||
}
|
||||
|
||||
// Takes `key` out of its group[n], from the list of its current state.
|
||||
@ -388,8 +397,8 @@ private:
|
||||
halves_[last].place[n] = at;
|
||||
list.pop_back();
|
||||
h.group[n] = 0;
|
||||
if (which_list && !g.waits()) index.waiting_by_start.erase(g.waiting_at);
|
||||
if (!g.keys[0].empty() || g.waits()) return;
|
||||
if (which_list >= kWaiting && !g.waits()) index.waiting_by_start.erase(g.waiting_at);
|
||||
if (!g.empty()) return;
|
||||
index.by_range.erase(Range{g.start, g.end});
|
||||
index.by_start[g.cls].erase(g.at);
|
||||
if (index.by_start[g.cls].empty()) index.used[g.cls >> 6] &= ~(std::uint64_t(1) << (g.cls & 63));
|
||||
@ -413,8 +422,8 @@ private:
|
||||
for (auto it = index.waiting_by_start.lower_bound(lo); it != index.waiting_by_start.end() && it->first < hi;
|
||||
++it) {
|
||||
++groups_looked_at_;
|
||||
visit(groups_[it->second], 1, out);
|
||||
visit(groups_[it->second], 2, out);
|
||||
visit(groups_[it->second], kWaiting, out);
|
||||
visit(groups_[it->second], kWaiting + 1, out);
|
||||
}
|
||||
}
|
||||
|
||||
@ -432,9 +441,7 @@ private:
|
||||
Group& g = groups_[it->second];
|
||||
++groups_looked_at_;
|
||||
if (g.end <= lo) continue;
|
||||
visit(g, 0, out);
|
||||
visit(g, 1, out);
|
||||
visit(g, 2, out);
|
||||
for (int list = 0; list < 4; ++list) visit(g, list, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,8 +91,8 @@ struct Model {
|
||||
fix(slot);
|
||||
}
|
||||
// As the device fixes a slot the title rewrote: the changes noted so far are applied first (sync_slot_changes),
|
||||
// then a half whose range another half waits for takes its answer (waiting_answer) instead of resolving.
|
||||
std::uint64_t answered = 0, stale_answers = 0;
|
||||
// then a half whose range another half has a record for takes its answer (answer()) instead of resolving.
|
||||
std::uint64_t answered = 0, answered_resolved = 0, stale_answers = 0;
|
||||
void write_answered(std::size_t slot, Words a, Words b = Words{}) {
|
||||
apply();
|
||||
slots[slot] = {a, b};
|
||||
@ -101,13 +101,15 @@ struct Model {
|
||||
const Words& w = slots[slot][half];
|
||||
ConsoleHeapSlotRecords::State state{};
|
||||
std::uint64_t target = 0;
|
||||
if (w.span && records.waiting_answer(w.base, w.span, false, state, target)) {
|
||||
if (w.span && records.answer(w.base, w.span, false, state, target)) {
|
||||
++answered;
|
||||
const bool resolved = state == ConsoleHeapSlotRecords::resolved;
|
||||
answered_resolved += resolved;
|
||||
records.set(slot, half, state, w.base, w.span, target);
|
||||
copy[slot][half] = 0;
|
||||
copy[slot][half] = resolved ? target : 0;
|
||||
// The answer must be what resolving now gives, not only after the next submission's changes.
|
||||
std::uint64_t refused = 0;
|
||||
if (resolve(w.base, w.span, refused)) ++stale_answers;
|
||||
if (resolve(w.base, w.span, refused) != copy[slot][half]) ++stale_answers;
|
||||
continue;
|
||||
}
|
||||
if (!w.span) {
|
||||
@ -360,13 +362,15 @@ int differential() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
EXPECT(m.records.recorded() > 0 && m.answered > 0 && m.stale_answers == 0);
|
||||
EXPECT(m.records.recorded() > 0 && m.answered_resolved > 0 && m.answered > m.answered_resolved &&
|
||||
m.stale_answers == 0);
|
||||
// The model's address space is a few MiB, so most changes touch most of it: this case checks results, not cost.
|
||||
std::printf("differential: 400 rounds, copy equal to a full re-resolution after each; %llu entries looked at, "
|
||||
"%llu slots fixed again, %zu halves recorded at the end; %llu rewritten halves answered by a waiting "
|
||||
"one of their range\n",
|
||||
"%llu slots fixed again, %zu halves recorded at the end; %llu rewritten halves answered by a record of "
|
||||
"their range (%llu resolved), each equal to a resolution at that moment\n",
|
||||
static_cast<unsigned long long>(looked), static_cast<unsigned long long>(touched),
|
||||
m.records.recorded(), static_cast<unsigned long long>(m.answered));
|
||||
m.records.recorded(), static_cast<unsigned long long>(m.answered),
|
||||
static_cast<unsigned long long>(m.answered_resolved));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user