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

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