mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #626 from oneandonlydean/perf/agc-pin-workers-linux
perf(agc): pin the queue workers to the performance cores on Linux by default
This commit is contained in:
@@ -10,7 +10,9 @@ std::uint64_t WorkerAffinityMask() {
|
||||
if (std::getenv("APS5_NO_WORKER_AFFINITY") != nullptr) return std::uint64_t{0};
|
||||
const auto requested = CpuTopology::MaskFromEnvironment("APS5_WORKER_AFFINITY_MASK");
|
||||
if (requested != 0) return requested;
|
||||
#ifndef __linux__
|
||||
if (std::getenv("APS5_WORKER_AFFINITY") == nullptr) return std::uint64_t{0};
|
||||
#endif
|
||||
const auto& layout = CpuTopology::Get();
|
||||
return layout.hybrid ? layout.performant : std::uint64_t{0};
|
||||
}();
|
||||
|
||||
@@ -303,6 +303,11 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(application_heap_replacement application_heap_invalid_metadata application_heap_default application_heap_exit_order
|
||||
PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
add_test_executable(cpu_topology_tests ${LIBS_INCLUDE_DIR}/tests/CpuTopology.cpp)
|
||||
target_include_directories(cpu_topology_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
add_test(NAME cpu_topology COMMAND cpu_topology_tests)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
|
||||
@@ -8,6 +8,12 @@
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#elif defined(__linux__)
|
||||
#include <fstream>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
// Hybrid CPU layout (Intel P/E cores) from the OS: which logical processors belong to the lowest and
|
||||
@@ -22,6 +28,32 @@ struct Layout {
|
||||
bool hybrid = false;
|
||||
};
|
||||
|
||||
#ifdef __linux__
|
||||
inline std::uint64_t MaskFromCpuList(const char* path) {
|
||||
std::ifstream file(path);
|
||||
std::string text;
|
||||
if (!std::getline(file, text)) return 0;
|
||||
std::uint64_t mask = 0;
|
||||
const char* cursor = text.c_str();
|
||||
while (*cursor != '\0') {
|
||||
char* end = nullptr;
|
||||
const unsigned long first = std::strtoul(cursor, &end, 10);
|
||||
if (end == cursor) return 0;
|
||||
unsigned long last = first;
|
||||
cursor = end;
|
||||
if (*cursor == '-') {
|
||||
last = std::strtoul(cursor + 1, &end, 10);
|
||||
if (end == cursor + 1 || last < first) return 0;
|
||||
cursor = end;
|
||||
}
|
||||
for (unsigned long cpu = first; cpu <= last && cpu < 64; ++cpu) mask |= std::uint64_t{1} << cpu;
|
||||
if (*cursor == ',') ++cursor;
|
||||
else if (*cursor != '\0') return 0;
|
||||
}
|
||||
return mask;
|
||||
}
|
||||
#endif
|
||||
|
||||
inline const Layout& Get() {
|
||||
static const Layout layout = [] {
|
||||
Layout result{};
|
||||
@@ -57,6 +89,16 @@ inline const Layout& Get() {
|
||||
result.efficient = byClass[lowest];
|
||||
result.performant = byClass[highest];
|
||||
result.hybrid = result.efficient != 0 && result.performant != 0;
|
||||
#elif defined(__linux__)
|
||||
cpu_set_t set;
|
||||
CPU_ZERO(&set);
|
||||
if (sched_getaffinity(0, sizeof(set), &set) == 0) {
|
||||
for (unsigned cpu = 0; cpu < 64; ++cpu)
|
||||
if (CPU_ISSET(cpu, &set)) result.process |= std::uint64_t{1} << cpu;
|
||||
}
|
||||
result.performant = MaskFromCpuList("/sys/devices/cpu_core/cpus");
|
||||
result.efficient = MaskFromCpuList("/sys/devices/cpu_atom/cpus");
|
||||
result.hybrid = result.efficient != 0 && result.performant != 0 && (result.efficient & result.performant) == 0;
|
||||
#endif
|
||||
return result;
|
||||
}();
|
||||
@@ -78,6 +120,8 @@ inline bool Trace() {
|
||||
inline unsigned long ThreadId(void* handle) {
|
||||
#ifdef _WIN32
|
||||
return handle != nullptr ? GetThreadId(static_cast<HANDLE>(handle)) : GetCurrentThreadId();
|
||||
#elif defined(__linux__)
|
||||
return handle == nullptr ? static_cast<unsigned long>(gettid()) : 0;
|
||||
#else
|
||||
(void)handle;
|
||||
return 0;
|
||||
@@ -93,6 +137,15 @@ inline std::uint64_t Pin(void* handle, std::uint64_t mask) {
|
||||
if (mask == 0) return 0;
|
||||
HANDLE thread = handle != nullptr ? static_cast<HANDLE>(handle) : GetCurrentThread();
|
||||
return SetThreadAffinityMask(thread, static_cast<DWORD_PTR>(mask)) != 0 ? mask : 0;
|
||||
#elif defined(__linux__)
|
||||
const auto& layout = Get();
|
||||
if (layout.process != 0) mask &= layout.process;
|
||||
if (mask == 0 || handle != nullptr) return 0;
|
||||
cpu_set_t set;
|
||||
CPU_ZERO(&set);
|
||||
for (unsigned cpu = 0; cpu < 64; ++cpu)
|
||||
if ((mask >> cpu) & 1u) CPU_SET(cpu, &set);
|
||||
return pthread_setaffinity_np(pthread_self(), sizeof(set), &set) == 0 ? mask : 0;
|
||||
#else
|
||||
(void)handle;
|
||||
(void)mask;
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "prx/libc/include/CpuTopology.hpp"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
static void Require(bool value, const char* what) {
|
||||
if (value) return;
|
||||
std::fprintf(stderr, "cpu topology test failed: %s\n", what);
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
static std::uint64_t Parse(const char* text) {
|
||||
const auto path = std::filesystem::temp_directory_path() / "anyps5_cpu_topology_test_cpus";
|
||||
{
|
||||
std::ofstream file(path, std::ios::trunc);
|
||||
file << text;
|
||||
}
|
||||
const auto mask = CpuTopology::MaskFromCpuList(path.c_str());
|
||||
std::filesystem::remove(path);
|
||||
return mask;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Require(Parse("0-15\n") == 0xffffull, "range");
|
||||
Require(Parse("16-31\n") == 0xffff0000ull, "high range");
|
||||
Require(Parse("0,2,4-5\n") == 0x35ull, "list of cpus and ranges");
|
||||
Require(Parse("3") == 0x8ull, "single cpu without a newline");
|
||||
Require(Parse("62-70\n") == 0xc000000000000000ull, "cpus past 63 are left out");
|
||||
Require(Parse("") == 0, "empty list");
|
||||
Require(Parse("4-2\n") == 0, "reversed range");
|
||||
Require(Parse("0-x\n") == 0, "malformed range");
|
||||
Require(Parse("0;1\n") == 0, "malformed separator");
|
||||
Require(CpuTopology::MaskFromCpuList("/nonexistent/anyps5/cpus") == 0, "missing file");
|
||||
|
||||
const auto& layout = CpuTopology::Get();
|
||||
Require(layout.process != 0, "process affinity read");
|
||||
Require(!layout.hybrid || (layout.performant & layout.efficient) == 0, "hybrid classes overlap");
|
||||
const std::uint64_t first = layout.process & (~layout.process + 1);
|
||||
Require(CpuTopology::Pin(nullptr, first) == first, "pin to one cpu of the process");
|
||||
cpu_set_t set;
|
||||
CPU_ZERO(&set);
|
||||
Require(pthread_getaffinity_np(pthread_self(), sizeof(set), &set) == 0, "read thread affinity");
|
||||
Require(CPU_COUNT(&set) == 1 && CPU_ISSET(static_cast<unsigned>(__builtin_ctzll(first)), &set), "thread runs on the pinned cpu only");
|
||||
Require(CpuTopology::Pin(nullptr, ~layout.process) == 0, "mask outside the process is not applied");
|
||||
int dummy = 0;
|
||||
Require(CpuTopology::Pin(&dummy, first) == 0, "other threads are not pinned by handle");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user