Skip to content
Open
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
6 changes: 5 additions & 1 deletion core/libs/prx/libSceAgcDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,10 @@ if(BUILD_TESTING)
set_tests_properties(agc_descriptor_pool_recycling PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME agc_draw_snapshot_cap COMMAND agc_driver_recorder_tests --draw-snapshot-cap-only)
set_tests_properties(agc_draw_snapshot_cap PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME agc_untracked_forget COMMAND agc_driver_recorder_tests --untracked-forget-only)
set_tests_properties(agc_untracked_forget PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME agc_untracked_key_proofs COMMAND agc_driver_recorder_tests --untracked-key-proofs-only)
set_tests_properties(agc_untracked_key_proofs PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME agc_draw_snapshot_epoch COMMAND agc_driver_recorder_tests --draw-snapshot-epoch-only)
set_tests_properties(agc_draw_snapshot_epoch PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME agc_multisample_guest_access COMMAND agc_driver_recorder_tests --multisample-only)
Expand All @@ -1154,7 +1158,7 @@ if(BUILD_TESTING)
add_test(NAME agc_metadata_pass COMMAND agc_driver_recorder_tests --metadata-pass-only)
set_tests_properties(agc_metadata_pass PROPERTIES SKIP_RETURN_CODE 77)
if(WIN32)
set_tests_properties(agc_recorder_read_list agc_host_import_unmap agc_parked_import_unmap agc_host_import_retirement agc_sparse_run_import agc_sparse_run_import_copied agc_unit_shadow agc_descriptor_pool_recycling agc_draw_snapshot_cap agc_draw_snapshot_epoch agc_multisample_guest_access agc_draw_inputs_in_place agc_resident_present agc_slice_dirty agc_queued_write_groups agc_metadata_pass agc_tiled_resolve PROPERTIES ENVIRONMENT_MODIFICATION
set_tests_properties(agc_recorder_read_list agc_host_import_unmap agc_parked_import_unmap agc_host_import_retirement agc_sparse_run_import agc_sparse_run_import_copied agc_unit_shadow agc_descriptor_pool_recycling agc_draw_snapshot_cap agc_untracked_forget agc_untracked_key_proofs agc_draw_snapshot_epoch agc_multisample_guest_access agc_draw_inputs_in_place agc_resident_present agc_slice_dirty agc_queued_write_groups agc_metadata_pass agc_tiled_resolve PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
add_test(NAME agc_host_import_second_mapping COMMAND agc_driver_recorder_tests --host-import-second-mapping-only)
set_tests_properties(agc_host_import_second_mapping PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT_MODIFICATION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ void FlushGpuWrites(std::uint64_t address, std::size_t bytes);
// value before consulting the page states and reading directly, and the same value after, knows
// the states it consulted were current and no page it read changed its mapping meanwhile.
std::uint64_t ForgetSerial();
std::uint64_t ForgetStamp(std::uint64_t address, std::size_t bytes);

// Attribution of guest memory accesses (the [hooksync] line in Recorder.cpp and the read-site
// counts of the [guestmem] line, APS5_PROFILE_DRAW): a sync the flush hook makes for an access has
Expand Down
28 changes: 27 additions & 1 deletion core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ namespace {
std::atomic<std::uint64_t> forgetCalls{0};
// See ForgetSerial: odd while a ForgetPages call stores its page states.
std::atomic<std::uint64_t> forgetSerial{0};
constexpr std::size_t ForgetSlotCount = 65536;
constexpr std::uint64_t ForgetBlockBytes = 65536;
std::array<std::atomic<std::uint64_t>, ForgetSlotCount> forgetSlots{};
std::atomic<std::uint64_t> wideForget{0};

void raiseForgetSlot(std::atomic<std::uint64_t>& slot, std::uint64_t serial) {
auto current = slot.load(std::memory_order_relaxed);
while (current < serial && !slot.compare_exchange_weak(current, serial, std::memory_order_release, std::memory_order_relaxed)) {}
}
std::atomic<std::uint64_t> forgetBytes{0};
std::atomic<std::uint64_t> collectMemoHits{0};
std::atomic<std::uint64_t> collectEpochBumps{0};
Expand Down Expand Up @@ -470,8 +479,15 @@ std::size_t ImagePagesSize() {
void ForgetPages(std::uintptr_t address, std::size_t bytes) {
forgetCalls.fetch_add(1, std::memory_order_relaxed);
forgetBytes.fetch_add(bytes, std::memory_order_relaxed);
forgetSerial.fetch_add(1, std::memory_order_release);
const auto started = forgetSerial.fetch_add(1, std::memory_order_release) + 1;
Pages().forget(address, bytes);
const auto done = started + 1;
if (bytes != 0) {
const auto first = address / ForgetBlockBytes;
const auto last = (address + bytes - 1) / ForgetBlockBytes;
if (last - first >= ForgetSlotCount) raiseForgetSlot(wideForget, done);
else for (auto block = first; block <= last; ++block) raiseForgetSlot(forgetSlots[block % ForgetSlotCount], done);
}
forgetSerial.fetch_add(1, std::memory_order_release);
}

Expand Down Expand Up @@ -707,6 +723,16 @@ std::uint64_t ForgetSerial() {
return forgetSerial.load(std::memory_order_acquire);
}

std::uint64_t ForgetStamp(std::uint64_t address, std::size_t bytes) {
if (bytes == 0) return 0;
const auto first = address / ForgetBlockBytes;
const auto last = (address + bytes - 1) / ForgetBlockBytes;
if (last - first >= ForgetSlotCount) return forgetSerial.load(std::memory_order_acquire);
auto newest = wideForget.load(std::memory_order_acquire);
for (auto block = first; block <= last; ++block) newest = std::max(newest, forgetSlots[block % ForgetSlotCount].load(std::memory_order_acquire));
return newest;
}

Commitment DescribeCommitted(std::uint64_t address, std::size_t bytes, bool writable) {
require(bytes <= std::numeric_limits<std::uint64_t>::max() - address, "address range overflow");
const TimedAccess timed(CounterVerify, bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef VK_NO_PROTOTYPES
#define VK_NO_PROTOTYPES
#endif
#include <array>
#include <vulkan/vulkan.h>
#include "prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp"
#include <cstddef>
Expand Down Expand Up @@ -68,6 +69,7 @@ DccKeys TextureClearKeys(const GuestTextureResource& resource, std::uint64_t gue
struct DccKeyProof {
DccKeys keys = DccKeys::Uncompressed;
std::uint64_t generation = 0;
std::array<std::uint64_t, 4> untracked{};
};
// TextureClearKeys with the scan skipped while the key range is unchanged since `proof` was taken
// (CollectWrites over the keys, then UnchangedSince its generation), so a surface's keys are scanned
Expand All @@ -93,6 +95,7 @@ struct DccRangeProof {
std::uint64_t count = 0;
DccKeys keys = DccKeys::Uncompressed;
std::uint64_t generation = 0;
std::array<std::uint64_t, 4> untracked{};
};
DccKeys ProvedCurrentDccKeys(std::uint64_t metaAddress, std::uint64_t surfaceBytes, DccRangeProof& proof);
// A surface's texels as a read sees them: the guest bytes, or the clear value of fast-cleared keys.
Expand Down
32 changes: 28 additions & 4 deletions core/libs/prx/libSceAgcDriver/Graphics/src/DccMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp"
#include "prx/libSceAgcDriver/Graphics/include/UnitShadow.hpp"
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cstdio>
Expand Down Expand Up @@ -686,9 +687,20 @@ DccKeys ProvedClearKeys(const GuestTextureResource& resource, std::uint64_t gues
counters.proved.fetch_add(1, std::memory_order_relaxed);
return proof.keys;
}
const auto epoch = GuestMemory::CollectEpoch();
std::array<std::uint64_t, 4> untracked{};
if (count != 0 && collected == 0 && epoch != 0) {
const auto* recorder = Recorder::Active();
untracked = {epoch, GuestMemory::NewestStamp(resource.dccAddress, count), GuestMemory::ForgetStamp(resource.dccAddress, count), recorder != nullptr ? recorder->NewestWriteNote(resource.dccAddress, count) : 0};
if ((GuestMemory::ForgetSerial() & 1u) != 0) untracked = {};
if (untracked[0] != 0 && proof.untracked == untracked) {
counters.proved.fetch_add(1, std::memory_order_relaxed);
return proof.keys;
}
}
bool memoized = false;
const auto keys = textureClearKeys(resource, guestBytes, memoized);
bool stable = collected != 0 && !memoized;
bool stable = (collected != 0 || untracked[0] != 0) && !memoized;
if (stable) {
// After the scan: a write noted since the collect is stamped above it anyway; one noted
// before it may still land after the bytes were read.
Expand All @@ -701,7 +713,7 @@ DccKeys ProvedClearKeys(const GuestTextureResource& resource, std::uint64_t gues
stable = !Recorder::SnapshotWriteOverlaps(resource.dccAddress, count);
}
}
proof = stable ? DccKeyProof{keys, collected} : DccKeyProof{};
proof = stable ? DccKeyProof{keys, collected, collected != 0 ? std::array<std::uint64_t, 4>{} : untracked} : DccKeyProof{};
counters.scanned.fetch_add(1, std::memory_order_relaxed);
if (!stable) counters.unstable.fetch_add(1, std::memory_order_relaxed);
return keys;
Expand All @@ -721,16 +733,28 @@ DccKeys ProvedCurrentDccKeys(std::uint64_t metaAddress, std::uint64_t surfaceByt
counters.rangeProved.fetch_add(1, std::memory_order_relaxed);
return proof.keys;
}
std::array<std::uint64_t, 4> untracked{};
if (collected == 0) {
const auto epoch = GuestMemory::CollectEpoch();
if (epoch != 0 && (GuestMemory::ForgetSerial() & 1u) == 0) {
const auto* recorder = Recorder::Active();
untracked = {epoch, GuestMemory::NewestStamp(metaAddress, count), GuestMemory::ForgetStamp(metaAddress, count), recorder != nullptr ? recorder->NewestWriteNote(metaAddress, count) : 0};
if (proof.untracked == untracked && proof.address == metaAddress && proof.count == count) {
counters.rangeProved.fetch_add(1, std::memory_order_relaxed);
return proof.keys;
}
}
}
bool memoized = false;
const auto keys = currentDccKeys(metaAddress, count, memoized);
bool stable = collected != 0 && !memoized;
bool stable = (collected != 0 || untracked[0] != 0) && !memoized;
if (stable) {
if (auto* recorder = Recorder::Active(); recorder != nullptr) {
const auto info = recorder->DescribePendingWrite(metaAddress, count);
stable = !info.has_value() || info->signaled;
}
}
proof = stable ? DccRangeProof{metaAddress, count, keys, collected} : DccRangeProof{};
proof = stable ? DccRangeProof{metaAddress, count, keys, collected, collected != 0 ? std::array<std::uint64_t, 4>{} : untracked} : DccRangeProof{};
counters.rangeScanned.fetch_add(1, std::memory_order_relaxed);
return keys;
}
Expand Down
5 changes: 3 additions & 2 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2124,8 +2124,9 @@ bool StorageTexture::compareUntracked(std::uint64_t address, std::size_t bytes,
const auto epoch = GuestMemory::CollectEpoch();
const bool whole = address == descriptor.baseAddress && bytes == guestBytes;
const auto hint = memoize && epoch != 0 && whole ? GuestMemory::UntrackedHint(address, bytes) : 0;
const std::array<std::uint64_t, 5> stamp{hint != 0, hint != 0 ? hint : epoch, GuestMemory::NewestStamp(address, bytes), GuestMemory::ForgetSerial(), recorder != nullptr ? recorder->NewestWriteNote(address, bytes) : 0};
const bool cacheable = memoize && epoch != 0 && (stamp[3] & 1u) == 0 && whole;
const bool forgetting = (GuestMemory::ForgetSerial() & 1u) != 0;
const std::array<std::uint64_t, 5> stamp{hint != 0, hint != 0 ? hint : epoch, GuestMemory::NewestStamp(address, bytes), GuestMemory::ForgetStamp(address, bytes), recorder != nullptr ? recorder->NewestWriteNote(address, bytes) : 0};
const bool cacheable = memoize && epoch != 0 && !forgetting && whole;
if (cacheable && comparedGuestBytes == stamp) {
std::fill(changed.begin(), changed.end(), GuestMemory::BlockUnchanged);
return true;
Expand Down
161 changes: 161 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3374,6 +3374,159 @@ void unitShadowTests(const Device& device, Recorder& recorder) {
Require(words[127 * unit] == 0x66 && words[128 * unit] == 0x77, "the retire did not publish the second slab's units covered by the old registration");
}

void untrackedForgetTests(const Device& device) {
const auto& base = device.GetContext();
if (base.hostImportAlignment == 0) {
std::cout << "host imports unavailable: untracked comparison memo not tested\n";
return;
}
constexpr std::uint32_t side = 256;
constexpr std::size_t surfaceBytes = side * side * 4;
constexpr std::size_t otherBytes = 65536;
const auto allocate = [](std::size_t bytes) -> void* {
#ifdef _WIN32
return VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
#else
return std::aligned_alloc(65536, bytes);
#endif
};
void* block = allocate(surfaceBytes);
void* other = allocate(otherBytes);
Require(block != nullptr && other != nullptr, "cannot allocate the untracked comparison blocks");
std::memset(block, 0x55, surfaceBytes);
std::memset(other, 0x11, otherBytes);
const auto address = reinterpret_cast<std::uint64_t>(block);
const auto otherAddress = reinterpret_cast<std::uint64_t>(other);
{
GuestAllocations::Mutation mutation;
mutation.Add(block, surfaceBytes, true, true, true);
mutation.Add(other, otherBytes, true, true, true);
}
struct Unregister {
const Context& context;
void* block;
void* other;
~Unregister() {
{
GuestAllocations::Mutation mutation;
mutation.Remove(block);
mutation.Remove(other);
}
HostImportFor(context, reinterpret_cast<std::uint64_t>(block), surfaceBytes);
}
} unregister{base, block, other};
if (HostImportFor(base, address, surfaceBytes) == nullptr) {
std::cout << "host import of the surface refused: untracked comparison memo not tested\n";
return;
}
if (AgcDriver::GuestMemory::Watched(address, surfaceBytes)) {
std::cout << "the surface is write-watched: untracked comparison memo not tested\n";
return;
}
TextureDetiler detiler(base);
auto context = base;
context.detiler = &detiler;
GuestTextureResource resource{};
resource.baseAddress = address;
resource.width = side;
resource.height = side;
resource.mipCount = 1;
resource.tileMode = TextureTileMode::kR64KBX;
resource.dimension = TextureDimension::k2D;
resource.format = 56;
resource.dstSelX = 4;
resource.dstSelY = 5;
resource.dstSelZ = 6;
resource.dstSelW = 7;
Require(DescribeSurface(resource).guestBytes == surfaceBytes, "the test surface has an unexpected size");
{
auto image = StorageTexture::Create(context, detiler, resource, 0);
AgcDriver::GuestMemory::BumpCollectEpoch();
image->Refresh();
const auto compares = StorageTexture::UntrackedCompares();
image->Refresh();
Require(StorageTexture::UntrackedCompares() == compares, "a second refresh in the same collect epoch compared the surface again");
GuestAllocations::GuestAllocationsInvalidate_nid_postfix(otherAddress, otherBytes);
image->Refresh();
Require(StorageTexture::UntrackedCompares() == compares, "forgetting memory the surface does not cover made its refresh compare again");
GuestAllocations::GuestAllocationsInvalidate_nid_postfix(address, 65536);
image->Refresh();
Require(StorageTexture::UntrackedCompares() == compares + 1, "forgetting the surface's own memory left its comparison memo standing");
}
#ifdef _WIN32
VirtualFree(other, 0, MEM_RELEASE);
#endif
std::cout << "untracked comparison memo: ok\n";
}

void untrackedKeyProofTests() {
constexpr std::uint32_t side = 128;
constexpr std::size_t surfaceBytes = side * side * 4;
constexpr std::size_t bytes = surfaceBytes + 65536;
#ifdef _WIN32
void* block = VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
#else
void* block = std::aligned_alloc(65536, bytes);
#endif
Require(block != nullptr, "cannot allocate the untracked key block");
auto* texels = static_cast<std::uint8_t*>(block);
std::memset(texels, 0x55, surfaceBytes);
std::memset(texels + surfaceBytes, 0x00, 65536);
const auto address = reinterpret_cast<std::uint64_t>(block);
{
GuestAllocations::Mutation mutation;
mutation.Add(block, bytes, true, true, true);
}
struct Unregister {
void* block;
~Unregister() {
GuestAllocations::Mutation mutation;
mutation.Remove(block);
}
} unregister{block};
const auto keys = address + surfaceBytes;
const auto keyCount = DccKeyBytes(surfaceBytes);
if (AgcDriver::GuestMemory::CollectWrites(keys, keyCount) != 0) {
std::cout << "the key range is write-watched: key proofs over untracked memory not tested\n";
return;
}
GuestTextureResource resource{};
resource.baseAddress = address;
resource.width = side;
resource.height = side;
resource.mipCount = 1;
resource.tileMode = TextureTileMode::kR64KBX;
resource.dimension = TextureDimension::k2D;
resource.format = 56;
resource.dstSelX = 4;
resource.dstSelY = 5;
resource.dstSelZ = 6;
resource.dstSelW = 7;
resource.dccAddress = keys;
Require(DescribeSurface(resource).guestBytes == surfaceBytes, "the key test surface has an unexpected size");
AgcDriver::GuestMemory::BumpCollectEpoch();
DccKeyProof proof;
const auto before = KeyProofCounts();
Require(ProvedClearKeys(resource, surfaceBytes, proof) == DccKeys::Clear0000, "0x00 keys are not a 0000 clear");
Require(ProvedClearKeys(resource, surfaceBytes, proof) == DccKeys::Clear0000, "the proved keys changed");
const auto proved = KeyProofCounts();
Require(proved.scanned == before.scanned + 1 && proved.proved == before.proved + 1, "a second key proof over untracked memory in the same collect epoch scanned the keys again");
std::memset(texels + surfaceBytes, 0xff, keyCount);
AgcDriver::GuestMemory::BumpCollectEpoch();
Require(ProvedClearKeys(resource, surfaceBytes, proof) != DccKeys::Clear0000, "keys stored before a new collect epoch were not read again");
Require(KeyProofCounts().scanned == proved.scanned + 1, "a key proof survived a store and a new collect epoch");
{
std::unique_lock gpu(AgcDriver::GuestMemory::GpuMutex());
DccRangeProof range;
const auto start = KeyProofCounts();
const auto first = ProvedCurrentDccKeys(keys, surfaceBytes, range);
Require(ProvedCurrentDccKeys(keys, surfaceBytes, range) == first, "the proved key range changed");
const auto after = KeyProofCounts();
Require(after.rangeScanned == start.rangeScanned + 1 && after.rangeProved == start.rangeProved + 1, "a second key range proof over untracked memory in the same collect epoch scanned the keys again");
}
std::cout << "key proofs over untracked memory: ok\n";
}

void storageRefreshTests(const Device& device, Recorder& recorder, bool watched) {
const auto& base = device.GetContext();
if (base.hostImportAlignment == 0) {
Expand Down Expand Up @@ -8208,6 +8361,14 @@ int main(int argc, char** argv) {
std::cout << "Address space pending write tests passed\n";
return 0;
}
if (argc == 2 && std::string_view(argv[1]) == "--untracked-key-proofs-only") {
untrackedKeyProofTests();
return 0;
}
if (argc == 2 && std::string_view(argv[1]) == "--untracked-forget-only") {
untrackedForgetTests(device);
return 0;
}
if (argc == 2 && std::string_view(argv[1]) == "--draw-snapshot-cap-only") {
drawSnapshotCapTests(device, recorder);
unwrittenSnapshotTests(device);
Expand Down
Loading