mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-07-23 18:45:36 +00:00
Page manager bitarray
This commit is contained in:
parent
2fc62b5b5f
commit
6b60a8515f
@ -152,6 +152,10 @@ public:
|
|||||||
data.fill(0);
|
data.fill(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline constexpr void Fill() {
|
||||||
|
data.fill(~0ULL);
|
||||||
|
}
|
||||||
|
|
||||||
inline constexpr bool None() const {
|
inline constexpr bool None() const {
|
||||||
u64 result = 0;
|
u64 result = 0;
|
||||||
for (const auto& word : data) {
|
for (const auto& word : data) {
|
||||||
|
@ -11,12 +11,11 @@ namespace VideoCore {
|
|||||||
|
|
||||||
constexpr u64 PAGES_PER_WORD = 64;
|
constexpr u64 PAGES_PER_WORD = 64;
|
||||||
constexpr u64 BYTES_PER_PAGE = 4_KB;
|
constexpr u64 BYTES_PER_PAGE = 4_KB;
|
||||||
constexpr u64 BYTES_PER_WORD = PAGES_PER_WORD * BYTES_PER_PAGE;
|
|
||||||
|
|
||||||
constexpr u64 HIGHER_PAGE_BITS = 22;
|
constexpr u64 HIGHER_PAGE_BITS = 22;
|
||||||
constexpr u64 HIGHER_PAGE_SIZE = 1ULL << HIGHER_PAGE_BITS;
|
constexpr u64 HIGHER_PAGE_SIZE = 1ULL << HIGHER_PAGE_BITS;
|
||||||
constexpr u64 HIGHER_PAGE_MASK = HIGHER_PAGE_SIZE - 1ULL;
|
constexpr u64 HIGHER_PAGE_MASK = HIGHER_PAGE_SIZE - 1ULL;
|
||||||
constexpr u64 NUM_REGION_WORDS = HIGHER_PAGE_SIZE / BYTES_PER_WORD;
|
constexpr u64 NUM_REGION_PAGES = HIGHER_PAGE_SIZE / BYTES_PER_PAGE;
|
||||||
|
|
||||||
enum class Type {
|
enum class Type {
|
||||||
CPU,
|
CPU,
|
||||||
@ -24,8 +23,6 @@ enum class Type {
|
|||||||
Untracked,
|
Untracked,
|
||||||
};
|
};
|
||||||
|
|
||||||
using WordsArray = std::array<u64, NUM_REGION_WORDS>;
|
using RegionBits = Common::BitArray<NUM_REGION_PAGES>;
|
||||||
// TODO: use this insteed of WordsArray once it is ready
|
|
||||||
using RegionBits = Common::BitArray<NUM_REGION_WORDS * PAGES_PER_WORD>;
|
|
||||||
|
|
||||||
} // namespace VideoCore
|
} // namespace VideoCore
|
@ -4,8 +4,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <span>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include "common/div_ceil.h"
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include "common/adaptive_mutex.h"
|
#include "common/adaptive_mutex.h"
|
||||||
@ -14,8 +14,8 @@
|
|||||||
#endif
|
#endif
|
||||||
#include "common/debug.h"
|
#include "common/debug.h"
|
||||||
#include "common/types.h"
|
#include "common/types.h"
|
||||||
#include "video_core/page_manager.h"
|
|
||||||
#include "video_core/buffer_cache/region_definitions.h"
|
#include "video_core/buffer_cache/region_definitions.h"
|
||||||
|
#include "video_core/page_manager.h"
|
||||||
|
|
||||||
namespace VideoCore {
|
namespace VideoCore {
|
||||||
|
|
||||||
@ -27,9 +27,9 @@ class RegionManager {
|
|||||||
public:
|
public:
|
||||||
explicit RegionManager(PageManager* tracker_, VAddr cpu_addr_)
|
explicit RegionManager(PageManager* tracker_, VAddr cpu_addr_)
|
||||||
: tracker{tracker_}, cpu_addr{cpu_addr_} {
|
: tracker{tracker_}, cpu_addr{cpu_addr_} {
|
||||||
cpu.fill(~u64{0});
|
cpu.Fill();
|
||||||
gpu.fill(0);
|
gpu.Clear();
|
||||||
untracked.fill(~u64{0});
|
untracked.Fill();
|
||||||
}
|
}
|
||||||
explicit RegionManager() = default;
|
explicit RegionManager() = default;
|
||||||
|
|
||||||
@ -41,67 +41,35 @@ public:
|
|||||||
return cpu_addr;
|
return cpu_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr u64 ExtractBits(u64 word, size_t page_start, size_t page_end) {
|
static constexpr size_t SanitizeAddress(size_t address) {
|
||||||
constexpr size_t number_bits = sizeof(u64) * 8;
|
return static_cast<size_t>(std::max<s64>(static_cast<s64>(address), 0LL));
|
||||||
const size_t limit_page_end = number_bits - std::min(page_end, number_bits);
|
|
||||||
u64 bits = (word >> page_start) << page_start;
|
|
||||||
bits = (bits << limit_page_end) >> limit_page_end;
|
|
||||||
return bits;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr std::pair<size_t, size_t> GetWordPage(VAddr address) {
|
template <Type type>
|
||||||
const size_t converted_address = static_cast<size_t>(address);
|
RegionBits& GetRegionBits() noexcept {
|
||||||
const size_t word_number = converted_address / BYTES_PER_WORD;
|
static_assert(type != Type::Untracked);
|
||||||
const size_t amount_pages = converted_address % BYTES_PER_WORD;
|
if constexpr (type == Type::CPU) {
|
||||||
return std::make_pair(word_number, amount_pages / BYTES_PER_PAGE);
|
return cpu;
|
||||||
}
|
} else if constexpr (type == Type::GPU) {
|
||||||
|
return gpu;
|
||||||
template <typename Func>
|
} else if constexpr (type == Type::Untracked) {
|
||||||
void IterateWords(size_t offset, size_t size, Func&& func) const {
|
return untracked;
|
||||||
RENDERER_TRACE;
|
} else {
|
||||||
using FuncReturn = std::invoke_result_t<Func, std::size_t, u64>;
|
static_assert(false, "Invalid type");
|
||||||
static constexpr bool BOOL_BREAK = std::is_same_v<FuncReturn, bool>;
|
|
||||||
const size_t start = static_cast<size_t>(std::max<s64>(static_cast<s64>(offset), 0LL));
|
|
||||||
const size_t end = static_cast<size_t>(std::max<s64>(static_cast<s64>(offset + size), 0LL));
|
|
||||||
if (start >= HIGHER_PAGE_SIZE || end <= start) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
auto [start_word, start_page] = GetWordPage(start);
|
|
||||||
auto [end_word, end_page] = GetWordPage(end + BYTES_PER_PAGE - 1ULL);
|
|
||||||
constexpr size_t num_words = NUM_REGION_WORDS;
|
|
||||||
start_word = std::min(start_word, num_words);
|
|
||||||
end_word = std::min(end_word, num_words);
|
|
||||||
const size_t diff = end_word - start_word;
|
|
||||||
end_word += (end_page + PAGES_PER_WORD - 1ULL) / PAGES_PER_WORD;
|
|
||||||
end_word = std::min(end_word, num_words);
|
|
||||||
end_page += diff * PAGES_PER_WORD;
|
|
||||||
constexpr u64 base_mask{~0ULL};
|
|
||||||
for (size_t word_index = start_word; word_index < end_word; word_index++) {
|
|
||||||
const u64 mask = ExtractBits(base_mask, start_page, end_page);
|
|
||||||
start_page = 0;
|
|
||||||
end_page -= PAGES_PER_WORD;
|
|
||||||
if constexpr (BOOL_BREAK) {
|
|
||||||
if (func(word_index, mask)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
func(word_index, mask);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IteratePages(u64 mask, auto&& func) const {
|
template <Type type>
|
||||||
RENDERER_TRACE;
|
const RegionBits& GetRegionBits() const noexcept {
|
||||||
size_t offset = 0;
|
static_assert(type != Type::Untracked);
|
||||||
while (mask != 0) {
|
if constexpr (type == Type::CPU) {
|
||||||
const size_t empty_bits = std::countr_zero(mask);
|
return cpu;
|
||||||
offset += empty_bits;
|
} else if constexpr (type == Type::GPU) {
|
||||||
mask >>= empty_bits;
|
return gpu;
|
||||||
|
} else if constexpr (type == Type::Untracked) {
|
||||||
const size_t continuous_bits = std::countr_one(mask);
|
return untracked;
|
||||||
func(offset, continuous_bits);
|
} else {
|
||||||
mask = continuous_bits < PAGES_PER_WORD ? (mask >> continuous_bits) : 0;
|
static_assert(false, "Invalid type");
|
||||||
offset += continuous_bits;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,24 +81,25 @@ public:
|
|||||||
*/
|
*/
|
||||||
template <Type type, bool enable>
|
template <Type type, bool enable>
|
||||||
void ChangeRegionState(u64 dirty_addr, u64 size) noexcept(type == Type::GPU) {
|
void ChangeRegionState(u64 dirty_addr, u64 size) noexcept(type == Type::GPU) {
|
||||||
std::scoped_lock lk{lock};
|
const size_t offset = dirty_addr - cpu_addr;
|
||||||
std::span<u64> state_words = Span<type>();
|
const size_t start_page = SanitizeAddress(offset) / BYTES_PER_PAGE;
|
||||||
IterateWords(dirty_addr - cpu_addr, size, [&](size_t index, u64 mask) {
|
const size_t end_page = Common::DivCeil(SanitizeAddress(offset + size), BYTES_PER_PAGE);
|
||||||
if constexpr (type == Type::CPU) {
|
if (start_page >= NUM_REGION_PAGES || end_page <= start_page) {
|
||||||
UpdateProtection<!enable>(index, untracked[index], mask);
|
return;
|
||||||
}
|
}
|
||||||
if constexpr (enable) {
|
RENDERER_TRACE;
|
||||||
state_words[index] |= mask;
|
std::unique_lock lk{lock};
|
||||||
if constexpr (type == Type::CPU) {
|
static_assert(type != Type::Untracked);
|
||||||
untracked[index] |= mask;
|
|
||||||
}
|
RegionBits& bits = GetRegionBits<type>();
|
||||||
} else {
|
if constexpr (enable) {
|
||||||
state_words[index] &= ~mask;
|
bits.SetRange(start_page, end_page);
|
||||||
if constexpr (type == Type::CPU) {
|
} else {
|
||||||
untracked[index] &= ~mask;
|
bits.UnsetRange(start_page, end_page);
|
||||||
}
|
}
|
||||||
}
|
if constexpr (type == Type::CPU) {
|
||||||
});
|
UpdateProtection<!enable>(std::move(lk));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -144,53 +113,36 @@ public:
|
|||||||
template <Type type, bool clear>
|
template <Type type, bool clear>
|
||||||
void ForEachModifiedRange(VAddr query_cpu_range, s64 size, auto&& func) {
|
void ForEachModifiedRange(VAddr query_cpu_range, s64 size, auto&& func) {
|
||||||
RENDERER_TRACE;
|
RENDERER_TRACE;
|
||||||
std::scoped_lock lk{lock};
|
const size_t offset = query_cpu_range - cpu_addr;
|
||||||
|
const size_t start_page = SanitizeAddress(offset) / BYTES_PER_PAGE;
|
||||||
|
const size_t end_page = Common::DivCeil(SanitizeAddress(offset + size), BYTES_PER_PAGE);
|
||||||
|
if (start_page >= NUM_REGION_PAGES || end_page <= start_page) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::unique_lock lk{lock};
|
||||||
static_assert(type != Type::Untracked);
|
static_assert(type != Type::Untracked);
|
||||||
|
|
||||||
std::span<u64> state_words = Span<type>();
|
RegionBits& bits = GetRegionBits<type>();
|
||||||
const size_t offset = query_cpu_range - cpu_addr;
|
RegionBits mask{};
|
||||||
bool pending = false;
|
mask.SetRange(start_page, end_page);
|
||||||
size_t pending_offset{};
|
if constexpr (type == Type::GPU) {
|
||||||
size_t pending_pointer{};
|
mask &= ~untracked;
|
||||||
const auto release = [&]() {
|
}
|
||||||
func(cpu_addr + pending_offset * BYTES_PER_PAGE,
|
mask &= bits;
|
||||||
(pending_pointer - pending_offset) * BYTES_PER_PAGE);
|
|
||||||
};
|
if constexpr (clear) {
|
||||||
IterateWords(offset, size, [&](size_t index, u64 mask) {
|
bits.UnsetRange(start_page, end_page);
|
||||||
RENDERER_TRACE;
|
if constexpr (type == Type::CPU) {
|
||||||
if constexpr (type == Type::GPU) {
|
UpdateProtection<true>(std::move(lk));
|
||||||
mask &= ~untracked[index];
|
} else {
|
||||||
|
lk.unlock();
|
||||||
}
|
}
|
||||||
const u64 word = state_words[index] & mask;
|
} else {
|
||||||
if constexpr (clear) {
|
lk.unlock();
|
||||||
if constexpr (type == Type::CPU) {
|
}
|
||||||
UpdateProtection<true>(index, untracked[index], mask);
|
|
||||||
untracked[index] &= ~mask;
|
for (const auto& [start, end] : mask) {
|
||||||
}
|
func(cpu_addr + start * BYTES_PER_PAGE, (end - start) * BYTES_PER_PAGE);
|
||||||
state_words[index] &= ~mask;
|
|
||||||
}
|
|
||||||
const size_t base_offset = index * PAGES_PER_WORD;
|
|
||||||
IteratePages(word, [&](size_t pages_offset, size_t pages_size) {
|
|
||||||
RENDERER_TRACE;
|
|
||||||
const auto reset = [&]() {
|
|
||||||
pending_offset = base_offset + pages_offset;
|
|
||||||
pending_pointer = base_offset + pages_offset + pages_size;
|
|
||||||
};
|
|
||||||
if (!pending) {
|
|
||||||
reset();
|
|
||||||
pending = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (pending_pointer == base_offset + pages_offset) {
|
|
||||||
pending_pointer += pages_size;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
release();
|
|
||||||
reset();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
if (pending) {
|
|
||||||
release();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,22 +154,23 @@ public:
|
|||||||
*/
|
*/
|
||||||
template <Type type>
|
template <Type type>
|
||||||
[[nodiscard]] bool IsRegionModified(u64 offset, u64 size) const noexcept {
|
[[nodiscard]] bool IsRegionModified(u64 offset, u64 size) const noexcept {
|
||||||
|
RENDERER_TRACE;
|
||||||
|
const size_t start_page = SanitizeAddress(offset) / BYTES_PER_PAGE;
|
||||||
|
const size_t end_page = Common::DivCeil(SanitizeAddress(offset + size), BYTES_PER_PAGE);
|
||||||
|
if (start_page >= NUM_REGION_PAGES || end_page <= start_page) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// std::scoped_lock lk{lock}; // Is this needed?
|
||||||
static_assert(type != Type::Untracked);
|
static_assert(type != Type::Untracked);
|
||||||
|
|
||||||
const std::span<const u64> state_words = Span<type>();
|
const RegionBits& bits = GetRegionBits<type>();
|
||||||
bool result = false;
|
RegionBits test{};
|
||||||
IterateWords(offset, size, [&](size_t index, u64 mask) {
|
test.SetRange(start_page, end_page);
|
||||||
if constexpr (type == Type::GPU) {
|
if constexpr (type == Type::GPU) {
|
||||||
mask &= ~untracked[index];
|
test &= ~untracked;
|
||||||
}
|
}
|
||||||
const u64 word = state_words[index] & mask;
|
test &= bits;
|
||||||
if (word != 0) {
|
return test.Any();
|
||||||
result = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -230,37 +183,13 @@ private:
|
|||||||
*
|
*
|
||||||
* @tparam add_to_tracker True when the tracker should start tracking the new pages
|
* @tparam add_to_tracker True when the tracker should start tracking the new pages
|
||||||
*/
|
*/
|
||||||
template <bool add_to_tracker>
|
template <bool add_to_tracker, typename Lock>
|
||||||
void UpdateProtection(u64 word_index, u64 current_bits, u64 new_bits) const {
|
void UpdateProtection(Lock&& lk) {
|
||||||
RENDERER_TRACE;
|
RENDERER_TRACE;
|
||||||
u64 changed_bits = (add_to_tracker ? current_bits : ~current_bits) & new_bits;
|
RegionBits mask = cpu ^ untracked;
|
||||||
VAddr addr = cpu_addr + word_index * BYTES_PER_WORD;
|
untracked = cpu;
|
||||||
IteratePages(changed_bits, [&](size_t offset, size_t size) {
|
lk.unlock();
|
||||||
tracker->UpdatePageWatchers<add_to_tracker>(addr + offset * BYTES_PER_PAGE,
|
tracker->UpdatePageWatchersMasked<add_to_tracker>(cpu_addr, mask);
|
||||||
size * BYTES_PER_PAGE);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
template <Type type>
|
|
||||||
std::span<u64> Span() noexcept {
|
|
||||||
if constexpr (type == Type::CPU) {
|
|
||||||
return cpu;
|
|
||||||
} else if constexpr (type == Type::GPU) {
|
|
||||||
return gpu;
|
|
||||||
} else if constexpr (type == Type::Untracked) {
|
|
||||||
return untracked;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <Type type>
|
|
||||||
std::span<const u64> Span() const noexcept {
|
|
||||||
if constexpr (type == Type::CPU) {
|
|
||||||
return cpu;
|
|
||||||
} else if constexpr (type == Type::GPU) {
|
|
||||||
return gpu;
|
|
||||||
} else if constexpr (type == Type::Untracked) {
|
|
||||||
return untracked;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
|
||||||
@ -270,9 +199,9 @@ private:
|
|||||||
#endif
|
#endif
|
||||||
PageManager* tracker;
|
PageManager* tracker;
|
||||||
VAddr cpu_addr = 0;
|
VAddr cpu_addr = 0;
|
||||||
WordsArray cpu;
|
RegionBits cpu;
|
||||||
WordsArray gpu;
|
RegionBits gpu;
|
||||||
WordsArray untracked;
|
RegionBits untracked;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace VideoCore
|
} // namespace VideoCore
|
||||||
|
Loading…
Reference in New Issue
Block a user