diff --git a/src/App_unix.cpp b/src/App_unix.cpp index fdb2b124a..f739e42e7 100644 --- a/src/App_unix.cpp +++ b/src/App_unix.cpp @@ -31,7 +31,6 @@ #include "App.h" #include "core/Config.h" #include "core/Controller.h" -#include "Cpu.h" #include "log/Log.h" @@ -39,11 +38,6 @@ void App::background() { signal(SIGPIPE, SIG_IGN); - const int64_t affinity = m_controller->config()->affinity(); - if (affinity != -1L) { - Cpu::setAffinity(-1, affinity); - } - if (!m_controller->config()->isBackground()) { return; } diff --git a/src/App_win.cpp b/src/App_win.cpp index b3a2c4cf3..9b923870e 100644 --- a/src/App_win.cpp +++ b/src/App_win.cpp @@ -27,18 +27,12 @@ #include "App.h" -#include "Cpu.h" #include "core/Controller.h" #include "core/Config.h" void App::background() { - const int64_t affinity = m_controller->config()->affinity(); - if (affinity != -1L) { - Cpu::setAffinity(-1, affinity); - } - if (!m_controller->config()->isBackground()) { return; } diff --git a/src/Cpu.h b/src/Cpu.h index e739ccce4..97e593ed8 100644 --- a/src/Cpu.h +++ b/src/Cpu.h @@ -42,7 +42,6 @@ public: static int optimalThreadsCount(xmrig::Algo algo, bool doubleHash, int maxCpuUsage); static void init(); - static void setAffinity(int id, uint64_t mask); static inline bool hasAES() { return (m_flags & AES) != 0; } static inline bool isX64() { return (m_flags & X86_64) != 0; } diff --git a/src/Cpu_unix.cpp b/src/Cpu_unix.cpp index 9a13e7a52..b895c8979 100644 --- a/src/Cpu_unix.cpp +++ b/src/Cpu_unix.cpp @@ -52,28 +52,3 @@ void Cpu::init() initCommon(); } - - -void Cpu::setAffinity(int id, uint64_t mask) -{ - cpu_set_t set; - CPU_ZERO(&set); - - for (int i = 0; i < m_totalThreads; i++) { - if (mask & (1UL << i)) { - CPU_SET(i, &set); - } - } - - if (id == -1) { -# ifndef __FreeBSD__ - sched_setaffinity(0, sizeof(&set), &set); -# endif - } else { -# ifndef __ANDROID__ - pthread_setaffinity_np(pthread_self(), sizeof(&set), &set); -# else - sched_setaffinity(gettid(), sizeof(&set), &set); -# endif - } -} diff --git a/src/Cpu_win.cpp b/src/Cpu_win.cpp index 13113a178..7258f7266 100644 --- a/src/Cpu_win.cpp +++ b/src/Cpu_win.cpp @@ -39,14 +39,3 @@ void Cpu::init() initCommon(); } - - -void Cpu::setAffinity(int id, uint64_t mask) -{ - if (id == -1) { - SetProcessAffinityMask(GetCurrentProcess(), mask); - } - else { - SetThreadAffinityMask(GetCurrentThread(), mask); - } -} diff --git a/src/Mem_unix.cpp b/src/Mem_unix.cpp index 0dd833d7f..df7aaad2e 100644 --- a/src/Mem_unix.cpp +++ b/src/Mem_unix.cpp @@ -34,10 +34,10 @@ #endif +#include "common/xmrig.h" #include "crypto/CryptoNight.h" #include "log/Log.h" #include "Mem.h" -#include "xmrig.h" bool Mem::allocate(xmrig::Algo algo, int threads, bool doubleHash, bool enabled) diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index 4ddb14296..52b55987f 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -29,16 +29,16 @@ #include "Platform.h" -char *Platform::m_defaultConfigName = nullptr; -char *Platform::m_userAgent = nullptr; +char Platform::m_defaultConfigName[520] = { 0 }; +xmrig::c_str Platform::m_userAgent; const char *Platform::defaultConfigName() { size_t size = 520; - if (m_defaultConfigName == nullptr) { - m_defaultConfigName = new char[size]; + if (*m_defaultConfigName) { + return m_defaultConfigName; } if (uv_exepath(m_defaultConfigName, &size) < 0) { @@ -58,5 +58,6 @@ const char *Platform::defaultConfigName() } } + *m_defaultConfigName = '\0'; return nullptr; } diff --git a/src/common/Platform.h b/src/common/Platform.h index 87c8cc4db..8704604a8 100644 --- a/src/common/Platform.h +++ b/src/common/Platform.h @@ -25,20 +25,26 @@ #define __PLATFORM_H__ +#include + + +#include "common/utils/c_str.h" + + class Platform { public: + static bool setThreadAffinity(uint64_t cpu_id); static const char *defaultConfigName(); static void init(const char *userAgent); - static void release(); static void setProcessPriority(int priority); static void setThreadPriority(int priority); - static inline const char *userAgent() { return m_userAgent; } + static inline const char *userAgent() { return m_userAgent.data(); } private: - static char *m_defaultConfigName; - static char *m_userAgent; + static char m_defaultConfigName[520]; + static xmrig::c_str m_userAgent; }; diff --git a/src/common/Platform_mac.cpp b/src/common/Platform_mac.cpp index ba541f1d0..b8181cc49 100644 --- a/src/common/Platform_mac.cpp +++ b/src/common/Platform_mac.cpp @@ -22,6 +22,8 @@ */ +#include +#include #include #include #include @@ -53,15 +55,24 @@ static inline char *createUserAgent() } -void Platform::init(const char *userAgent) +bool Platform::setThreadAffinity(uint64_t cpu_id) { - m_userAgent = userAgent ? strdup(userAgent) : createUserAgent(); + thread_port_t mach_thread; + thread_affinity_policy_data_t policy = { static_cast(cpu_id) }; + mach_thread = pthread_mach_thread_np(pthread_self()); + + return thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS; } -void Platform::release() +void Platform::init(const char *userAgent) { - delete [] m_userAgent; + if (userAgent) { + m_userAgent = userAgent; + } + else { + m_userAgent = createUserAgent(); + } } diff --git a/src/common/Platform_unix.cpp b/src/common/Platform_unix.cpp index c05893072..624594e9b 100644 --- a/src/common/Platform_unix.cpp +++ b/src/common/Platform_unix.cpp @@ -21,6 +21,14 @@ * along with this program. If not, see . */ +#ifdef __FreeBSD__ +# include +# include +# include +# include +#endif + + #include #include #include @@ -37,6 +45,11 @@ #endif +#ifdef __FreeBSD__ +typedef cpuset_t cpu_set_t; +#endif + + static inline char *createUserAgent() { const size_t max = 160; @@ -63,15 +76,28 @@ static inline char *createUserAgent() } -void Platform::init(const char *userAgent) +bool Platform::setThreadAffinity(uint64_t cpu_id) { - m_userAgent = userAgent ? strdup(userAgent) : createUserAgent(); + cpu_set_t mn; + CPU_ZERO(&mn); + CPU_SET(cpu_id, &mn); + +# ifndef __ANDROID__ + return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &mn) == 0; +# else + return sched_setaffinity(gettid(), sizeof(cpu_set_t), &mn) == 0; +# endif } -void Platform::release() +void Platform::init(const char *userAgent) { - delete [] m_userAgent; + if (userAgent) { + m_userAgent = userAgent; + } + else { + m_userAgent = createUserAgent(); + } } diff --git a/src/common/Platform_win.cpp b/src/common/Platform_win.cpp index 880bdd982..47f418671 100644 --- a/src/common/Platform_win.cpp +++ b/src/common/Platform_win.cpp @@ -27,9 +27,11 @@ #include +#include "log/Log.h" #include "Platform.h" #include "version.h" + #ifdef XMRIG_NVIDIA_PROJECT # include "nvidia/cryptonight.h" #endif @@ -82,16 +84,24 @@ static inline char *createUserAgent() } -void Platform::init(const char *userAgent) +bool Platform::setThreadAffinity(uint64_t cpu_id) { - m_userAgent = userAgent ? strdup(userAgent) : createUserAgent(); + if (cpu_id >= 64) { + LOG_ERR("Unable to set affinity. Windows supports only affinity up to 63."); + } + + return SetThreadAffinityMask(GetCurrentThread(), 1ULL << cpu_id) != 0; } -void Platform::release() +void Platform::init(const char *userAgent) { - delete [] m_defaultConfigName; - delete [] m_userAgent; + if (userAgent) { + m_userAgent = userAgent; + } + else { + m_userAgent = createUserAgent(); + } } @@ -131,7 +141,6 @@ void Platform::setProcessPriority(int priority) } - void Platform::setThreadPriority(int priority) { if (priority == -1) { diff --git a/src/core/Controller.cpp b/src/core/Controller.cpp index f120c0c12..5f0a9bb3d 100644 --- a/src/core/Controller.cpp +++ b/src/core/Controller.cpp @@ -73,7 +73,6 @@ xmrig::Controller::Controller() xmrig::Controller::~Controller() { ConfigLoader::release(); - Platform::release(); delete d_ptr; } diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp index 01d032e9b..29f57fb25 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/Handle.cpp @@ -25,8 +25,7 @@ #include "workers/Handle.h" -Handle::Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays, int64_t affinity) : - m_affinity(affinity), +Handle::Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays) : m_worker(nullptr), m_totalThreads(totalThreads), m_totalWays(totalWays), diff --git a/src/workers/Handle.h b/src/workers/Handle.h index 8a64922a5..b3a7c76f7 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -38,11 +38,10 @@ class IWorker; class Handle { public: - Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays, int64_t affinity); + Handle(xmrig::IThread *config, size_t totalThreads, size_t totalWays); void join(); void start(void (*callback) (void *)); - inline int64_t affinity() const { return m_affinity; } inline IWorker *worker() const { return m_worker; } inline size_t threadId() const { return m_config->index(); } inline size_t totalThreads() const { return m_totalThreads; } @@ -51,7 +50,6 @@ public: inline xmrig::IThread *config() const { return m_config; } private: - int64_t m_affinity; IWorker *m_worker; size_t m_totalThreads; size_t m_totalWays; diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 30305ac6e..594343904 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -42,11 +42,11 @@ Worker::Worker(Handle *handle) : m_sequence(0), m_thread(static_cast(handle->config())) { - if (Cpu::threads() > 1 && handle->affinity() != -1L) { - Cpu::setAffinity(m_id, handle->affinity()); + if (Cpu::threads() > 1 && m_thread->affinity() != -1L) { + Platform::setThreadAffinity(m_thread->affinity()); } - Platform::setThreadPriority(handle->config()->priority()); + Platform::setThreadPriority(m_thread->priority()); m_ctx = Mem::create(m_id); } diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 38965d8ad..00941c7be 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -130,7 +130,7 @@ void Workers::start(xmrig::Controller *controller) uv_timer_start(&m_timer, Workers::onTick, 500, 500); for (xmrig::IThread *thread : threads) { - Handle *handle = new Handle(thread, threads.size(), totalWays, controller->config()->affinity()); + Handle *handle = new Handle(thread, threads.size(), totalWays); m_workers.push_back(handle); handle->start(Workers::onReady); }