mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-05-03 08:03:50 +00:00
Add Windows and macOS CPU affinity support
- Windows: SetThreadAffinityMask to pin main thread to core 0 and exclude workers from core 0 - macOS: thread_policy_set with THREAD_AFFINITY_POLICY tags to hint scheduler separation (tag 1 for main, tag 2 for workers)
This commit is contained in:
parent
86505ad377
commit
4c8fa9f1fe
2 changed files with 58 additions and 5 deletions
|
|
@ -59,6 +59,15 @@
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
#include <mach/mach.h>
|
||||||
|
#include <mach/thread_policy.h>
|
||||||
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace wowee {
|
namespace wowee {
|
||||||
|
|
@ -238,11 +247,10 @@ void Application::run() {
|
||||||
LOG_INFO("Starting main loop");
|
LOG_INFO("Starting main loop");
|
||||||
|
|
||||||
// Pin main thread to a dedicated CPU core to reduce scheduling jitter
|
// Pin main thread to a dedicated CPU core to reduce scheduling jitter
|
||||||
#ifdef __linux__
|
|
||||||
{
|
{
|
||||||
int numCores = static_cast<int>(std::thread::hardware_concurrency());
|
int numCores = static_cast<int>(std::thread::hardware_concurrency());
|
||||||
if (numCores >= 2) {
|
if (numCores >= 2) {
|
||||||
// Use core 0 for the main thread (typically the highest-clocked core)
|
#ifdef __linux__
|
||||||
cpu_set_t cpuset;
|
cpu_set_t cpuset;
|
||||||
CPU_ZERO(&cpuset);
|
CPU_ZERO(&cpuset);
|
||||||
CPU_SET(0, &cpuset);
|
CPU_SET(0, &cpuset);
|
||||||
|
|
@ -252,9 +260,31 @@ void Application::run() {
|
||||||
} else {
|
} else {
|
||||||
LOG_WARNING("Failed to pin main thread to CPU core 0 (error ", rc, ")");
|
LOG_WARNING("Failed to pin main thread to CPU core 0 (error ", rc, ")");
|
||||||
}
|
}
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
DWORD_PTR mask = 1; // Core 0
|
||||||
|
DWORD_PTR prev = SetThreadAffinityMask(GetCurrentThread(), mask);
|
||||||
|
if (prev != 0) {
|
||||||
|
LOG_INFO("Main thread pinned to CPU core 0 (", numCores, " cores available)");
|
||||||
|
} else {
|
||||||
|
LOG_WARNING("Failed to pin main thread to CPU core 0 (error ", GetLastError(), ")");
|
||||||
}
|
}
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
// macOS doesn't support hard pinning — use affinity tags to hint
|
||||||
|
// that the main thread should stay on its own core group
|
||||||
|
thread_affinity_policy_data_t policy = { 1 }; // tag 1 = main thread group
|
||||||
|
kern_return_t kr = thread_policy_set(
|
||||||
|
pthread_mach_thread_np(pthread_self()),
|
||||||
|
THREAD_AFFINITY_POLICY,
|
||||||
|
reinterpret_cast<thread_policy_t>(&policy),
|
||||||
|
THREAD_AFFINITY_POLICY_COUNT);
|
||||||
|
if (kr == KERN_SUCCESS) {
|
||||||
|
LOG_INFO("Main thread affinity tag set (", numCores, " cores available)");
|
||||||
|
} else {
|
||||||
|
LOG_WARNING("Failed to set main thread affinity tag (error ", kr, ")");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const bool frameProfileEnabled = envFlagEnabled("WOWEE_FRAME_PROFILE", false);
|
const bool frameProfileEnabled = envFlagEnabled("WOWEE_FRAME_PROFILE", false);
|
||||||
if (frameProfileEnabled) {
|
if (frameProfileEnabled) {
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,15 @@
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
#include <mach/mach.h>
|
||||||
|
#include <mach/thread_policy.h>
|
||||||
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace wowee {
|
namespace wowee {
|
||||||
|
|
@ -922,19 +931,33 @@ bool TerrainManager::advanceFinalization(FinalizingTile& ft) {
|
||||||
|
|
||||||
void TerrainManager::workerLoop() {
|
void TerrainManager::workerLoop() {
|
||||||
// Keep worker threads off core 0 (reserved for main thread)
|
// Keep worker threads off core 0 (reserved for main thread)
|
||||||
#ifdef __linux__
|
|
||||||
{
|
{
|
||||||
int numCores = static_cast<int>(std::thread::hardware_concurrency());
|
int numCores = static_cast<int>(std::thread::hardware_concurrency());
|
||||||
if (numCores >= 2) {
|
if (numCores >= 2) {
|
||||||
|
#ifdef __linux__
|
||||||
cpu_set_t cpuset;
|
cpu_set_t cpuset;
|
||||||
CPU_ZERO(&cpuset);
|
CPU_ZERO(&cpuset);
|
||||||
for (int i = 1; i < numCores; i++) {
|
for (int i = 1; i < numCores; i++) {
|
||||||
CPU_SET(i, &cpuset);
|
CPU_SET(i, &cpuset);
|
||||||
}
|
}
|
||||||
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
|
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
DWORD_PTR mask = 0;
|
||||||
|
for (int i = 1; i < numCores && i < 64; i++) {
|
||||||
|
mask |= (static_cast<DWORD_PTR>(1) << i);
|
||||||
}
|
}
|
||||||
}
|
SetThreadAffinityMask(GetCurrentThread(), mask);
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
// Use affinity tag 2 for workers (separate from main thread tag 1)
|
||||||
|
thread_affinity_policy_data_t policy = { 2 };
|
||||||
|
thread_policy_set(
|
||||||
|
pthread_mach_thread_np(pthread_self()),
|
||||||
|
THREAD_AFFINITY_POLICY,
|
||||||
|
reinterpret_cast<thread_policy_t>(&policy),
|
||||||
|
THREAD_AFFINITY_POLICY_COUNT);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
LOG_INFO("Terrain worker thread started");
|
LOG_INFO("Terrain worker thread started");
|
||||||
|
|
||||||
while (workerRunning.load()) {
|
while (workerRunning.load()) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue