CPU autoconfig

This commit is contained in:
fireice-uk
2017-04-25 19:50:10 +01:00
parent 37065f425d
commit d5b02fa0c3
5 changed files with 210 additions and 50 deletions
+49 -39
View File
@@ -45,7 +45,7 @@ using namespace rapidjson;
/*
* This enum needs to match index in oConfigValues, otherwise we will get a runtime error
*/
enum configEnum { iCpuThreadNum, aCpuThreadsConf, sUseSlowMem, bNiceHashMode,
enum configEnum { aCpuThreadsConf, sUseSlowMem, bNiceHashMode,
bTlsMode, bTlsSecureAlgo, sTlsFingerprint, sPoolAddr, sWalletAddr, sPoolPwd,
iCallTimeout, iNetRetry, iGiveUpLimit, iVerboseLevel, iAutohashTime,
sOutputFile, iHttpdPort, bPreferIpv4 };
@@ -56,10 +56,10 @@ struct configVal {
Type iType;
};
//Same order as in configEnum, as per comment above
// Same order as in configEnum, as per comment above
// kNullType means any type
configVal oConfigValues[] = {
{ iCpuThreadNum, "cpu_thread_num", kNumberType },
{ aCpuThreadsConf, "cpu_threads_conf", kArrayType },
{ aCpuThreadsConf, "cpu_threads_conf", kNullType },
{ sUseSlowMem, "use_slow_memory", kStringType },
{ bNiceHashMode, "nicehash_nonce", kTrueType },
{ bTlsMode, "use_tls", kTrueType },
@@ -84,6 +84,8 @@ inline bool checkType(Type have, Type want)
{
if(want == have)
return true;
else if(want == kNullType)
return true;
else if(want == kTrueType && have == kFalseType)
return true;
else if(want == kFalseType && have == kTrueType)
@@ -111,6 +113,9 @@ jconf::jconf()
bool jconf::GetThreadConfig(size_t id, thd_cfg &cfg)
{
if(!prv->configValues[aCpuThreadsConf]->IsArray())
return false;
if(id >= prv->configValues[aCpuThreadsConf]->Size())
return false;
@@ -206,7 +211,15 @@ bool jconf::PreferIpv4()
size_t jconf::GetThreadCount()
{
return prv->configValues[aCpuThreadsConf]->Size();
if(prv->configValues[aCpuThreadsConf]->IsArray())
return prv->configValues[aCpuThreadsConf]->Size();
else
return 0;
}
bool jconf::NeedsAutoconf()
{
return !prv->configValues[aCpuThreadsConf]->IsArray();
}
uint64_t jconf::GetCallTimeout()
@@ -249,38 +262,34 @@ const char* jconf::GetOutputFile()
return prv->configValues[sOutputFile]->GetString();
}
void jconf::cpuid(uint32_t eax, int32_t ecx, int32_t val[4])
{
memset(val, 0, sizeof(int32_t)*4);
#ifdef _WIN32
__cpuidex(val, eax, ecx);
#else
__cpuid_count(eax, ecx, val[0], val[1], val[2], val[3]);
#endif
}
bool jconf::check_cpu_features()
{
constexpr int AESNI_BIT = 1 << 25;
constexpr int SSE2_BIT = 1 << 26;
constexpr int BMI2_BIT = 1 << 8;
int cpu_info[4];
int32_t cpu_info[4];
bool bHaveSse2;
#ifdef _WIN32
__cpuid(cpu_info, 1);
#else
__cpuid(1, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
#endif
cpuid(1, 0, cpu_info);
bHaveAes = (cpu_info[2] & AESNI_BIT) != 0;
bHaveSse2 = (cpu_info[3] & SSE2_BIT) != 0;
#ifdef _WIN32
__cpuidex(cpu_info, 7, 0);
#else
__cpuid_count(7, 0, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
#endif
cpuid(7, 0, cpu_info);
bHaveBmi2 = (cpu_info[1] & BMI2_BIT) != 0;
if(!bHaveAes)
printer::inst()->print_msg(L0, "Your CPU doesn't support hardware AES. Don't expect high hashrates.");
if(bHaveBmi2)
printer::inst()->print_msg(L0, "CPU supports BMI2 instructions. Faster multiplication enabled.");
return bHaveSse2;
}
@@ -384,23 +393,8 @@ bool jconf::parse_config(const char* sFilename)
}
}
size_t n_thd = prv->configValues[aCpuThreadsConf]->Size();
if(prv->configValues[iCpuThreadNum]->GetUint64() != n_thd)
{
printer::inst()->print_msg(L0,
"Invalid config file. Your CPU config array has %llu members, while you want to use %llu threads.",
int_port(n_thd), int_port(prv->configValues[iCpuThreadNum]->GetUint64()));
return false;
}
if(NiceHashMode() && n_thd >= 32)
{
printer::inst()->print_msg(L0, "You need to use less than 32 threads in NiceHash mode.");
return false;
}
thd_cfg c;
for(size_t i=0; i < n_thd; i++)
for(size_t i=0; i < GetThreadCount(); i++)
{
if(!GetThreadConfig(i, c))
{
@@ -409,6 +403,12 @@ bool jconf::parse_config(const char* sFilename)
}
}
if(NiceHashMode() && GetThreadCount() >= 32)
{
printer::inst()->print_msg(L0, "You need to use less than 32 threads in NiceHash mode.");
return false;
}
if(GetSlowMemSetting() == unknown_value)
{
printer::inst()->print_msg(L0,
@@ -457,5 +457,15 @@ bool jconf::parse_config(const char* sFilename)
#endif // _WIN32
printer::inst()->set_verbose_level(prv->configValues[iVerboseLevel]->GetUint64());
if(!NeedsAutoconf())
{
if(!bHaveAes)
printer::inst()->print_msg(L0, "Your CPU doesn't support hardware AES. Don't expect high hashrates.");
if(bHaveBmi2)
printer::inst()->print_msg(L0, "CPU supports BMI2 instructions. Faster multiplication enabled.");
}
return true;
}