Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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 @@ -187,6 +187,7 @@ void SetCollectPrefetch(bool enabled);
std::uint64_t CollectPrefetchWalks();
std::uint64_t ThreadCollectEpoch();
std::uint64_t UnwatchSerial();
bool UnwatchChangesSince(std::uint64_t since, std::vector<std::pair<std::uint64_t, std::uint64_t>>& spans, std::uint64_t& current);
std::uint64_t CollectWritesUncached(std::uint64_t address, std::size_t bytes);
struct GpuUseObservation {
std::uint64_t address = 0;
Expand Down
70 changes: 66 additions & 4 deletions core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "prx/libc/include/HostThreadSlot.hpp"
#include "prx/libc/include/HostThread.hpp"
#include <cerrno>
#include <deque>
#include <unordered_set>
#include <condition_variable>
#include <mutex>
Expand Down Expand Up @@ -330,6 +331,57 @@ std::atomic<std::uint64_t> collectEpochBumps{0};
std::atomic<std::uint64_t> prefetchRanges{0};
std::atomic<std::uint64_t> prefetchWalks{0};
std::atomic<std::uint64_t> unwatchSerial{0};

struct UnwatchChange {
std::uint64_t serial;
std::uint64_t begin;
std::uint64_t end;
};

struct UnwatchLog {
std::mutex mutex;
std::deque<UnwatchChange> entries;
std::uint64_t incompleteThrough = 0;
};

UnwatchLog& unwatchLog() {
static auto* log = new UnwatchLog;
return *log;
}

void bumpUnwatchSerial(std::span<const std::pair<std::uint64_t, std::uint64_t>> spans) {
constexpr std::size_t LogEntries = 4096;
#ifdef _WIN32
constexpr std::uint64_t page = 4096;
#else
const auto page = std::max<std::uint64_t>(4096, ::getpagesize());
#endif
auto& log = unwatchLog();
const std::lock_guard lock(log.mutex);
const auto serial = unwatchSerial.fetch_add(1, std::memory_order_release) + 1;
try {
for (const auto& [begin, end] : spans) {
if (end < begin || end > std::numeric_limits<std::uint64_t>::max() - (page - 1)) {
log.incompleteThrough = serial;
continue;
}
if (begin == end) continue;
log.entries.push_back({serial, begin & ~(page - 1), (end + page - 1) & ~(page - 1)});
}
} catch (const std::bad_alloc&) {
log.incompleteThrough = serial;
}
while (log.entries.size() > LogEntries) {
log.incompleteThrough = std::max(log.incompleteThrough, log.entries.front().serial);
log.entries.pop_front();
}
}

void bumpUnwatchSerial(std::uint64_t address, std::size_t bytes) {
const bool wraps = bytes > std::numeric_limits<std::uint64_t>::max() - address;
const std::array<std::pair<std::uint64_t, std::uint64_t>, 1> span{{{address, wraps ? 0 : address + bytes}}};
bumpUnwatchSerial(span);
}
// Walks that reported at least one written page (the ones the probe pass used to double), and the
// tracker mutex acquisitions that had to wait (APS5_PROFILE_DRAW; see lockTracker).
std::atomic<std::uint64_t> collectDirty{0};
Expand Down Expand Up @@ -2096,7 +2148,7 @@ void watchPrivateMapping(std::uintptr_t address, std::size_t bytes, std::uint64_
if (const auto found = tracker.driverPieces.find(block); found != tracker.driverPieces.end()) found->second.dropped = std::max<std::uint64_t>(found->second.dropped, tracker.generation);
tracker.stamp(block, tracker.generation, StampKind::Cpu);
}
unwatchSerial.fetch_add(1, std::memory_order_release);
bumpUnwatchSerial(address, bytes);
}
#endif

Expand Down Expand Up @@ -2677,6 +2729,16 @@ std::uint64_t UnwatchSerial() {
return unwatchSerial.load(std::memory_order_acquire);
}

bool UnwatchChangesSince(std::uint64_t since, std::vector<std::pair<std::uint64_t, std::uint64_t>>& spans, std::uint64_t& current) {
auto& log = unwatchLog();
const std::lock_guard lock(log.mutex);
current = UnwatchSerial();
if (since < log.incompleteThrough || since > current) return false;
const auto first = std::upper_bound(log.entries.begin(), log.entries.end(), since, [](std::uint64_t value, const UnwatchChange& entry) { return value < entry.serial; });
for (auto it = first; it != log.entries.end(); ++it) spans.emplace_back(it->begin, it->end);
return true;
}

void AdoptLatestCollectEpoch() {
const auto latest = lastBumpedEpoch.load(std::memory_order_acquire);
if (latest > HostThreadSlot<std::uint64_t, CollectEpochSlot>::Get()) HostThreadSlot<std::uint64_t, CollectEpochSlot>::Set(latest);
Expand Down Expand Up @@ -2829,7 +2891,7 @@ void unwatchLocked(WriteTracker& tracker, std::uint64_t address, std::size_t byt
#ifdef _WIN32
tracker.startHints(address, bytes);
if (tracker.coverage.Exclude(address, bytes, GuestArena::GuestArenaCommitGeneration_nid_postfix())) {
unwatchSerial.fetch_add(1, std::memory_order_release);
bumpUnwatchSerial(address, bytes);
bumpSubscriptions(address, bytes, WriteBumpSource::Unwatch);
noteLeftWatch(address, bytes);
}
Expand All @@ -2841,7 +2903,7 @@ void unwatchLocked(WriteTracker& tracker, std::uint64_t address, std::size_t byt
}
#else
if (GuestWriteWatch::GuestWriteWatchUnregister_nid_postfix(reinterpret_cast<const void*>(address), bytes)) {
unwatchSerial.fetch_add(1, std::memory_order_release);
bumpUnwatchSerial(address, bytes);
bumpSubscriptions(address, bytes, WriteBumpSource::Unwatch);
noteLeftWatch(address, bytes);
}
Expand Down Expand Up @@ -2982,7 +3044,7 @@ void Rewatch(std::span<const std::pair<std::uint64_t, std::uint64_t>> ranges) {
}
restored = true;
}
if (restored) unwatchSerial.fetch_add(1, std::memory_order_release);
if (restored) bumpUnwatchSerial(ranges);
#else
static_cast<void>(ranges);
#endif
Expand Down
Loading
Loading