Merge pull request #306 from oneandonlydean/fix/agc-batch-stamps-under-draw-profile

fix(agc): stamp every batch under APS5_PROFILE_DRAW so the present report's GPU busy is real
This commit is contained in:
Alexey
2026-10-03 10:00:51 +03:00
committed by GitHub
3 changed files with 67 additions and 13 deletions
@@ -402,6 +402,7 @@ public:
// range index to pass to End, or NoTiming when timing is off or the batch's queries are used up.
static constexpr std::uint32_t NoTiming = 0xffffffffu;
static bool GpuTimingEnabled();
static bool BatchStampsEnabled();
std::uint32_t BeginGpuTiming(std::uint64_t key);
// `bytes`: what the range moved (a fill's, a copy's), summed per key on the [gputime] line.
void EndGpuTiming(std::uint32_t index, std::uint64_t bytes = 0);
@@ -454,7 +455,7 @@ public:
// frame record): finish() writes an entry per submitted batch into a ring of the last 128,
// under GuestMemory::GpuMutex and the ring's own mutex; the readers below take only the ring
// mutex, so the presenter reads them WITHOUT the GpuMutex. gpuStartNs/gpuEndNs are the
// whole-batch stamps (0 without APS5_PROFILE_GPU); `reads` and `readGeneration` (the
// whole-batch stamps (0 unless BatchStampsEnabled); `reads` and `readGeneration` (the
// write-watch generation collected over the noted in-place reads at submit) are filled with
// APS5_FLIP_READ_CHECK=1 only.
struct Completed {
@@ -694,6 +695,7 @@ private:
// Command buffers and fences of completed batches, reused by later ones (hundreds of batches per
// frame would otherwise allocate and free their objects each time).
std::vector<std::pair<VkCommandBuffer, VkFence>> spare;
std::vector<VkQueryPool> sparePools;
mutable std::mutex completedMutex;
std::array<Completed, CompletedRingSize> completed;
std::uint64_t newestSubmitted = 0;
@@ -1099,6 +1099,8 @@ Recorder::~Recorder() {
context.Function<PFN_vkDestroyFence>("vkDestroyFence")(context.device, fence, nullptr);
}
spare.clear();
for (const auto pool : sparePools) context.Function<PFN_vkDestroyQueryPool>("vkDestroyQueryPool")(context.device, pool, nullptr);
sparePools.clear();
// Sync() above waited for every batch, so no submission still signals the timeline.
if (timeline != VK_NULL_HANDLE) context.Function<PFN_vkDestroySemaphore>("vkDestroySemaphore")(context.device, timeline, nullptr);
timeline = VK_NULL_HANDLE;
@@ -1412,7 +1414,7 @@ void Recorder::ensureOpen() {
open = std::move(batch);
// The whole-batch range: its start stamp waits for the previous batches like any
// bottom-of-pipe stamp, so it marks when this batch's execution began.
open->batchTiming = BeginGpuTiming(BatchTimingKey);
if (BatchStampsEnabled()) open->batchTiming = beginTiming(BatchTimingKey);
}
}
@@ -1673,6 +1675,10 @@ bool Recorder::GpuTimingEnabled() {
return enabled;
}
bool Recorder::BatchStampsEnabled() {
return GpuTimingEnabled() || DrawProfiled();
}
void Recorder::CountBarriers(CommandClass which, std::uint32_t count) {
if (!DrawOrGpuProfiled()) return;
classBarriers[static_cast<std::size_t>(which)].fetch_add(count, std::memory_order_relaxed);
@@ -1761,19 +1767,26 @@ std::uint32_t Recorder::BeginGpuTiming(std::uint64_t key) {
}
std::uint32_t Recorder::beginTiming(std::uint64_t key) {
if (!GpuTimingEnabled()) return NoTiming;
const bool full = GpuTimingEnabled();
if (!full && !(key == BatchTimingKey && DrawProfiled())) return NoTiming;
const auto commands = open->commands;
const std::uint32_t queryCount = full ? MaxTimedRanges * 2 : 2;
if (open->queries == VK_NULL_HANDLE) {
VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
info.queryType = VK_QUERY_TYPE_TIMESTAMP;
info.queryCount = MaxTimedRanges * 2;
if (context.Function<PFN_vkCreateQueryPool>("vkCreateQueryPool")(context.device, &info, nullptr, &open->queries) != VK_SUCCESS) {
open->queries = VK_NULL_HANDLE;
return NoTiming;
if (!full && !sparePools.empty()) {
open->queries = sparePools.back();
sparePools.pop_back();
} else {
VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
info.queryType = VK_QUERY_TYPE_TIMESTAMP;
info.queryCount = queryCount;
if (context.Function<PFN_vkCreateQueryPool>("vkCreateQueryPool")(context.device, &info, nullptr, &open->queries) != VK_SUCCESS) {
open->queries = VK_NULL_HANDLE;
return NoTiming;
}
}
context.Function<PFN_vkCmdResetQueryPool>("vkCmdResetQueryPool")(commands, open->queries, 0, info.queryCount);
context.Function<PFN_vkCmdResetQueryPool>("vkCmdResetQueryPool")(commands, open->queries, 0, queryCount);
}
if (open->timedKeys.size() >= MaxTimedRanges) {
if (open->timedKeys.size() >= queryCount / 2) {
timingDropped.fetch_add(1, std::memory_order_relaxed);
return NoTiming;
}
@@ -1825,8 +1838,15 @@ void Recorder::readGpuTiming(Batch& batch) {
std::vector<std::uint64_t> stamps(batch.timedKeys.size() * 2);
const auto result = context.Function<PFN_vkGetQueryPoolResults>("vkGetQueryPoolResults")(context.device, batch.queries, 0, static_cast<std::uint32_t>(stamps.size()), stamps.size() * sizeof(std::uint64_t), stamps.data(), sizeof(std::uint64_t), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT);
if (result != VK_SUCCESS) return;
static auto lastReport = std::chrono::steady_clock::now();
const auto period = context.limits.timestampPeriod;
if (!GpuTimingEnabled()) {
if (batch.batchTiming == 0 && stamps[1] >= stamps[0]) {
batch.gpuStartNs = static_cast<double>(stamps[0]) * period;
batch.gpuEndNs = static_cast<double>(stamps[1]) * period;
}
return;
}
static auto lastReport = std::chrono::steady_clock::now();
// The union of the timed ranges (class ranges nest program ranges: a copy's transfer inside
// its class range) against the batch span gives what no range covers.
std::vector<std::pair<std::uint64_t, std::uint64_t>> intervals;
@@ -2986,7 +3006,16 @@ void Recorder::finish(std::unique_ptr<Batch> batch, bool wait, int source) {
}
void Recorder::release(Batch& batch) noexcept {
if (batch.queries != VK_NULL_HANDLE) context.Function<PFN_vkDestroyQueryPool>("vkDestroyQueryPool")(context.device, batch.queries, nullptr);
if (batch.queries != VK_NULL_HANDLE) {
if (!GpuTimingEnabled() && sparePools.size() < 64) {
try {
sparePools.push_back(batch.queries);
batch.queries = VK_NULL_HANDLE;
} catch (...) {
}
}
if (batch.queries != VK_NULL_HANDLE) context.Function<PFN_vkDestroyQueryPool>("vkDestroyQueryPool")(context.device, batch.queries, nullptr);
}
batch.queries = VK_NULL_HANDLE;
if (batch.samples != VK_NULL_HANDLE) context.Function<PFN_vkDestroyQueryPool>("vkDestroyQueryPool")(context.device, batch.samples, nullptr);
batch.samples = VK_NULL_HANDLE;
@@ -317,6 +317,28 @@ void afterRecordedWorkTests(const Device& device, Recorder& recorder) {
Require(seen == 1, "an action ran before the completion label recorded ahead of it");
}
void batchStampTests(Recorder& recorder) {
Require(recorder.Idle(), "batch stamps: the recorder is busy");
double previousEnd = 0;
for (int round = 0; round < 3; ++round) {
recorder.NotePendingWrite(0x60000, 0x100);
const auto serial = recorder.Submissions() + 1;
recorder.Submit();
recorder.Sync();
std::size_t missing = 0;
const auto batches = recorder.CompletedBatches(serial - 1, serial, missing);
Require(missing == 0 && batches.size() == 1 && batches.front().serial == serial, "a finished batch has no completion record");
const auto& batch = batches.front();
if (Recorder::BatchStampsEnabled()) {
Require(batch.gpuStartNs > 0 && batch.gpuEndNs >= batch.gpuStartNs && batch.gpuStartNs >= previousEnd, "batch stamps are missing or out of order");
previousEnd = batch.gpuEndNs;
} else {
Require(batch.gpuStartNs == 0 && batch.gpuEndNs == 0, "batch stamps were written with profiling off");
}
}
std::cout << "batch stamps " << (Recorder::BatchStampsEnabled() ? "checked" : "off") << '\n';
}
void labelTests(Recorder& recorder) {
const std::array<std::byte, 4> value{std::byte{1}, std::byte{0}, std::byte{0}, std::byte{0}};
Require(!recorder.PendingLabelIn(0x60000, 0x100), "an empty table reports a label");
@@ -2302,6 +2324,7 @@ int main() {
writeSettledTests(device, recorder);
completionCountTests(device, recorder);
afterRecordedWorkTests(device, recorder);
batchStampTests(recorder);
labelTests(recorder);
lateLabelTests(recorder);
unchangedSinceTests();