Added classes IThread, CpuThread and API endpoint "GET /1/threads".

This commit is contained in:
XMRig 2018-04-01 22:49:21 +07:00
parent 44d56393db
commit a042cbf885
19 changed files with 320 additions and 78 deletions

View file

@ -51,6 +51,7 @@ static const char *algoNames[] = {
xmrig::CommonConfig::CommonConfig() :
m_algorithm(CRYPTONIGHT),
m_adjusted(false),
m_apiIPv6(true),
m_apiRestricted(true),
@ -63,7 +64,6 @@ xmrig::CommonConfig::CommonConfig() :
m_fileName(nullptr),
m_logFile(nullptr),
m_userAgent(nullptr),
m_algorithm(ALGO_CRYPTONIGHT),
m_apiPort(0),
m_donateLevel(kDefaultDonateLevel),
m_printTime(60),
@ -367,7 +367,7 @@ void xmrig::CommonConfig::setAlgo(const char *algo)
if (strcasecmp(algo, "cryptonight-light") == 0) {
fprintf(stderr, "Algorithm \"cryptonight-light\" is deprecated, use \"cryptonight-lite\" instead\n");
m_algorithm = ALGO_CRYPTONIGHT_LITE;
m_algorithm = CRYPTONIGHT_LITE;
return;
}
@ -375,7 +375,7 @@ void xmrig::CommonConfig::setAlgo(const char *algo)
for (size_t i = 0; i < size; i++) {
if (algoNames[i] && strcasecmp(algo, algoNames[i]) == 0) {
m_algorithm = (int) i;
m_algorithm = static_cast<Algo>(i);
break;
}
}

View file

@ -29,6 +29,7 @@
#include "interfaces/IConfig.h"
#include "xmrig.h"
class Url;
@ -45,6 +46,7 @@ public:
const char *algoName() const;
inline Algo algorithm() const { return m_algorithm; }
inline bool isApiIPv6() const { return m_apiIPv6; }
inline bool isApiRestricted() const { return m_apiRestricted; }
inline bool isBackground() const { return m_background; }
@ -55,7 +57,6 @@ public:
inline const char *logFile() const { return m_logFile; }
inline const char *userAgent() const { return m_userAgent; }
inline const std::vector<Url*> &pools() const { return m_pools; }
inline int algorithm() const { return m_algorithm; }
inline int apiPort() const { return m_apiPort; }
inline int donateLevel() const { return m_donateLevel; }
inline int printTime() const { return m_printTime; }
@ -75,6 +76,7 @@ protected:
bool save() override;
void setFileName(const char *fileName) override;
Algo m_algorithm;
bool m_adjusted;
bool m_apiIPv6;
bool m_apiRestricted;
@ -87,7 +89,6 @@ protected:
char *m_fileName;
char *m_logFile;
char *m_userAgent;
int m_algorithm;
int m_apiPort;
int m_donateLevel;
int m_printTime;

View file

@ -34,6 +34,7 @@
#include "rapidjson/document.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/prettywriter.h"
#include "workers/CpuThread.h"
#include "xmrig.h"
@ -41,18 +42,17 @@ static char affinity_tmp[20] = { 0 };
xmrig::Config::Config() : xmrig::CommonConfig(),
m_algoVariant(AV_AUTO),
m_doubleHash(false),
m_dryRun(false),
m_hugePages(true),
m_safe(false),
m_algoVariant(0),
m_maxCpuUsage(75),
m_printTime(60),
m_priority(-1),
m_threads(0),
m_affinity(-1L)
m_affinity(-1L),
m_threadsCount(0)
{
}
@ -135,7 +135,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const
doc.AddMember("retries", retries(), allocator);
doc.AddMember("retry-pause", retryPause(), allocator);
doc.AddMember("safe", m_safe, allocator);
doc.AddMember("threads", threads(), allocator);
doc.AddMember("threads", threadsCount(), allocator);
doc.AddMember("user-agent", userAgent() ? rapidjson::Value(rapidjson::StringRef(userAgent())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator);
# ifdef HAVE_SYSLOG_H
@ -159,20 +159,24 @@ bool xmrig::Config::adjust()
}
m_algoVariant = getAlgoVariant();
if (m_algoVariant == AV2_AESNI_DOUBLE || m_algoVariant == AV4_SOFT_AES_DOUBLE) {
if (m_algoVariant == AV_DOUBLE || m_algoVariant == AV_DOUBLE_SOFT) {
m_doubleHash = true;
}
if (!m_threads) {
m_threads = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage);
if (!m_threadsCount) {
m_threadsCount = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage);
}
else if (m_safe) {
const int count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage);
if (m_threads > count) {
m_threads = count;
const size_t count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage);
if (m_threadsCount > count) {
m_threadsCount = count;
}
}
for (size_t i = 0; i < m_threadsCount; ++i) {
m_threads.push_back(CpuThread::createFromAV(i, m_algorithm, m_algoVariant, m_affinity, m_priority));
}
return true;
}
@ -225,7 +229,7 @@ bool xmrig::Config::parseString(int key, const char *arg)
case xmrig::IConfig::ThreadsKey: /* --threads */
if (strncmp(arg, "all", 3) == 0) {
m_threads = Cpu::threads();
m_threadsCount = Cpu::threads();
return true;
}
@ -275,14 +279,14 @@ bool xmrig::Config::parseInt(int key, int arg)
{
switch (key) {
case xmrig::IConfig::ThreadsKey: /* --threads */
if (m_threads >= 0 && arg < 1024) {
m_threads = arg;
if (m_threadsCount >= 0 && arg < 1024) {
m_threadsCount = arg;
}
break;
case xmrig::IConfig::AVKey: /* --av */
if (arg >= AV0_AUTO && arg < AV_MAX) {
m_algoVariant = arg;
if (arg >= AV_AUTO && arg < AV_MAX) {
m_algoVariant = static_cast<AlgoVariant>(arg);
}
break;
@ -306,20 +310,20 @@ bool xmrig::Config::parseInt(int key, int arg)
}
int xmrig::Config::getAlgoVariant() const
xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const
{
# ifndef XMRIG_NO_AEON
if (m_algorithm == xmrig::ALGO_CRYPTONIGHT_LITE) {
if (m_algorithm == xmrig::CRYPTONIGHT_LITE) {
return getAlgoVariantLite();
}
# endif
if (m_algoVariant <= AV0_AUTO || m_algoVariant >= AV_MAX) {
return Cpu::hasAES() ? AV1_AESNI : AV3_SOFT_AES;
if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) {
return Cpu::hasAES() ? AV_SINGLE : AV_SINGLE_SOFT;
}
if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV2_AESNI_DOUBLE) {
return m_algoVariant + 2;
if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV_DOUBLE) {
return static_cast<AlgoVariant>(m_algoVariant + 2);
}
return m_algoVariant;
@ -327,14 +331,14 @@ int xmrig::Config::getAlgoVariant() const
#ifndef XMRIG_NO_AEON
int xmrig::Config::getAlgoVariantLite() const
xmrig::AlgoVariant xmrig::Config::getAlgoVariantLite() const
{
if (m_algoVariant <= AV0_AUTO || m_algoVariant >= AV_MAX) {
return Cpu::hasAES() ? AV2_AESNI_DOUBLE : AV4_SOFT_AES_DOUBLE;
if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) {
return Cpu::hasAES() ? AV_DOUBLE : AV_DOUBLE_SOFT;
}
if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV2_AESNI_DOUBLE) {
return m_algoVariant + 2;
if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV_DOUBLE) {
return static_cast<AlgoVariant>(m_algoVariant + 2);
}
return m_algoVariant;

View file

@ -29,8 +29,9 @@
#include <vector>
#include "rapidjson/fwd.h"
#include "core/CommonConfig.h"
#include "rapidjson/fwd.h"
#include "xmrig.h"
class Addr;
@ -41,6 +42,7 @@ namespace xmrig {
class ConfigLoader;
class IThread;
class IWatcherListener;
@ -57,18 +59,7 @@ class IWatcherListener;
*/
class Config : public CommonConfig
{
friend class ConfigLoader;
public:
enum AlgoVariant {
AV0_AUTO,
AV1_AESNI,
AV2_AESNI_DOUBLE,
AV3_SOFT_AES,
AV4_SOFT_AES_DOUBLE,
AV_MAX
};
Config();
~Config();
@ -76,14 +67,15 @@ public:
void getJSON(rapidjson::Document &doc) const override;
inline bool isDoubleHash() const { return m_doubleHash; }
inline bool isDryRun() const { return m_dryRun; }
inline bool isHugePages() const { return m_hugePages; }
inline int algoVariant() const { return m_algoVariant; }
inline int printTime() const { return m_printTime; }
inline int priority() const { return m_priority; }
inline int threads() const { return m_threads; }
inline int64_t affinity() const { return m_affinity; }
inline AlgoVariant algoVariant() const { return m_algoVariant; }
inline bool isDoubleHash() const { return m_doubleHash; }
inline bool isDryRun() const { return m_dryRun; }
inline bool isHugePages() const { return m_hugePages; }
inline const std::vector<IThread *> &threads() const { return m_threads; }
inline int printTime() const { return m_printTime; }
inline int priority() const { return m_priority; }
inline int threadsCount() const { return m_threadsCount; }
inline int64_t affinity() const { return m_affinity; }
static Config *load(int argc, char **argv, IWatcherListener *listener);
@ -97,21 +89,22 @@ protected:
private:
bool parseInt(int key, int arg);
int getAlgoVariant() const;
AlgoVariant getAlgoVariant() const;
# ifndef XMRIG_NO_AEON
int getAlgoVariantLite() const;
AlgoVariant getAlgoVariantLite() const;
# endif
AlgoVariant m_algoVariant;
bool m_doubleHash;
bool m_dryRun;
bool m_hugePages;
bool m_safe;
int m_algoVariant;
int m_maxCpuUsage;
int m_printTime;
int m_priority;
int m_threads;
int64_t m_affinity;
size_t m_threadsCount;
std::vector<IThread *> m_threads;
};