Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct DrawPhaseTotals {

struct PendingDrawPhases {
bool phases = false;
int kind = 0;
std::uint64_t captures = 0;
std::array<double, DrawDriverPhaseCount> ms{};
std::chrono::steady_clock::time_point tailAt{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Driver {
static PendingDispatchPhases& pendingDispatchPhases();
static std::chrono::steady_clock::time_point& packetStartedAt();
static PendingDrawPhases& pendingDrawPhases();
void addDrawPhases(const std::array<double, DrawDriverPhaseCount>& ms, bool drawn, std::uint64_t captures);
void addDrawPhases(const std::array<double, DrawDriverPhaseCount>& ms, bool drawn, std::uint64_t captures, int kind = 0);
void noteLabelStore(std::uint64_t address, std::span<const std::byte> bytes, std::uint64_t stamp, std::uint32_t queue);
bool storedSince(std::span<const std::uint32_t> packet, std::uint64_t address, std::size_t bytes, std::uint64_t received, std::uint32_t* writer = nullptr);
static void traceLabel(std::span<const std::uint32_t> packet, std::uint32_t queue);
Expand Down Expand Up @@ -263,6 +263,7 @@ class Driver {
static std::shared_ptr<const ShaderRecompiler::RecompileResult> patchDataWords(const DispatchVariant& variant, const std::vector<std::uint32_t>& words);
bool syncPendingRuns(std::uint64_t program, std::uint32_t queue, const ShaderRecompiler::RecompileResult& compiled, std::span<const ShaderRecompiler::MemoryRegion> regions, std::uint64_t& synced, PendingView& pending, std::optional<SampledReadScope>& sampling, std::span<const VouchedRegions> skipped = {});
EntryOutcome validateVariant(std::uint64_t program, std::uint32_t queue, const DispatchVariant& variant, std::span<const ShaderRecompiler::MemoryRegion> regions, std::uint64_t& imagesFlushed, std::uint64_t& runsSynced, std::optional<SampledReadScope>& sampling, std::vector<std::pair<std::uint32_t, std::uint32_t>>* live = nullptr);
EntryOutcome validateVariantTimed(std::uint64_t program, std::uint32_t queue, const DispatchVariant& variant, std::span<const ShaderRecompiler::MemoryRegion> regions, std::uint64_t& imagesFlushed, std::uint64_t& runsSynced, std::optional<SampledReadScope>& sampling, std::vector<std::pair<std::uint32_t, std::uint32_t>>* live = nullptr);
static int waitTimeoutMs();
static bool pollReapAll();
static bool pollTryEach();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <cstdio>
#include <atomic>
#include <chrono>
#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Synchronization/DeferredLabels.hpp"
Expand Down Expand Up @@ -89,7 +92,9 @@ bool Driver::vouchedValidation() {
return enabled;
}

EntryOutcome Driver::validateVariant(std::uint64_t program, std::uint32_t queue, const DispatchVariant& variant, std::span<const ShaderRecompiler::MemoryRegion> regions, std::uint64_t& imagesFlushed, std::uint64_t& runsSynced, std::optional<SampledReadScope>& sampling, std::vector<std::pair<std::uint32_t, std::uint32_t>>* live) {
static thread_local double tmpStepUs[6];
EntryOutcome Driver::validateVariantTimed(std::uint64_t program, std::uint32_t queue, const DispatchVariant& variant, std::span<const ShaderRecompiler::MemoryRegion> regions, std::uint64_t& imagesFlushed, std::uint64_t& runsSynced, std::optional<SampledReadScope>& sampling, std::vector<std::pair<std::uint32_t, std::uint32_t>>* live) {
auto tmpT0 = std::chrono::steady_clock::now(); auto tmpLap = [&](int k) { const auto n = std::chrono::steady_clock::now(); tmpStepUs[k] += std::chrono::duration<double, std::micro>(n - tmpT0).count(); tmpT0 = n; };
if (live != nullptr) live->clear();
if (DrawPipeline::Active()) {
auto& pipeline = DrawPipeline::Queue0();
Expand Down Expand Up @@ -118,7 +123,9 @@ EntryOutcome Driver::validateVariant(std::uint64_t program, std::uint32_t queue,
}
}
const std::span<const std::pair<std::uint64_t, std::uint64_t>> runs = vouched.empty() ? std::span<const std::pair<std::uint64_t, std::uint64_t>>(variant.runs) : std::span<const std::pair<std::uint64_t, std::uint64_t>>(checked);
tmpLap(0);
if (!syncPendingRuns(program, queue, *variant.compiled, regions, runsSynced, pending, sampling, vouched)) return EntryOutcome::Differing;
tmpLap(1);
auto publish = Graphics::Recorder::PublishGeneration();
auto pendingSerial = Graphics::StorageTexture::PendingSerial();
auto forget = GuestMemory::ForgetSerial();
Expand All @@ -135,6 +142,7 @@ EntryOutcome Driver::validateVariant(std::uint64_t program, std::uint32_t queue,
forget = GuestMemory::ForgetSerial();
if (Graphics::StorageTexture::AnyPendingOverlaps(variant.runs) || Graphics::AnyShadowedOverlaps(variant.runs)) return EntryOutcome::FlushingImage;
}
tmpLap(2);
if ((forget & 1) != 0) return EntryOutcome::ForgetMoved;
for (const auto& label : deferredLabels().labels) {
for (const auto& [begin, end] : runs) {
Expand All @@ -147,13 +155,27 @@ EntryOutcome Driver::validateVariant(std::uint64_t program, std::uint32_t queue,
}
}

tmpLap(3);
pending.Load();
bool unmapped = false;
if (!validateCaptured(program, queue, regions, *variant.compiled, true, pending, &unmapped, &sampling, masked ? &mask : nullptr, vouched)) return unmapped ? EntryOutcome::Inaccessible : EntryOutcome::Differing;
tmpLap(4);
if (Graphics::Recorder::PublishGeneration() != publish) return EntryOutcome::PublishMoved;
if (Graphics::StorageTexture::PendingSerial() != pendingSerial) return EntryOutcome::PendingMoved;
if (GuestMemory::ForgetSerial() != forget) return EntryOutcome::ForgetMoved;
return masked && !live->empty() ? EntryOutcome::EqualData : EntryOutcome::Equal;
}

EntryOutcome Driver::validateVariant(std::uint64_t program, std::uint32_t queue, const DispatchVariant& variant, std::span<const ShaderRecompiler::MemoryRegion> regions, std::uint64_t& imagesFlushed, std::uint64_t& runsSynced, std::optional<SampledReadScope>& sampling, std::vector<std::pair<std::uint32_t, std::uint32_t>>* live) {
static std::atomic<std::uint64_t> calls{0};
const auto t0 = std::chrono::steady_clock::now();
const auto result = validateVariantTimed(program, queue, variant, regions, imagesFlushed, runsSynced, sampling, live);
static thread_local double totalUs = 0; static thread_local std::uint64_t mine = 0;
totalUs += std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count();
++mine;
if (calls.fetch_add(1) % 200000 == 199999 && mine != 0) std::fprintf(stderr, "[tmp-validate] per call (thread avg over %llu): total %.2f us; pipeline overlap/drain + vouch %.2f, syncPendingRuns %.2f, storage/shadow overlaps %.2f, labels/legacy %.2f, validateCaptured compare %.2f\n", (unsigned long long)mine, totalUs / mine, tmpStepUs[0] / mine, tmpStepUs[1] / mine, tmpStepUs[2] / mine, tmpStepUs[3] / mine, tmpStepUs[4] / mine);
return result;
}


}
11 changes: 10 additions & 1 deletion core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <atomic>
#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp"
Expand Down Expand Up @@ -91,12 +92,14 @@
std::uint64_t drawIdentity = 0;
DrawAliasKeys drawAliases;
std::vector<DrawUserPatch> userPatches;
static thread_local double tmpK[6]; static thread_local std::uint64_t tmpKn = 0; auto tmpKt = std::chrono::steady_clock::now(); auto tmpKlap = [&](int k) { const auto n = std::chrono::steady_clock::now(); tmpK[k] += std::chrono::duration<double, std::micro>(n - tmpKt).count(); tmpKt = n; };
if (registerKey) {
const auto keyStart = profile ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{};
std::shared_ptr<const DrawRelocation> relocation;
bool aliasFirst = false;
if (drawRelocationEnabled()) {
drawIdentity = drawIdentityKey(queue, *submission.shaders);
tmpKlap(0);
std::lock_guard cacheLock(drawCacheMutex);
learnPendingRelocation();
drawStageDonorKeys.clear();
Expand All @@ -108,7 +111,9 @@
aliasFirst = relocation != nullptr && trial.productive;
}
}
tmpKlap(1);
drawKey = drawRegisterKey(queue, *submission.shaders, localDevice->Serial(), &shapeKey, traceDrawCache() ? &drawKeyComponents : nullptr, aliasFirst ? relocation.get() : nullptr, aliasFirst ? &drawAliases : nullptr);
tmpKlap(2);
std::unique_lock cacheLock(drawCacheMutex);
++drawEntryCounters.lookups;
++drawEntryCounters.registerKeyLookups;
Expand Down Expand Up @@ -136,7 +141,9 @@
++trial.lookups;
if (trial.lookups >= DrawRelocationTrial::Window) {
trial.productive = trial.hits * DrawRelocationTrial::MinimumHitShare >= trial.lookups;
if (!trial.productive) trial.idle = DrawRelocationTrial::IdleDraws;
static const bool neverIdle = std::getenv("APS5_RELOCATION_NEVER_IDLE") != nullptr;

Check failure on line 144 in core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp

View workflow job for this annotation

GitHub Actions / conventions

env-reference

Every APS5_* or ANYPS5_* name a pull request adds to the code has a row in docs/dev/ENVIRONMENT.md, a row whose name the pull request stops reading is removed, and a row the pull request removes is no longer read: APS5_RELOCATION_NEVER_IDLE is read here but has no row in docs/dev/ENVIRONMENT.md

Check failure on line 144 in core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp

View workflow job for this annotation

GitHub Actions / conventions

env-reference

Every APS5_* or ANYPS5_* name a pull request adds to the code has a row in docs/dev/ENVIRONMENT.md, a row whose name the pull request stops reading is removed, and a row the pull request removes is no longer read: APS5_RELOCATION_NEVER_IDLE is read here but has no row in docs/dev/ENVIRONMENT.md
if (!trial.productive && !neverIdle) trial.idle = DrawRelocationTrial::IdleDraws;
if (neverIdle) trial.productive = true;
trial.lookups = trial.hits = 0;
}
}
Expand Down Expand Up @@ -171,6 +178,7 @@
decode = nullptr;
}
if (dataEntry != nullptr && !DecodeProgramsCurrent(*dataEntry->decode, *submission.shaders)) dataEntry = nullptr;
if (registerKey) { tmpKlap(3); if (++tmpKn % 200000 == 0) std::fprintf(stderr, "[tmp-key] per draw (thread avg over %llu): identity %.2f us, relocation lookup+learn %.2f, register key build %.2f, cache find + relocate + data shape %.2f\n", (unsigned long long)tmpKn, tmpK[0] / tmpKn, tmpK[1] / tmpKn, tmpK[2] / tmpKn, tmpK[3] / tmpKn); }
phaseTiming.Phase(DrawRowKeyLookupValidate);

if (dataEntry != nullptr) {
Expand Down Expand Up @@ -292,6 +300,7 @@
bool verifyHit = false;
const bool stageEntries = useDrawEntries && !(graphics.stages.tessellation && !drawParameters.indirect && drawParameters.instanceCount > 1u);
lookupDraw(submission, localDevice, graphics, pixel, programs, roles, vertexInfos, stageEntries, registerKey, profile, drawKey, entry, matched, matchedRegions, drawHit, verifyHit, phaseTiming, phaseMs, dataEntry, dataCandidates, stageData);
if (profile) pendingDrawPhases().kind = drawHit ? (userPatches.empty() ? 1 : 2) : (std::any_of(matched.begin(), matched.end(), [](const auto& m) { return m != nullptr; }) || std::any_of(dataCandidates.begin(), dataCandidates.end(), [](const auto& c) { return !c.empty(); }) ? 3 : 4);
for (std::size_t i = 0; i < matched.size(); ++i) {
if (matched[i] == nullptr || matched[i]->shaderDataPatches.empty()) continue;
applyShaderDataPatches(*matched[i]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cstdio>
#include <atomic>
#include "prx/libSceAgcDriver/Execution/include/PerformanceTimer.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp"
#include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp"
Expand Down Expand Up @@ -177,7 +179,42 @@
std::vector<ShaderRecompiler::MemoryRegion> regions(stageCapture.regions.begin(), stageCapture.regions.end());
for (const auto& read : decodeReads[i]) regions.push_back({read.address, read.Bytes()});
std::stable_sort(regions.begin(), regions.end(), [](const ShaderRecompiler::MemoryRegion& a, const ShaderRecompiler::MemoryRegion& b) { return a.guestAddress < b.guestAddress; });
tagVariantRuns(*variant, regions, stageCapture.userDataUse, decodeReads[i].empty() && (!vertexInfos[i] || vertexInfos[i]->resourcesNum == 0), programs[i].hiddenUserWords, stageCapture.supplied);
static const bool relocateFetch = std::getenv("APS5_NO_VERTEX_FETCH_RELOCATION") == nullptr;

Check failure on line 182 in core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCapture.cpp

View workflow job for this annotation

GitHub Actions / conventions

env-reference

Every APS5_* or ANYPS5_* name a pull request adds to the code has a row in docs/dev/ENVIRONMENT.md, a row whose name the pull request stops reading is removed, and a row the pull request removes is no longer read: APS5_NO_VERTEX_FETCH_RELOCATION is read here but has no row in docs/dev/ENVIRONMENT.md

Check failure on line 182 in core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCapture.cpp

View workflow job for this annotation

GitHub Actions / conventions

env-reference

Every APS5_* or ANYPS5_* name a pull request adds to the code has a row in docs/dev/ENVIRONMENT.md, a row whose name the pull request stops reading is removed, and a row the pull request removes is no longer read: APS5_NO_VERTEX_FETCH_RELOCATION is read here but has no row in docs/dev/ENVIRONMENT.md
bool eligible = decodeReads[i].empty() && (!vertexInfos[i] || vertexInfos[i]->resourcesNum == 0);
const auto* use = &stageCapture.userDataUse;
ShaderRecompiler::SrtUserDataUse widened;
if (relocateFetch && !eligible && !decodeReads[i].empty() && std::all_of(decodeReads[i].begin(), decodeReads[i].end(), [](const Graphics::DecodeRead& read) { return read.origin >= 0 && read.origin < 64; })) {
widened = stageCapture.userDataUse;
for (const auto& read : decodeReads[i]) {
widened.asBase |= std::uint64_t {1} << read.origin;
for (std::uint32_t at = 0; at < read.size; at += sizeof(std::uint32_t)) widened.reads.emplace_back(read.address + at, read.origin);
}
use = &widened;
eligible = true;
}
tagVariantRuns(*variant, regions, *use, eligible, programs[i].hiddenUserWords, stageCapture.supplied);
{
static std::atomic<std::uint64_t> stages{0}, decodeOnly{0}, fetchOnly{0}, both{0}, eligibleNoBase{0}, eligibleBaseIsValue{0}, eligibleHidden{0}, eligibleCut{0}, eligibleOk{0};
const bool hasDecode = !decodeReads[i].empty();
const bool hasFetch = vertexInfos[i] && vertexInfos[i]->resourcesNum != 0;
const auto n = stages.fetch_add(1) + 1;
static std::atomic<std::uint64_t> bothRelocated{0}, bothUntraced{0}, bothNoWords{0};
if (hasDecode && hasFetch) { both.fetch_add(1); if (variant->relocatableWords != 0) bothRelocated.fetch_add(1); else if (!std::all_of(decodeReads[i].begin(), decodeReads[i].end(), [](const Graphics::DecodeRead& read) { return read.origin >= 0 && read.origin < 64; })) bothUntraced.fetch_add(1); else bothNoWords.fetch_add(1); }
else if (hasDecode) decodeOnly.fetch_add(1);
else if (hasFetch) fetchOnly.fetch_add(1);
else {
const auto& use = stageCapture.userDataUse;
const auto hidden = programs[i].hiddenUserWords;
auto mask = use.asBase & ~use.asValue;
auto afterHidden = hidden != 0 ? (hidden >= 64 ? 0 : mask & ~((std::uint64_t{1} << hidden) - 1)) : mask;
if (use.asBase == 0) eligibleNoBase.fetch_add(1);
else if (mask == 0) eligibleBaseIsValue.fetch_add(1);
else if (afterHidden == 0) eligibleHidden.fetch_add(1);
else if (variant->relocatableWords == 0) eligibleCut.fetch_add(1);
else eligibleOk.fetch_add(1);
}
if (n % 20000 == 0) std::fprintf(stderr, "[tmp-reloc] %llu stage captures: not eligible: decode reads %llu, vertex fetch %llu, both %llu; eligible: no base word %llu, every base word also a value %llu, cut by hidden words %llu, cut by duplicate/uncovered reads %llu, relocatable %llu; vertex-fetch stages now relocatable %llu, with an untraced decode read %llu, traced but no relocatable word left %llu\n", (unsigned long long)n, (unsigned long long)decodeOnly.load(), (unsigned long long)fetchOnly.load(), (unsigned long long)both.load(), (unsigned long long)eligibleNoBase.load(), (unsigned long long)eligibleBaseIsValue.load(), (unsigned long long)eligibleHidden.load(), (unsigned long long)eligibleCut.load(), (unsigned long long)eligibleOk.load(), (unsigned long long)bothRelocated.load(), (unsigned long long)bothUntraced.load(), (unsigned long long)bothNoWords.load());
}
if (drawStageDataHits() && stageCapture.capture != nullptr && !stageCapture.capture->readTrace.leaves.empty()) recordStageDataPositions(*variant, *stageCapture.capture, decodeReads[i]);
tagBufferWords(*variant, stageCapture.bufferBaseWords);
if (traceDrawCache() && entry != nullptr && matched[i] == nullptr && i < entry->stages.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ PendingDrawPhases& Driver::pendingDrawPhases() {
return pending;
}

void Driver::addDrawPhases(const std::array<double, DrawDriverPhaseCount>& ms, bool drawn, std::uint64_t captures) {
void Driver::addDrawPhases(const std::array<double, DrawDriverPhaseCount>& ms, bool drawn, std::uint64_t captures, int kind) {
std::lock_guard lock(drawPhasesMutex);
static std::array<std::array<double, DrawDriverPhaseCount>, 5> kindMs{};
static std::array<std::uint64_t, 5> kindCount{};
if (drawn && kind >= 0 && kind < 5) {
for (std::size_t i = 0; i < DrawDriverPhaseCount; ++i) kindMs[kind][i] += ms[i];
++kindCount[kind];
}
auto& totals = drawPhaseTotals;
for (std::size_t i = 0; i < DrawDriverPhaseCount; ++i) totals.ms[i] += ms[i];
++totals.packets;
Expand All @@ -28,6 +34,23 @@ void Driver::addDrawPhases(const std::array<double, DrawDriverPhaseCount>& ms, b
total += totals.ms[i];
}
AgcDriver::ProfilePrint_nid_no_patch( "[draw] driver phases (10 s, %llu draw packets, %llu drawn, %llu captures), us per packet:%s, total %.1f (%.1f ms; Graphics::Draw %.1f ms, skipped %.1f ms)\n", static_cast<unsigned long long>(totals.packets), static_cast<unsigned long long>(totals.drawn), static_cast<unsigned long long>(totals.captures), report.c_str(), total * 1000 / static_cast<double>(totals.packets), total, totals.ms[DrawRowGraphics], totals.ms[DrawRowSkipped]);
static const char* kindNames[5] = {"unknown", "full hit", "relocated hit", "partial (stages reused)", "miss"};
for (std::size_t k = 0; k < 5; ++k) {
if (kindCount[k] == 0) continue;
std::string row;
double sum = 0;
for (std::size_t i = 0; i < DrawDriverPhaseCount; ++i) {
const double us = kindMs[k][i] * 1000 / static_cast<double>(kindCount[k]);
sum += us;
if (us < 0.05) continue;
char text[64];
std::snprintf(text, sizeof(text), " %s %.1f", DrawDriverPhaseNames[i], us);
row += text;
}
std::fprintf(stderr, "[tmp-phases] %s: %llu draws, us per draw:%s, total %.1f\n", kindNames[k], static_cast<unsigned long long>(kindCount[k]), row.c_str(), sum);
}
kindMs = {};
kindCount = {};
totals.ms = {};
totals.packets = totals.drawn = totals.captures = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void Driver::execute(const Submission& submission) {
} else {
ms[DrawRowSkipped] = std::chrono::duration<double, std::milli>(now - packetTimer.start).count();
}
addDrawPhases(ms, drawn && pending.phases, pending.captures);
addDrawPhases(ms, drawn && pending.phases, pending.captures, pending.kind);
};
if (profilePackets) packetProfile.flushMs += std::chrono::duration<double, std::milli>(packetTimer.start - flushStart).count();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct DecodeRead {
std::uint64_t address;
std::array<std::byte, 16> data{};
std::uint32_t size = 0;
std::int32_t origin = -1;
std::span<const std::byte> Bytes() const { return {data.data(), size}; }
};
ShaderRecompiler::ShaderVertexStageInfo DecodeVertexStageInfo(std::span<const std::byte> header, std::uint64_t headerAddress, std::span<const std::uint32_t> userData, std::vector<DecodeRead>* reads = nullptr, bool staticAbi = false, std::uint32_t hiddenUserWords = 0);
Expand Down
Loading
Loading