More correct CPU affinity support for macOS.

This commit is contained in:
XMRig 2020-12-13 01:20:31 +07:00
parent 87fafcf91b
commit a36fb7e728
No known key found for this signature in database
GPG key ID: 446A53638BE94409
7 changed files with 50 additions and 42 deletions

View file

@ -1,12 +1,6 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -27,12 +21,17 @@
#include "backend/common/interfaces/IWorker.h" #include "backend/common/interfaces/IWorker.h"
#include "base/tools/Object.h"
#include <thread> #include <thread>
#ifdef XMRIG_OS_APPLE
# include <pthread.h>
# include <mach/thread_act.h>
#endif
namespace xmrig { namespace xmrig {
@ -46,21 +45,48 @@ public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Thread) XMRIG_DISABLE_COPY_MOVE_DEFAULT(Thread)
inline Thread(IBackend *backend, size_t id, const T &config) : m_id(id), m_config(config), m_backend(backend) {} inline Thread(IBackend *backend, size_t id, const T &config) : m_id(id), m_config(config), m_backend(backend) {}
# ifdef XMRIG_OS_APPLE
inline ~Thread() { pthread_join(m_thread, nullptr); delete m_worker; }
inline void start(void *(*callback)(void *))
{
if (m_config.affinity >= 0) {
pthread_create_suspended_np(&m_thread, nullptr, callback, this);
mach_port_t mach_thread = pthread_mach_thread_np(m_thread);
thread_affinity_policy_data_t policy = { static_cast<integer_t>(m_config.affinity + 1) };
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, reinterpret_cast<thread_policy_t>(&policy), THREAD_AFFINITY_POLICY_COUNT);
thread_resume(mach_thread);
}
else {
pthread_create(&m_thread, nullptr, callback, this);
}
}
# else
inline ~Thread() { m_thread.join(); delete m_worker; } inline ~Thread() { m_thread.join(); delete m_worker; }
inline void start(void *(*callback)(void *)) { m_thread = std::thread(callback, this); }
# endif
inline const T &config() const { return m_config; } inline const T &config() const { return m_config; }
inline IBackend *backend() const { return m_backend; } inline IBackend *backend() const { return m_backend; }
inline IWorker *worker() const { return m_worker; } inline IWorker *worker() const { return m_worker; }
inline size_t id() const { return m_id; } inline size_t id() const { return m_id; }
inline void setWorker(IWorker *worker) { m_worker = worker; } inline void setWorker(IWorker *worker) { m_worker = worker; }
inline void start(void (*callback) (void *)) { m_thread = std::thread(callback, this); }
private: private:
const size_t m_id = 0; const size_t m_id = 0;
const T m_config; const T m_config;
IBackend *m_backend; IBackend *m_backend;
IWorker *m_worker = nullptr; IWorker *m_worker = nullptr;
#ifdef XMRIG_OS_APPLE
pthread_t m_thread{};
# else
std::thread m_thread; std::thread m_thread;
# endif
}; };

View file

@ -174,7 +174,7 @@ xmrig::IWorker *xmrig::Workers<T>::create(Thread<T> *)
template<class T> template<class T>
void xmrig::Workers<T>::onReady(void *arg) void *xmrig::Workers<T>::onReady(void *arg)
{ {
auto handle = static_cast<Thread<T>* >(arg); auto handle = static_cast<Thread<T>* >(arg);
@ -187,13 +187,15 @@ void xmrig::Workers<T>::onReady(void *arg)
handle->backend()->start(worker, false); handle->backend()->start(worker, false);
delete worker; delete worker;
return; return nullptr;
} }
assert(handle->backend() != nullptr); assert(handle->backend() != nullptr);
handle->setWorker(worker); handle->setWorker(worker);
handle->backend()->start(worker, true); handle->backend()->start(worker, true);
return nullptr;
} }

View file

@ -65,7 +65,7 @@ public:
private: private:
static IWorker *create(Thread<T> *handle); static IWorker *create(Thread<T> *handle);
static void onReady(void *arg); static void *onReady(void *arg);
void start(const std::vector<T> &data, bool sleep); void start(const std::vector<T> &data, bool sleep);

View file

@ -1,13 +1,6 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -32,6 +25,7 @@ xmrig::CudaLaunchData::CudaLaunchData(const Miner *miner, const Algorithm &algor
algorithm(algorithm), algorithm(algorithm),
device(device), device(device),
thread(thread), thread(thread),
affinity(thread.affinity()),
miner(miner) miner(miner)
{ {
} }

View file

@ -1,13 +1,6 @@
/* XMRig /* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com> * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org> * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -56,6 +49,7 @@ public:
const Algorithm algorithm; const Algorithm algorithm;
const CudaDevice &device; const CudaDevice &device;
const CudaThread thread; const CudaThread thread;
const int64_t affinity;
const Miner *miner; const Miner *miner;
const uint32_t benchSize = 0; const uint32_t benchSize = 0;
}; };

View file

@ -26,6 +26,7 @@
#include <thread> #include <thread>
#ifndef XMRIG_OS_APPLE
bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id)
{ {
auto cpu = static_cast<HwlocCpuInfo *>(Cpu::info()); auto cpu = static_cast<HwlocCpuInfo *>(Cpu::info());
@ -44,3 +45,4 @@ bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id)
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
return result; return result;
} }
#endif

View file

@ -19,8 +19,6 @@
#include <IOKit/IOKitLib.h> #include <IOKit/IOKitLib.h>
#include <IOKit/ps/IOPowerSources.h> #include <IOKit/ps/IOPowerSources.h>
#include <mach/thread_act.h>
#include <mach/thread_policy.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/resource.h> #include <sys/resource.h>
@ -50,18 +48,10 @@ char *xmrig::Platform::createUserAgent()
} }
#ifndef XMRIG_FEATURE_HWLOC
bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id) bool xmrig::Platform::setThreadAffinity(uint64_t cpu_id)
{ {
thread_port_t mach_thread; return true;
thread_affinity_policy_data_t policy = { static_cast<integer_t>(cpu_id) };
mach_thread = pthread_mach_thread_np(pthread_self());
const bool result = (thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 1) == KERN_SUCCESS);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return result;
} }
#endif
void xmrig::Platform::setProcessPriority(int) void xmrig::Platform::setProcessPriority(int)