Use new method to set affinity.

This commit is contained in:
XMRig
2018-04-13 09:27:37 +07:00
parent c1800094d0
commit 9ce9147dad
16 changed files with 82 additions and 82 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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; }

View File

@ -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
}
}

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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;
}

View File

@ -25,20 +25,26 @@
#define __PLATFORM_H__
#include <stdint.h>
#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;
};

View File

@ -22,6 +22,8 @@
*/
#include <mach/thread_act.h>
#include <mach/thread_policy.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
@ -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<integer_t>(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();
}
}

View File

@ -21,6 +21,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef __FreeBSD__
# include <sys/types.h>
# include <sys/param.h>
# include <sys/cpuset.h>
# include <pthread_np.h>
#endif
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
@ -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();
}
}

View File

@ -27,9 +27,11 @@
#include <uv.h>
#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) {

View File

@ -73,7 +73,6 @@ xmrig::Controller::Controller()
xmrig::Controller::~Controller()
{
ConfigLoader::release();
Platform::release();
delete d_ptr;
}

View File

@ -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),

View File

@ -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;

View File

@ -42,11 +42,11 @@ Worker::Worker(Handle *handle) :
m_sequence(0),
m_thread(static_cast<xmrig::CpuThread *>(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);
}

View File

@ -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);
}