Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ bool EmptyIndirectSkip() {
return enabled;
}

void FinishLeaseWritesBeforeRead() {
if (!Graphics::LeaseWritesMayBePending()) return;
GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Indirect);
std::lock_guard gpuLock(GuestMemory::GpuMutex());
Graphics::FinishLeaseWrites();
}

struct CpuIndirectProgramsStorage {};

std::unordered_map<std::uint64_t, bool>& CpuIndirectPrograms() {
Expand Down Expand Up @@ -536,6 +543,7 @@ DrawVerdict Driver::draw(QueueState& queue, std::span<const std::uint32_t> packe
const auto& indirect = *drawParameters.indirect;
recordQueuedLabelsBeforeRead(submission.queue);
const auto readStart = std::chrono::steady_clock::now();
FinishLeaseWritesBeforeRead();
const auto count = std::min(indirect.countIndirect ? Pm4::ReadDrawCount(indirect) : indirect.count, indirect.count);
bool any = false;
for (std::uint32_t record = 0; record < count && !any; ++record) any = recordDraws(Pm4::ReadDrawArguments(indirect, record));
Expand Down Expand Up @@ -1124,6 +1132,7 @@ DrawVerdict Driver::finishDraw(DrawTail& tail, PerformanceTimer& timing) {
}
recordQueuedLabelsBeforeRead(submission.queue);
const auto readStart = std::chrono::steady_clock::now();
FinishLeaseWritesBeforeRead();
const auto count = std::min(indirect.countIndirect ? Pm4::ReadDrawCount(indirect) : indirect.count, indirect.count);
std::vector<Pm4::DrawArguments> records;
for (std::uint32_t record = 0; record < count; ++record) records.push_back(Pm4::ReadDrawArguments(indirect, record));
Expand Down
4 changes: 2 additions & 2 deletions core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6464,7 +6464,7 @@ VulkanDevice::IndirectOutcome VulkanDevice::recordCompletedDispatch(const Shader
const auto syncStart = std::chrono::steady_clock::now();
recorder.Sync();
timer.phase(PhaseSync);
if (resources->HoldsLease()) Graphics::CountLeaseOutcome(true, 0);
if (resources->HoldsLease()) Graphics::CountLeaseOutcome(true, 0, !resources->BdaReadOnly());
if (profile) {
// APS5_PROFILE_DRAW: the [recorder] line's "address-based" syncs by program, every 10 s,
// so the programs still waiting here are known (under the GpuMutex, like the rest).
Expand Down Expand Up @@ -6493,7 +6493,7 @@ VulkanDevice::IndirectOutcome VulkanDevice::recordCompletedDispatch(const Shader
} else if (resources->HoldsLease()) {
// The lease is in the open batch (kept above, nothing submitted since): the pin waiter
// finishes the recorder up to that batch's serial. Prints the [address-sync] leases line.
Graphics::CountLeaseOutcome(false, recorder.Submissions() + 1);
Graphics::CountLeaseOutcome(false, recorder.Submissions() + 1, !resources->BdaReadOnly());
}
if (TraceDispatchIo()) std::fprintf(stderr, "[dispatch-io] %s:%s\n", groupsText, resources->Describe().c_str());
WatchMemory(programAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,15 @@ std::uint64_t ImageMirrorSerial(const Context& context, std::uint64_t address, s
// when synced, and the waiter targets the newest of them. Under APS5_PROFILE_DRAW CountLeaseOutcome
// prints the [address-sync] leases line every 10 s from LeaseCounters (cumulative).
bool SyncLeaseWork();
void CountLeaseOutcome(bool synced, std::uint64_t batchSerial);
void CountLeaseOutcome(bool synced, std::uint64_t batchSerial, bool writes);
void FinishLeaseWork();
void FinishLeaseWrites();
bool LeaseWorkPending();
bool LeaseWritesMayBePending();
struct LeaseStats {
std::uint64_t deferred = 0;
std::uint64_t synced = 0;
std::uint64_t writing = 0;
// Pin-contention waits made by guest threads (the waiter calls), how many of them finished the
// newest lease batch, how many had to drain the whole recorder instead (no lease batch noted),
// how many only dropped the cached address space (no GPU wait, see AddressSpaceStats), and
Expand Down
3 changes: 2 additions & 1 deletion core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2506,7 +2506,7 @@ void keepRecordedDraw(Recorder& recorder, const std::shared_ptr<ShaderResources>
// Counted for the [address-sync] line: a lease released by the completion (the pin waiter
// finishes the recorder up to the open batch, which holds the kept resources and gets the
// next serial), or waited for by the caller (any wait releases it at once).
if (resources->HoldsLease()) CountLeaseOutcome(outcome.waited, outcome.waited ? 0 : recorder.Submissions() + 1);
if (resources->HoldsLease()) CountLeaseOutcome(outcome.waited, outcome.waited ? 0 : recorder.Submissions() + 1, !resources->BdaReadOnly());
// Every use of a (possibly shared) resources object registers its GPU writes anew.
resources->MarkGpuWrites(recorder);
if (spaceKeep != nullptr) *spaceKeep = resources->ReleaseSpace();
Expand Down Expand Up @@ -3543,6 +3543,7 @@ bool prepareDraw(const Context& context, const State& state, const Pm4::DrawPara
// The reads wait for the producing batch through the flush hook, as the driver's
// resolve did for every indirect dispatch before its GPU path.
const auto readStart = std::chrono::steady_clock::now();
FinishLeaseWrites();
const auto count = std::min(args->countIndirect ? Pm4::ReadDrawCount(*args) : args->count, args->count);
records.reserve(count);
for (std::uint32_t record = 0; record < count; ++record) records.push_back(Pm4::ReadDrawArguments(*args, record));
Expand Down
43 changes: 34 additions & 9 deletions core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3708,6 +3708,9 @@ struct LeaseState {
const Recorder* leaseRecorder = nullptr;
std::uint64_t newestLeaseSerial = 0;
std::uint64_t finishedLeaseSerial = 0;
std::uint64_t newestWritingLeaseSerial = 0;
std::atomic<std::uint64_t> notedWritingLeases{0};
std::atomic<std::uint64_t> finishedWritingLeases{0};
};

LeaseState& Leases() {
Expand Down Expand Up @@ -3885,14 +3888,23 @@ void ensurePinWaiter() {
static_cast<void>(registered);
}

void finishLeaseBatches(std::uint64_t target, const void* site) {
const auto& state = Leases();
auto* recorder = Recorder::Active();
if (recorder == nullptr || recorder != state.leaseRecorder || target <= recorder->CompletedThrough()) return;
if (target >= recorder->OpenSerial()) recorder->Submit();
Recorder::AnnounceSyncSite(site);
recorder->FinishUpTo(target);
}

}

bool SyncLeaseWork() {
static const bool sync = std::getenv("APS5_SYNC_LEASE_DISPATCH") != nullptr;
return sync;
}

void CountLeaseOutcome(bool synced, std::uint64_t batchSerial) {
void CountLeaseOutcome(bool synced, std::uint64_t batchSerial, bool writes) {
static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr;
auto& state = Leases();
if (!synced) {
Expand All @@ -3903,11 +3915,17 @@ void CountLeaseOutcome(bool synced, std::uint64_t batchSerial) {
state.leaseRecorder = recorder;
state.newestLeaseSerial = 0;
state.finishedLeaseSerial = 0;
state.newestWritingLeaseSerial = 0;
}
state.newestLeaseSerial = std::max(state.newestLeaseSerial, batchSerial);
if (writes) {
state.newestWritingLeaseSerial = std::max(state.newestWritingLeaseSerial, batchSerial);
state.notedWritingLeases.fetch_add(1);
}
}
std::lock_guard lock(state.mutex);
++(synced ? state.stats.synced : state.stats.deferred);
if (writes) ++state.stats.writing;
if (!profile) return;
// APS5_PROFILE_DRAW: the leases (dispatches and draws) deferred and synced, the guest's
// pin-contention waits and the BDA table cache, every 10 s (cumulative counts, like the
Expand All @@ -3918,18 +3936,20 @@ void CountLeaseOutcome(bool synced, std::uint64_t batchSerial) {
const auto& stats = state.stats;
const auto table = BdaResources::TableCacheCounters();
const auto& snapshots = Snapshots();
AgcDriver::ProfilePrint_nid_no_patch("[address-sync] leases: %llu released at completion, %llu synced at once; %llu pin-contention waits by guest threads (%llu finished the lease batch, %llu drained the recorder, %llu cache-only drops) %.1f s; BDA table cache %llu hits / %llu misses (%zu tables held); snapshot compares: %llu skipped (live-backed region), %llu made\n", static_cast<unsigned long long>(stats.deferred), static_cast<unsigned long long>(stats.synced), static_cast<unsigned long long>(stats.contentionWaits), static_cast<unsigned long long>(stats.contentionSyncs), static_cast<unsigned long long>(stats.contentionDrains), static_cast<unsigned long long>(stats.cacheDrops), stats.contentionMs / 1000, static_cast<unsigned long long>(table.hits), static_cast<unsigned long long>(table.misses), table.held, static_cast<unsigned long long>(snapshots.skipped.load(std::memory_order_relaxed)), static_cast<unsigned long long>(snapshots.checked.load(std::memory_order_relaxed)));
AgcDriver::ProfilePrint_nid_no_patch("[address-sync] leases: %llu released at completion, %llu synced at once; %llu by builds that store through addresses; %llu pin-contention waits by guest threads (%llu finished the lease batch, %llu drained the recorder, %llu cache-only drops) %.1f s; BDA table cache %llu hits / %llu misses (%zu tables held); snapshot compares: %llu skipped (live-backed region), %llu made\n", static_cast<unsigned long long>(stats.deferred), static_cast<unsigned long long>(stats.synced), static_cast<unsigned long long>(stats.writing), static_cast<unsigned long long>(stats.contentionWaits), static_cast<unsigned long long>(stats.contentionSyncs), static_cast<unsigned long long>(stats.contentionDrains), static_cast<unsigned long long>(stats.cacheDrops), stats.contentionMs / 1000, static_cast<unsigned long long>(table.hits), static_cast<unsigned long long>(table.misses), table.held, static_cast<unsigned long long>(snapshots.skipped.load(std::memory_order_relaxed)), static_cast<unsigned long long>(snapshots.checked.load(std::memory_order_relaxed)));
}

void FinishLeaseWork() {
GuestMemory::AssertGpuLockHeld("FinishLeaseWork");
const auto& state = Leases();
auto* recorder = Recorder::Active();
const auto target = state.newestLeaseSerial;
if (recorder == nullptr || recorder != state.leaseRecorder || target <= recorder->CompletedThrough()) return;
if (target >= recorder->OpenSerial()) recorder->Submit();
Recorder::AnnounceSyncSite(__builtin_return_address(0));
recorder->FinishUpTo(target);
finishLeaseBatches(Leases().newestLeaseSerial, __builtin_return_address(0));
}

void FinishLeaseWrites() {
GuestMemory::AssertGpuLockHeld("FinishLeaseWrites");
auto& state = Leases();
const auto noted = state.notedWritingLeases.load();
finishLeaseBatches(state.newestWritingLeaseSerial, __builtin_return_address(0));
state.finishedWritingLeases.store(noted);
}

bool LeaseWorkPending() {
Expand All @@ -3939,6 +3959,11 @@ bool LeaseWorkPending() {
return recorder != nullptr && recorder == state.leaseRecorder && state.newestLeaseSerial > recorder->CompletedThrough();
}

bool LeaseWritesMayBePending() {
const auto& state = Leases();
return state.notedWritingLeases.load() > state.finishedWritingLeases.load();
}

LeaseStats LeaseCounters() {
auto& state = Leases();
std::lock_guard lock(state.mutex);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if(BUILD_TESTING)
agc_add_draw_pipeline_test(agc_driver_indirect_draw_pending_producer)
add_test(NAME agc_driver_indirect_draw_pending_producer_gpu_indirect_disabled COMMAND agc_driver_indirect_draw_pending_producer_tests --gpu-indirect-disabled)
set_tests_properties(agc_driver_indirect_draw_pending_producer_gpu_indirect_disabled PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_GPU_INDIRECT_DRAW=1" SKIP_RETURN_CODE 77 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
if(WIN32)
set_tests_properties(agc_driver_indirect_draw_pending_producer_gpu_indirect_disabled PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endif()
Loading
Loading