Preparation for #1.4.0 (#30)

- Fixed CPU affinity on Windows for NUMA and CPUs with lot of cores
- Implemented per thread configurable Multihash mode (double, triple, quadruple, quintuple)
- Rebased from XMRig 2.4.4
This commit is contained in:
Ben Gräf 2018-01-19 19:42:06 +01:00 committed by GitHub
parent 5f8ea98764
commit acf27e9341
41 changed files with 2575 additions and 1104 deletions

View file

@ -5,6 +5,7 @@
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
* Copyright 2018 Sebastian Stolzenberg <https://github.com/sebastianstolzenberg>
*
*
* This program is free software: you can redistribute it and/or modify
@ -22,60 +23,116 @@
*/
#include <cmath>
#include <cstring>
#include <algorithm>
#include <memory>
#include <libcpuid.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include "Cpu.h"
#include "CpuImpl.h"
bool Cpu::m_l2_exclusive = false;
char Cpu::m_brand[64] = { 0 };
int Cpu::m_flags = 0;
int Cpu::m_l2_cache = 0;
int Cpu::m_l3_cache = 0;
int Cpu::m_sockets = 1;
int Cpu::m_totalCores = 0;
int Cpu::m_totalThreads = 0;
int Cpu::optimalThreadsCount(int algo, bool doubleHash, int maxCpuUsage)
CpuImpl& CpuImpl::instance()
{
if (m_totalThreads == 1) {
return 1;
static CpuImpl cpu;
return cpu;
}
CpuImpl::CpuImpl()
: m_l2_exclusive(false)
, m_brand{ 0 }
, m_flags(0)
, m_l2_cache(0)
, m_l3_cache(0)
, m_sockets(1)
, m_totalCores(0)
, m_totalThreads(0)
{
}
void CpuImpl::optimizeParameters(size_t& threadsCount, size_t& hashFactor,
Options::Algo algo, size_t maxCpuUsage, bool safeMode)
{
// limits hashfactor to maximum possible value defined by compiler flag
hashFactor = std::min(hashFactor, static_cast<size_t>(MAX_NUM_HASH_BLOCKS));
if (!safeMode && threadsCount > 0 && hashFactor > 0)
{
// all parameters have been set manually and safe mode is off ... no optimization necessary
return;
}
int cache = 0;
size_t cache = availableCache();
size_t algoBlockSize;
switch (algo) {
case Options::ALGO_CRYPTONIGHT_LITE:
algoBlockSize = 1024;
break;
case Options::ALGO_CRYPTONIGHT:
default:
algoBlockSize = 2048;
break;
}
size_t maximumReasonableFactor = std::max(cache / algoBlockSize, static_cast<size_t>(1ul));
size_t maximumReasonableThreadCount = std::min(maximumReasonableFactor, m_totalThreads);
size_t maximumReasonableHashFactor = std::min(maximumReasonableFactor, static_cast<size_t>(MAX_NUM_HASH_BLOCKS));
if (safeMode) {
if (threadsCount > maximumReasonableThreadCount) {
threadsCount = maximumReasonableThreadCount;
}
if (hashFactor > maximumReasonableFactor / threadsCount) {
hashFactor = std::min(maximumReasonableFactor / threadsCount, maximumReasonableHashFactor);
hashFactor = std::max(hashFactor, static_cast<size_t>(1));
}
}
if (threadsCount == 0) {
if (hashFactor == 0) {
threadsCount = maximumReasonableThreadCount;
}
else {
threadsCount = std::min(maximumReasonableThreadCount,
maximumReasonableFactor / hashFactor);
}
if (maxCpuUsage < 100)
{
threadsCount = std::min(threadsCount, m_totalThreads * maxCpuUsage / 100);
}
threadsCount = std::max(threadsCount, static_cast<size_t>(1));
}
if (hashFactor == 0) {
hashFactor = std::min(maximumReasonableHashFactor, maximumReasonableFactor / threadsCount);
hashFactor = std::max(hashFactor, static_cast<size_t>(1));
}
}
bool CpuImpl::hasAES()
{
return (m_flags & Cpu::AES) != 0;
}
bool CpuImpl::isX64()
{
return (m_flags & Cpu::X86_64) != 0;
}
size_t CpuImpl::availableCache()
{
size_t cache = 0;
if (m_l3_cache) {
cache = m_l2_exclusive ? (m_l2_cache + m_l3_cache) : m_l3_cache;
}
else {
cache = m_l2_cache;
}
int count = 0;
const int size = (algo ? 1024 : 2048) * (doubleHash ? 2 : 1);
if (cache) {
count = cache / size;
}
else {
count = m_totalThreads / 2;
}
if (count > m_totalThreads) {
count = m_totalThreads;
}
if (((float) count / m_totalThreads * 100) > maxCpuUsage) {
count = (int) ceil((float) m_totalThreads * (maxCpuUsage / 100.0));
}
return count < 1 ? 1 : count;
return cache;
}
void Cpu::initCommon()
void CpuImpl::initCommon()
{
struct cpu_raw_data_t raw = { 0 };
struct cpu_id_t data = { 0 };
@ -105,14 +162,75 @@ void Cpu::initCommon()
}
# if defined(__x86_64__) || defined(_M_AMD64)
m_flags |= X86_64;
m_flags |= Cpu::X86_64;
# endif
if (data.flags[CPU_FEATURE_AES]) {
m_flags |= AES;
m_flags |= Cpu::AES;
}
if (data.flags[CPU_FEATURE_BMI2]) {
m_flags |= BMI2;
m_flags |= Cpu::BMI2;
}
}
void Cpu::init()
{
CpuImpl::instance().init();
}
void Cpu::optimizeParameters(size_t& threadsCount, size_t& hashFactor, Options::Algo algo,
size_t maxCpuUsage, bool safeMode)
{
CpuImpl::instance().optimizeParameters(threadsCount, hashFactor, algo, maxCpuUsage, safeMode);
}
void Cpu::setAffinity(int id, uint64_t mask)
{
CpuImpl::instance().setAffinity(id, mask);
}
bool Cpu::hasAES()
{
return CpuImpl::instance().hasAES();
}
bool Cpu::isX64()
{
return CpuImpl::instance().isX64();
}
const char* Cpu::brand()
{
return CpuImpl::instance().brand();
}
size_t Cpu::cores()
{
return CpuImpl::instance().cores();
}
size_t Cpu::l2()
{
return CpuImpl::instance().l2();
}
size_t Cpu::l3()
{
return CpuImpl::instance().l3();
}
size_t Cpu::sockets()
{
return CpuImpl::instance().sockets();
}
size_t Cpu::threads()
{
return CpuImpl::instance().threads();
}
size_t Cpu::availableCache()
{
return CpuImpl::instance().availableCache();
}