Skip to content
Snippets Groups Projects
Commit c7c58243 authored by fireice-uk's avatar fireice-uk Committed by GitHub
Browse files

Merge pull request #43 from fireice-uk/fix-aff-alt-c

Affinity fix version C
parents dcd176ad 4253e7a0
No related branches found
No related tags found
No related merge requests found
...@@ -50,22 +50,8 @@ ...@@ -50,22 +50,8 @@
#include <thread> #include <thread>
#include <bitset> #include <bitset>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
namespace xmrstak
{
namespace cpu
{
void minethd::thd_setaffinity(std::thread::native_handle_type h, uint64_t cpu_id)
{
SetThreadAffinityMask(h, 1ULL << cpu_id);
}
} // namespace cpu
} // namespace xmrstak
#else #else
#include <pthread.h> #include <pthread.h>
...@@ -75,44 +61,37 @@ void minethd::thd_setaffinity(std::thread::native_handle_type h, uint64_t cpu_id ...@@ -75,44 +61,37 @@ void minethd::thd_setaffinity(std::thread::native_handle_type h, uint64_t cpu_id
#define SYSCTL_CORE_COUNT "machdep.cpu.core_count" #define SYSCTL_CORE_COUNT "machdep.cpu.core_count"
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
#include <pthread_np.h> #include <pthread_np.h>
#endif #endif //__APPLE__
#endif //_WIN32
namespace xmrstak namespace xmrstak
{ {
namespace cpu namespace cpu
{ {
void minethd::thd_setaffinity(std::thread::native_handle_type h, uint64_t cpu_id) bool minethd::thd_setaffinity(std::thread::native_handle_type h, uint64_t cpu_id)
{ {
#if defined(__APPLE__) #if defined(_WIN32)
return SetThreadAffinityMask(h, 1ULL << cpu_id) != 0;
#elif defined(__APPLE__)
thread_port_t mach_thread; thread_port_t mach_thread;
thread_affinity_policy_data_t policy = { static_cast<integer_t>(cpu_id) }; thread_affinity_policy_data_t policy = { static_cast<integer_t>(cpu_id) };
mach_thread = pthread_mach_thread_np(h); mach_thread = pthread_mach_thread_np(h);
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1); return thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS;
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
cpuset_t mn; cpuset_t mn;
CPU_ZERO(&mn); CPU_ZERO(&mn);
CPU_SET(cpu_id, &mn); CPU_SET(cpu_id, &mn);
pthread_setaffinity_np(h, sizeof(cpuset_t), &mn); return pthread_setaffinity_np(h, sizeof(cpuset_t), &mn) == 0;
#else #else
cpu_set_t mn; cpu_set_t mn;
CPU_ZERO(&mn); CPU_ZERO(&mn);
CPU_SET(cpu_id, &mn); CPU_SET(cpu_id, &mn);
pthread_setaffinity_np(h, sizeof(cpu_set_t), &mn); return pthread_setaffinity_np(h, sizeof(cpu_set_t), &mn) == 0;
#endif #endif
} }
} // namespace cpu
} // namespace xmrstak
#endif // _WIN32
namespace xmrstak
{
namespace cpu
{
minethd::minethd(miner_work& pWork, size_t iNo, bool double_work, bool no_prefetch, int64_t affinity) minethd::minethd(miner_work& pWork, size_t iNo, bool double_work, bool no_prefetch, int64_t affinity)
{ {
oWork = pWork; oWork = pWork;
...@@ -122,11 +101,17 @@ minethd::minethd(miner_work& pWork, size_t iNo, bool double_work, bool no_prefet ...@@ -122,11 +101,17 @@ minethd::minethd(miner_work& pWork, size_t iNo, bool double_work, bool no_prefet
bNoPrefetch = no_prefetch; bNoPrefetch = no_prefetch;
this->affinity = affinity; this->affinity = affinity;
std::lock_guard<std::mutex> lock(work_thd_mtx); std::future<void> order_guard = order_fix.get_future();
if(double_work) if(double_work)
oWorkThd = std::thread(&minethd::double_work_main, this); oWorkThd = std::thread(&minethd::double_work_main, this);
else else
oWorkThd = std::thread(&minethd::work_main, this); oWorkThd = std::thread(&minethd::work_main, this);
order_guard.wait();
if(!thd_setaffinity(oWorkThd.native_handle(), affinity))
printer::inst()->print_msg(L1, "WARNING setting affinity failed.");
} }
cryptonight_ctx* minethd::minethd_alloc_ctx() cryptonight_ctx* minethd::minethd_alloc_ctx()
...@@ -279,7 +264,13 @@ std::vector<iBackend*> minethd::thread_starter(uint32_t threadOffset, miner_work ...@@ -279,7 +264,13 @@ std::vector<iBackend*> minethd::thread_starter(uint32_t threadOffset, miner_work
pvThreads.push_back(thd); pvThreads.push_back(thd);
if(cfg.iCpuAff >= 0) if(cfg.iCpuAff >= 0)
{
#if defined(__APPLE__)
printer::inst()->print_msg(L1, "WARNING on MacOS thread affinity is only advisory.");
#endif
printer::inst()->print_msg(L1, "Starting %s thread, affinity: %d.", cfg.bDoubleMode ? "double" : "single", (int)cfg.iCpuAff); printer::inst()->print_msg(L1, "Starting %s thread, affinity: %d.", cfg.bDoubleMode ? "double" : "single", (int)cfg.iCpuAff);
}
else else
printer::inst()->print_msg(L1, "Starting %s thread, no affinity.", cfg.bDoubleMode ? "double" : "single"); printer::inst()->print_msg(L1, "Starting %s thread, no affinity.", cfg.bDoubleMode ? "double" : "single");
} }
...@@ -315,24 +306,12 @@ minethd::cn_hash_fun minethd::func_selector(bool bHaveAes, bool bNoPrefetch) ...@@ -315,24 +306,12 @@ minethd::cn_hash_fun minethd::func_selector(bool bHaveAes, bool bNoPrefetch)
return func_table[digit.to_ulong()]; return func_table[digit.to_ulong()];
} }
void minethd::pin_thd_affinity()
{
//Lock is needed because we need to use oWorkThd
std::lock_guard<std::mutex> lock(work_thd_mtx);
// pin memory to NUMA node
bindMemoryToNUMANode(affinity);
#if defined(__APPLE__)
printer::inst()->print_msg(L1, "WARNING on MacOS thread affinity is only advisory.");
#endif
thd_setaffinity(oWorkThd.native_handle(), affinity);
}
void minethd::work_main() void minethd::work_main()
{ {
if(affinity >= 0) //-1 means no affinity if(affinity >= 0) //-1 means no affinity
pin_thd_affinity(); bindMemoryToNUMANode(affinity);
order_fix.set_value();
cn_hash_fun hash_fun; cn_hash_fun hash_fun;
cryptonight_ctx* ctx; cryptonight_ctx* ctx;
...@@ -429,7 +408,7 @@ uint32_t* minethd::prep_double_work(uint8_t bDoubleWorkBlob[sizeof(miner_work::b ...@@ -429,7 +408,7 @@ uint32_t* minethd::prep_double_work(uint8_t bDoubleWorkBlob[sizeof(miner_work::b
void minethd::double_work_main() void minethd::double_work_main()
{ {
if(affinity >= 0) //-1 means no affinity if(affinity >= 0) //-1 means no affinity
pin_thd_affinity(); bindMemoryToNUMANode(affinity);
cn_hash_fun_dbl hash_fun; cn_hash_fun_dbl hash_fun;
cryptonight_ctx* ctx0; cryptonight_ctx* ctx0;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <atomic> #include <atomic>
#include <mutex> #include <future>
namespace xmrstak namespace xmrstak
{ {
...@@ -24,7 +24,7 @@ public: ...@@ -24,7 +24,7 @@ public:
typedef void (*cn_hash_fun)(const void*, size_t, void*, cryptonight_ctx*); typedef void (*cn_hash_fun)(const void*, size_t, void*, cryptonight_ctx*);
static cn_hash_fun func_selector(bool bHaveAes, bool bNoPrefetch); static cn_hash_fun func_selector(bool bHaveAes, bool bNoPrefetch);
static void thd_setaffinity(std::thread::native_handle_type h, uint64_t cpu_id); static bool thd_setaffinity(std::thread::native_handle_type h, uint64_t cpu_id);
static cryptonight_ctx* minethd_alloc_ctx(); static cryptonight_ctx* minethd_alloc_ctx();
...@@ -45,9 +45,7 @@ private: ...@@ -45,9 +45,7 @@ private:
static miner_work oGlobalWork; static miner_work oGlobalWork;
miner_work oWork; miner_work oWork;
void pin_thd_affinity(); std::promise<void> order_fix;
// Held by the creating context to prevent a race cond with oWorkThd = std::thread(...)
std::mutex work_thd_mtx;
std::thread oWorkThd; std::thread oWorkThd;
int64_t affinity; int64_t affinity;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment