perf(agc): skip unchanged heap mirror runs and idle mirror sweeps in address-based builds

A build served by the cached address space walked every region of the
space for its mirrors, sorted the heap mirrors, compared each heap
mirror's blocks with the write tracker under its own lock and swept the
whole mirror registry. In an offline replay with 37,000 heap mirrors
that took about 10 ms per build, every build.

The space now keeps its writable mirrors, its sorted heap mirrors and
their adjacent runs from when it is published. Each run is still
flushed and collected; a run with no block stamped since the generation
its last complete check collected skips the per-mirror compare, and that
generation is stored only once the build's blocks were copied. The
registry sweep is skipped while every live mirror's range is held by the
cached address space's lease and no mirror was added, replaced or
dropped since.
This commit is contained in:
Celegans12
2026-10-08 00:33:14 +02:00
parent d70b899894
commit e804ce85f7
3 changed files with 111 additions and 24 deletions
@@ -150,6 +150,8 @@ struct MirrorStats {
std::uint64_t rebuilds = 0;
std::uint64_t blocksCopied = 0;
std::uint64_t heapRefills = 0;
std::uint64_t sweeps = 0;
std::uint64_t heapChecks = 0;
};
MirrorStats MirrorCounters();
void ClearImageMirrors(VkDevice device);
@@ -83,6 +83,17 @@ struct GuestBufferMemory::AddressSpace {
std::uint64_t serial = 0;
GuestAllocations::Lease lease;
std::vector<Region> base;
struct HeapRun {
std::uint64_t begin;
std::uint64_t bytes;
std::size_t first;
std::size_t last;
};
bool mirrored = false;
std::vector<std::size_t> writableMirrors;
std::vector<ImageMirror*> heapMirrors;
std::vector<HeapRun> heapRuns;
std::unique_ptr<std::atomic<std::uint64_t>[]> verified;
std::vector<CopiedRange> copied;
// The BDA table entries of `base`, in its order.
std::vector<ShaderRecompiler::BdaAbi::Range> ranges;
@@ -544,6 +555,11 @@ struct ImageMirrors {
// Refreshes that had to wait for recorded work writing the range.
std::uint64_t syncs = 0;
std::uint64_t serials = 0;
std::uint64_t version = 0;
std::uint64_t pinnedSpace = 0;
std::uint64_t pinnedVersion = 0;
std::uint64_t sweeps = 0;
std::uint64_t heapChecks = 0;
std::chrono::steady_clock::time_point lastReport = std::chrono::steady_clock::now();
};
@@ -778,11 +794,30 @@ void refreshMirror(ImageMirror& mirror, std::uint64_t address, std::uint64_t byt
compareBlocks(blocks);
}
void refreshHeapMirrors(std::vector<ImageMirror*>& mirrors, std::vector<RefreshBlock>& blocks) {
void appendChangedBlocks(std::span<ImageMirror* const> mirrors, std::uint64_t generation, std::vector<RefreshBlock>& blocks) {
constexpr std::uint64_t block = 65536;
std::sort(mirrors.begin(), mirrors.end(), [](const ImageMirror* left, const ImageMirror* right) { return left->base < right->base; });
thread_local std::vector<std::uint8_t>* changedSlot = nullptr;
auto& changed = ShaderRecompiler::ThreadOwned(changedSlot);
Mirrors().heapChecks += mirrors.size();
for (auto* const pointer : mirrors) {
auto& mirror = *pointer;
changed.assign(mirror.generations.size(), 0);
GuestMemory::ChangedBlocks(mirror.base, static_cast<std::size_t>(mirror.bytes), mirror.generations, changed);
const auto aligned = mirror.base / block * block;
const auto end = mirror.base + mirror.bytes;
bool refilled = false;
for (std::size_t at = 0; at < changed.size(); ++at) {
if (changed[at] == 0) continue;
const auto from = std::max(mirror.base, aligned + at * block);
blocks.push_back({&mirror, from, static_cast<std::size_t>(std::min(end, aligned + (at + 1) * block) - from), generation});
refilled = true;
}
if (refilled) ++Mirrors().heapRefills;
}
}
void refreshHeapMirrors(std::vector<ImageMirror*>& mirrors, std::vector<RefreshBlock>& blocks) {
std::sort(mirrors.begin(), mirrors.end(), [](const ImageMirror* left, const ImageMirror* right) { return left->base < right->base; });
for (std::size_t first = 0; first < mirrors.size();) {
auto last = first + 1;
while (last < mirrors.size() && mirrors[last]->base == mirrors[last - 1]->base + mirrors[last - 1]->bytes) ++last;
@@ -791,25 +826,45 @@ void refreshHeapMirrors(std::vector<ImageMirror*>& mirrors, std::vector<RefreshB
prepareRange(begin, bytes);
const auto generation = GuestMemory::CollectWrites(begin, static_cast<std::size_t>(bytes));
Require(generation != 0, "a heap mirror's range is no longer write-watched");
for (auto index = first; index < last; ++index) {
auto& mirror = *mirrors[index];
changed.assign(mirror.generations.size(), 0);
GuestMemory::ChangedBlocks(mirror.base, static_cast<std::size_t>(mirror.bytes), mirror.generations, changed);
const auto aligned = mirror.base / block * block;
const auto end = mirror.base + mirror.bytes;
bool refilled = false;
for (std::size_t at = 0; at < changed.size(); ++at) {
if (changed[at] == 0) continue;
const auto from = std::max(mirror.base, aligned + at * block);
blocks.push_back({&mirror, from, static_cast<std::size_t>(std::min(end, aligned + (at + 1) * block) - from), generation});
refilled = true;
}
if (refilled) ++Mirrors().heapRefills;
}
appendChangedBlocks(std::span<ImageMirror* const>(mirrors).subspan(first, last - first), generation, blocks);
first = last;
}
}
void planSpaceMirrors(GuestBufferMemory::AddressSpace& space) {
for (std::size_t index = 0; index < space.base.size(); ++index) {
const auto& region = space.base[index];
if (region.mirror == nullptr) continue;
space.mirrored = true;
if (!region.mirror->heap) {
if (region.mirror->writable) space.writableMirrors.push_back(index);
continue;
}
auto* mirror = region.mirror.get();
if (space.heapRuns.empty() || mirror->base != space.heapMirrors.back()->base + space.heapMirrors.back()->bytes) space.heapRuns.push_back({mirror->base, 0, space.heapMirrors.size(), 0});
space.heapMirrors.push_back(mirror);
auto& run = space.heapRuns.back();
run.bytes = mirror->base + mirror->bytes - run.begin;
run.last = space.heapMirrors.size();
}
space.verified = std::make_unique<std::atomic<std::uint64_t>[]>(space.heapRuns.size());
}
std::vector<std::pair<std::size_t, std::uint64_t>> refreshHeapRuns(const GuestBufferMemory::AddressSpace& space, std::vector<RefreshBlock>& blocks) {
std::vector<std::pair<std::size_t, std::uint64_t>> checked;
checked.reserve(space.heapRuns.size());
for (std::size_t index = 0; index < space.heapRuns.size(); ++index) {
const auto& run = space.heapRuns[index];
prepareRange(run.begin, run.bytes);
const auto generation = GuestMemory::CollectWrites(run.begin, static_cast<std::size_t>(run.bytes));
Require(generation != 0, "a heap mirror's range is no longer write-watched");
const auto since = space.verified[index].load(std::memory_order_relaxed);
if (since == 0 || !GuestMemory::UnchangedSince(run.begin, static_cast<std::size_t>(run.bytes), since)) appendChangedBlocks(std::span<ImageMirror* const>(space.heapMirrors).subspan(run.first, run.last - run.first), generation, blocks);
checked.emplace_back(index, generation);
}
return checked;
}
// The mirror for a leased image range: the existing one, or a new one when none exists or the
// registered Range object changed (a protection change). Null when the range cannot be mirrored. An
// existing writable mirror's blocks are appended to `blocks` for the caller's one compare of every
@@ -823,6 +878,7 @@ std::shared_ptr<ImageMirror> acquireMirror(const Context& context, const std::sh
state.failed.clear();
state.heapBytes = 0;
state.device = context.device;
++state.version;
}
if (state.failed.contains(range->address)) return nullptr;
const auto found = state.entries.find(range->address);
@@ -876,6 +932,7 @@ std::shared_ptr<ImageMirror> acquireMirror(const Context& context, const std::sh
if (const auto found = state.entries.find(range->address); found != state.entries.end() && found->second->heap) state.heapBytes -= found->second->bytes;
if (heap) state.heapBytes += mirror->bytes;
state.entries[range->address] = mirror;
++state.version;
return mirror;
}
@@ -893,16 +950,28 @@ std::shared_ptr<ImageMirror> findMirror(const Context& context, std::uint64_t be
// Drops mirrors whose registered range is gone (a later build made a new one for the replacement).
void sweepMirrors() {
const auto space = Spaces().current.load();
auto& state = Mirrors();
std::lock_guard lock(state.mutex);
if (space != nullptr && state.pinnedSpace == space->serial && state.pinnedVersion == state.version) return;
++state.sweeps;
bool pinned = space != nullptr;
std::size_t leased = 0;
for (auto it = state.entries.begin(); it != state.entries.end();) {
if (!it->second->range.expired()) {
if (pinned) {
while (leased < space->lease.size() && space->lease[leased]->address < it->first) ++leased;
pinned = leased < space->lease.size() && sameRange(it->second->range, space->lease[leased]);
}
++it;
continue;
}
if (it->second->heap) state.heapBytes -= it->second->bytes;
it = state.entries.erase(it);
++state.version;
}
state.pinnedSpace = pinned ? space->serial : 0;
state.pinnedVersion = state.version;
}
void reportMirrors() {
@@ -1105,7 +1174,7 @@ std::string AddressCopyOverflow(std::vector<AddressCopy> copies, std::uint64_t l
MirrorStats MirrorCounters() {
auto& state = Mirrors();
std::lock_guard lock(state.mutex);
MirrorStats stats{0, state.heapBytes, state.rebuilds, state.blocksCopied, state.heapRefills};
MirrorStats stats{0, state.heapBytes, state.rebuilds, state.blocksCopied, state.heapRefills, state.sweeps, state.heapChecks};
for (const auto& [base, mirror] : state.entries) stats.heapMirrors += mirror->heap ? 1 : 0;
return stats;
}
@@ -1394,6 +1463,7 @@ void GuestBufferMemory::AcquireRegistered() {
std::vector<RefreshBlock> blocks;
std::vector<ImageMirror*> heaps;
bool mirrored = false;
bool served = false;
auto& spaces = Spaces();
// Read before the lease below is acquired: a mutation ending in between leaves the space
// stale by its generation, never newer than it (see AddressSpace).
@@ -1421,11 +1491,11 @@ void GuestBufferMemory::AcquireRegistered() {
// an existing mirror. The preparation may wait for recorded work, whose completions can
// retire an import the space's regions point at: UploadFinish re-checks the epoch
// before any of them is dereferenced.
for (const auto& region : space->base) {
if (region.mirror == nullptr) continue;
mirrored = true;
if (region.mirror->heap) heaps.push_back(region.mirror.get());
else if (region.mirror->writable && prepareRefresh(*region.mirror, region.begin, region.end - region.begin)) appendBlocks(blocks, *region.mirror, region.begin, region.end - region.begin);
served = true;
mirrored = space->mirrored;
for (const auto index : space->writableMirrors) {
const auto& region = space->base[index];
if (prepareRefresh(*region.mirror, region.begin, region.end - region.begin)) appendBlocks(blocks, *region.mirror, region.begin, region.end - region.begin);
}
lap(timing.mirrorsUs, at);
}
@@ -1537,16 +1607,20 @@ void GuestBufferMemory::AcquireRegistered() {
}
regions = std::move(extras);
lease.clear();
planSpaceMirrors(*built);
space = std::move(built);
spaces.droppedByWaiter.store(false, std::memory_order_relaxed);
spaces.current.store(space);
}
}
}
refreshHeapMirrors(heaps, blocks);
std::vector<std::pair<std::size_t, std::uint64_t>> checked;
if (served) checked = refreshHeapRuns(*space, blocks);
else refreshHeapMirrors(heaps, blocks);
lap(timing.mirrorsUs, at);
const auto copiedBefore = Mirrors().blocksCopied;
compareBlocks(blocks);
for (const auto& [run, collected] : checked) space->verified[run].store(collected, std::memory_order_relaxed);
lap(timing.compareUs, at);
if (profile) {
timing.blocksCompared += blocks.size();
@@ -102,6 +102,10 @@ void heapMirrorTests(const Context& context, const BdaTestAccess& access) {
guest[65536 + 7] = 0x44;
Require(build(0, {}) == first && MirrorCounters().blocksCopied == one.blocksCopied + 2, "two written heap blocks were not read again");
Require(access.addressBytes(first)[5] == std::byte{0x33} && access.addressBytes(first)[65536 + 7] == std::byte{0x44}, "the heap mirror missed the CPU writes");
const auto quiet = MirrorCounters();
Require(build(0, {}) == first && MirrorCounters().heapChecks == quiet.heapChecks && MirrorCounters().sweeps == quiet.sweeps && MirrorCounters().blocksCopied == quiet.blocksCopied, "an unchanged heap range was compared block by block or the mirrors were swept again");
guest[65536 + 9] = 0x66;
Require(build(0, {}) == first && MirrorCounters().heapChecks > quiet.heapChecks && MirrorCounters().blocksCopied == quiet.blocksCopied + 1 && access.addressBytes(first)[65536 + 9] == std::byte{0x66}, "a CPU write after an unchanged build was missed");
registry(false, false);
sweep();
const auto swept = MirrorCounters();
@@ -116,6 +120,13 @@ void heapMirrorTests(const Context& context, const BdaTestAccess& access) {
});
Require(guest[16] == 0x77 && guest[16 + 8] == 0x55, "a writable heap mirror's write-back lost the GPU's store or rolled back the CPU's");
Require(build(0, {}) == device && access.addressBytes(device)[16] == std::byte{0x77} && access.addressBytes(device)[16 + 8] == std::byte{0x55}, "the writable heap mirror missed the stores");
Require(build(address + 40, [&](GuestBufferMemory& leased) {
std::uint32_t adjustment = 0;
const auto view = leased.Descriptor(address + 40, 8, adjustment);
access.bytes(view.buffer)[view.offset + adjustment] = std::byte{0x78};
}) == device && guest[40] == 0x78, "a writable heap mirror's second write-back lost the GPU's store");
guest[65536 + 1] = 0x79;
Require(build(0, {}) == device && access.addressBytes(device)[40] == std::byte{0x78} && access.addressBytes(device)[65536 + 1] == std::byte{0x79}, "the writable heap mirror missed a store after a write-back");
registry(false, true);
sweep();
Require(MirrorCounters().heapMirrors == before.heapMirrors, "the writable heap mirror outlived its range");