Skip to content

Commit

Permalink
gpu: reduce cpu usage on cache commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DHrpcs3 committed Nov 1, 2024
1 parent 9558bb7 commit 4bccf99
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
18 changes: 15 additions & 3 deletions rpcsx/gpu/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,14 @@ Device::Device() : vkContext(createVkContext(this)) {

cacheUpdateThread = std::jthread([this](const std::stop_token &stopToken) {
auto &sched = graphicsPipes[0].scheduler;
std::uint32_t prevIdleValue = 0;
while (!stopToken.stop_requested()) {
if (gpuCacheCommandIdle.wait(prevIdleValue) != std::errc{}) {
continue;
}

prevIdleValue = gpuCacheCommandIdle.load(std::memory_order::acquire);

for (int vmId = 0; vmId < kMaxProcessCount; ++vmId) {
auto page = gpuCacheCommand[vmId].load(std::memory_order::relaxed);
if (page == 0) {
Expand Down Expand Up @@ -996,11 +1003,16 @@ static void notifyPageChanges(Device *device, int vmId, std::uint32_t firstPage,
(static_cast<std::uint64_t>(pageCount - 1) << 32) | firstPage;

while (true) {
for (std::size_t i = 0; i < std::size(device->cacheCommands); ++i) {
for (std::size_t i = 0; i < std::size(device->cpuCacheCommands); ++i) {
std::uint64_t expCommand = 0;
if (device->cacheCommands[vmId][i].compare_exchange_strong(
expCommand, command, std::memory_order::acquire,
if (device->cpuCacheCommands[vmId][i].compare_exchange_strong(
expCommand, command, std::memory_order::release,
std::memory_order::relaxed)) {
device->cpuCacheCommandsIdle[vmId].fetch_add(
1, std::memory_order::release);
device->cpuCacheCommandsIdle[vmId].notify_one();

while (device->cpuCacheCommands[vmId][i].load(std::memory_order::acquire) != 0) {}
return;
}
}
Expand Down
11 changes: 7 additions & 4 deletions rpcsx/gpu/DeviceContext.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "orbis/utils/SharedAtomic.hpp"
#include <atomic>
#include <cstdint>

Expand Down Expand Up @@ -66,10 +67,12 @@ enum {
struct DeviceContext {
static constexpr auto kMaxProcessCount = 6;

PadState kbPadState;
std::atomic<std::uint64_t> cacheCommands[kMaxProcessCount][4];
std::atomic<std::uint32_t> gpuCacheCommand[kMaxProcessCount];
std::atomic<std::uint8_t> *cachePages[kMaxProcessCount];
PadState kbPadState{};
std::atomic<std::uint64_t> cpuCacheCommands[kMaxProcessCount][4]{};
orbis::shared_atomic32 cpuCacheCommandsIdle[kMaxProcessCount]{};
orbis::shared_atomic32 gpuCacheCommand[kMaxProcessCount]{};
orbis::shared_atomic32 gpuCacheCommandIdle{};
std::atomic<std::uint8_t> *cachePages[kMaxProcessCount]{};

volatile std::uint32_t flipBuffer[kMaxProcessCount];
volatile std::uint64_t flipArg[kMaxProcessCount];
Expand Down
24 changes: 20 additions & 4 deletions rpcsx/iodev/dce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,26 @@ static void runBridge(int vmId) {
auto gpu = amdgpu::DeviceCtl{orbis::g_context.gpuDevice};
auto &gpuCtx = gpu.getContext();
std::vector<std::uint64_t> fetchedCommands;
fetchedCommands.reserve(std::size(gpuCtx.cacheCommands));
fetchedCommands.reserve(std::size(gpuCtx.cpuCacheCommands));

std::vector<std::atomic<std::uint64_t> *> fetchedAtomics;
std::uint32_t prevIdleValue = 0;

while (true) {
for (auto &command : gpuCtx.cacheCommands) {
std::uint64_t value = command[vmId].load(std::memory_order::relaxed);
if (gpuCtx.cpuCacheCommandsIdle[vmId].wait(prevIdleValue) !=
std::errc{}) {
continue;
}

prevIdleValue =
gpuCtx.cpuCacheCommandsIdle[vmId].load(std::memory_order::acquire);

for (auto &command : gpuCtx.cpuCacheCommands[vmId]) {
std::uint64_t value = command.load(std::memory_order::relaxed);

if (value != 0) {
fetchedCommands.push_back(value);
command[vmId].store(0, std::memory_order::relaxed);
fetchedAtomics.push_back(&command);
}
}

Expand Down Expand Up @@ -187,7 +198,12 @@ static void runBridge(int vmId) {
}
}

for (auto fetchedAtomic : fetchedAtomics) {
fetchedAtomic->store(0, std::memory_order::release);
}

fetchedCommands.clear();
fetchedAtomics.clear();
}
}}.detach();
}
Expand Down

0 comments on commit 4bccf99

Please sign in to comment.