Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

feat(perf): PS5 Zen 2 dynamic core pinning and real-time scheduling

Rafael Abud Ferreira229ae89e3650
diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cpp
index 25c6278..3936ec6 100644
--- a/src/app/stream_player.cpp
+++ b/src/app/stream_player.cpp
@@ -179,6 +179,8 @@ struct StreamPlayer::Impl {
}
void audioLoop() {
+ platform::setThreadAffinity(platform::ThreadRole::Audio, "xcloud_adec");
+ platform::setThreadRealtimePriority();
audioDecoder.resetPlc();
QueuedAudioPacket packet;
bool first = true;
@@ -226,6 +228,7 @@ struct StreamPlayer::Impl {
}
void videoLoop() {
+ platform::setThreadAffinity(platform::ThreadRole::VideoDecode, "xcloud_vdec");
media::VideoDecoder decoder;
if (!decoder.init(decodeThreads)) {
end("could not start the H.264 decoder");
diff --git a/src/app/worker.cpp b/src/app/worker.cpp
index 96fe0f1..d625798 100644
--- a/src/app/worker.cpp
+++ b/src/app/worker.cpp
@@ -715,6 +715,7 @@ void loadLibrary(xcloud::GssvClient& gssv) {
xblAuth = g_xblAuth;
}
platform::startThread(g_hydrationThread, [library, publish, owned = gssv, xblAuth] {
+ platform::setThreadAffinity(platform::ThreadRole::Worker, "xcloud_hydr");
auto changed = [&] { publish(*library); };
library->loadFirstScreen(xblAuth, changed, &g_stopHydration);
library->loadFriends(xblAuth, changed, &g_stopHydration);
diff --git a/src/media/audio_in_ps5.cpp b/src/media/audio_in_ps5.cpp
index 69a2272..89bc5d5 100644
--- a/src/media/audio_in_ps5.cpp
+++ b/src/media/audio_in_ps5.cpp
@@ -7,6 +7,7 @@
#include "media/audio_in.h"
#include "input/controller.h"
#include "media/audio_in_common.h"
+#include "platform/platform.h"
#include "util/log.h"
#include <thread>
@@ -87,6 +88,7 @@ bool AudioIn::open(AudioCallback cb) {
impl_->mic.reset();
impl_->thread = std::thread([this]() {
+ platform::setThreadAffinity(platform::ThreadRole::Audio, "ps5_audio_in");
std::vector<int16_t> grain(kGrain);
unsigned lastStatus = 0xFFFFFFFF;
uint64_t loopCount = 0;
diff --git a/src/platform/linux/platform_linux.cpp b/src/platform/linux/platform_linux.cpp
index e6c5efa..be09933 100644
--- a/src/platform/linux/platform_linux.cpp
+++ b/src/platform/linux/platform_linux.cpp
@@ -16,6 +16,8 @@
#include <cstring>
#include <netdb.h>
#include <netinet/in.h>
+#include <pthread.h>
+#include <sched.h>
#include <sys/random.h>
#include <sys/socket.h>
#include <sys/stat.h>
@@ -165,4 +167,41 @@ bool restartApp() { return false; }
std::string firmwareVersion() { return {}; }
+void setThreadAffinity(ThreadRole role, const char* name) {
+ if (name && *name) {
+ pthread_setname_np(pthread_self(), name);
+ }
+ long numCores = sysconf(_SC_NPROCESSORS_ONLN);
+ if (numCores < 4) return; // Do not restrict on systems with few cores
+
+ cpu_set_t mask;
+ CPU_ZERO(&mask);
+ switch (role) {
+ case ThreadRole::Main:
+ CPU_SET(0, &mask);
+ break;
+ case ThreadRole::Network:
+ CPU_SET(std::min(1L, numCores - 1), &mask);
+ break;
+ case ThreadRole::VideoDecode:
+ CPU_SET(std::min(2L, numCores - 1), &mask);
+ break;
+ case ThreadRole::Audio:
+ CPU_SET(std::min(3L, numCores - 1), &mask);
+ break;
+ case ThreadRole::Worker:
+ for (long c = 4; c < numCores; ++c) CPU_SET(c, &mask);
+ if (numCores <= 4) CPU_SET(numCores - 1, &mask);
+ break;
+ }
+ pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &mask);
+}
+
+void setThreadRealtimePriority() {
+ struct sched_param param{};
+ int maxPrio = sched_get_priority_max(SCHED_RR);
+ param.sched_priority = maxPrio > 0 ? maxPrio : 1;
+ pthread_setschedparam(pthread_self(), SCHED_RR, &param);
+}
+
} // namespace xc::platform
diff --git a/src/platform/platform.h b/src/platform/platform.h
index c2f2b19..b27ad2f 100644
--- a/src/platform/platform.h
+++ b/src/platform/platform.h
@@ -53,6 +53,17 @@ struct Thread {
};
bool startThread(Thread& t, std::function<void()> fn, size_t stackBytes = 8u << 20);
+// Hardware core pinning and real-time scheduling for low latency and zero stutter.
+enum class ThreadRole {
+ Main, // Core 0: Main thread, UI, Vulkan command submission
+ Network, // Core 1: WebRTC I/O, SRTP decryption, Jitter buffer
+ VideoDecode, // Core 2: Video decoder (FFmpeg / libavcodec)
+ Audio, // Core 3: Audio decode & libSceAudioOut playback (Realtime priority)
+ Worker, // Core 4..7: Background tasks, library hydration, image decoding
+};
+void setThreadAffinity(ThreadRole role, const char* name = nullptr);
+void setThreadRealtimePriority();
+
// Short on-screen message: the PS5's system notification; in the desktop
// app a toast in the window (platform/toasts.h); stdout for the tools.
void notify(const std::string& text);
diff --git a/src/platform/ps5/platform_ps5.cpp b/src/platform/ps5/platform_ps5.cpp
index f5dc3ad..1cbe92c 100644
--- a/src/platform/ps5/platform_ps5.cpp
+++ b/src/platform/ps5/platform_ps5.cpp
@@ -17,6 +17,10 @@
#include <arpa/inet.h>
#include <atomic>
#include <netinet/in.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <sched.h>
+#include <sys/cpuset.h>
#include <sys/socket.h>
extern "C" {
@@ -213,4 +217,85 @@ void notify(const std::string& text, const std::string& forLog) {
sceKernelSendNotificationRequest(0, &req, sizeof req, 0);
}
+void setThreadAffinity(ThreadRole role, const char* name) {
+ if (name && *name) {
+ pthread_set_name_np(pthread_self(), name);
+ }
+ static cpuset_t s_procMask;
+ static bool s_procMaskQueried = false;
+ static int s_allowedCpus[32];
+ static int s_numAllowedCpus = 0;
+
+ if (!s_procMaskQueried) {
+ s_procMaskQueried = true;
+ CPU_ZERO(&s_procMask);
+ int grc = pthread_getaffinity_np(pthread_self(), sizeof(cpuset_t), &s_procMask);
+ if (grc == 0) {
+ for (int i = 0; i < 32; ++i) {
+ if (CPU_ISSET(i, &s_procMask)) {
+ s_allowedCpus[s_numAllowedCpus++] = i;
+ }
+ }
+ }
+ XC_LOGI("ps5 cpuset: %d allowed cpus detected (grc=%d)", s_numAllowedCpus, grc);
+ for (int i = 0; i < s_numAllowedCpus; ++i) {
+ XC_LOGI(" cpu slot %d: logical core %d", i, s_allowedCpus[i]);
+ }
+ }
+
+ if (s_numAllowedCpus <= 1) {
+ return;
+ }
+
+ cpuset_t mask;
+ CPU_ZERO(&mask);
+ auto setSlot = [&](int slotIndex) {
+ int idx = s_allowedCpus[slotIndex % s_numAllowedCpus];
+ CPU_SET(idx, &mask);
+ };
+
+ switch (role) {
+ case ThreadRole::Main:
+ setSlot(0);
+ if (s_numAllowedCpus >= 8) setSlot(1);
+ break;
+ case ThreadRole::Network:
+ setSlot(1);
+ break;
+ case ThreadRole::VideoDecode:
+ setSlot(2);
+ if (s_numAllowedCpus >= 4) setSlot(3);
+ break;
+ case ThreadRole::Audio:
+ setSlot(s_numAllowedCpus >= 6 ? 4 : 1);
+ break;
+ case ThreadRole::Worker:
+ if (s_numAllowedCpus >= 6) {
+ setSlot(5);
+ if (s_numAllowedCpus >= 7) setSlot(6);
+ } else {
+ setSlot(s_numAllowedCpus - 1);
+ }
+ break;
+ }
+ int rc = pthread_setaffinity_np(pthread_self(), sizeof(cpuset_t), &mask);
+ if (rc != 0) {
+ XC_LOGW("pthread_setaffinity_np(%s): %d", name ? name : "thread", rc);
+ } else {
+ XC_LOGI("thread %s pinned to role %d", name ? name : "unknown", static_cast<int>(role));
+ }
+}
+
+void setThreadRealtimePriority() {
+ struct sched_param param{};
+ param.sched_priority = sched_get_priority_max(SCHED_RR);
+ if (param.sched_priority <= 0) param.sched_priority = 50;
+ int rc = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
+ if (rc != 0) {
+ XC_LOGW("pthread_setschedparam(SCHED_RR, prio=%d): %d", param.sched_priority, rc);
+ } else {
+ XC_LOGI("thread elevated to SCHED_RR (prio=%d)", param.sched_priority);
+ }
+}
+
} // namespace xc::platform
diff --git a/src/stream/stream_session.cpp b/src/stream/stream_session.cpp
index c5f2dea..d5bfdd9 100644
--- a/src/stream/stream_session.cpp
+++ b/src/stream/stream_session.cpp
@@ -523,6 +523,11 @@ struct StreamSession::Impl {
videoReceiver->addToChain(videoReporter);
video->setMediaHandler(videoReceiver);
video->onFrame([this](rtc::binary data, rtc::FrameInfo info) {
+ static thread_local bool s_netPinned = false;
+ if (!s_netPinned) {
+ platform::setThreadAffinity(platform::ThreadRole::Network, "xcloud_net");
+ s_netPinned = true;
+ }
if (resolutionPending.exchange(false)) sendResolution(opt.resolutionAlias);
uint64_t now = platform::nowMs();
uint64_t last = lastVideoMs.exchange(now);
diff --git a/src/ui/image_cache.cpp b/src/ui/image_cache.cpp
index f7a2784..aa2fdbc 100644
--- a/src/ui/image_cache.cpp
+++ b/src/ui/image_cache.cpp
@@ -162,6 +162,7 @@ void ImageCache::trimDisk() {
}
void ImageCache::loop() {
+ platform::setThreadAffinity(platform::ThreadRole::Worker, "xcloud_img");
for (;;) {
Job job;
{