New config subsystem

This commit is contained in:
XMRig 2019-04-05 22:14:01 +07:00
parent 85ee55f309
commit fa2a0b9b79
37 changed files with 1628 additions and 1254 deletions

View file

@ -26,89 +26,28 @@
#include <assert.h>
#include "base/io/log/backends/ConsoleLog.h"
#include "base/io/log/backends/FileLog.h"
#include "base/io/log/Log.h"
#include "base/kernel/interfaces/IControllerListener.h"
#include "common/config/ConfigLoader.h"
#include "common/cpu/Cpu.h"
#include "common/Platform.h"
#include "core/config/Config.h"
#include "core/Controller.h"
#include "net/Network.h"
#ifdef HAVE_SYSLOG_H
# include "base/io/log/backends/SysLog.h"
#endif
#ifdef XMRIG_FEATURE_API
# include "api/Api.h"
#endif
class xmrig::ControllerPrivate
{
public:
inline ControllerPrivate(Process *process) :
api(nullptr),
config(nullptr),
network(nullptr),
process(process)
{}
inline ~ControllerPrivate()
{
# ifdef XMRIG_FEATURE_API
delete api;
# endif
delete network;
delete config;
}
Api *api;
Config *config;
Network *network;
Process *process;
std::vector<IControllerListener *> listeners;
};
xmrig::Controller::Controller(Process *process)
: d_ptr(new ControllerPrivate(process))
xmrig::Controller::Controller(Process *process) :
Base(process),
m_network(nullptr)
{
}
xmrig::Controller::~Controller()
{
delete d_ptr;
}
xmrig::Api *xmrig::Controller::api() const
{
assert(d_ptr->api != nullptr);
return d_ptr->api;
delete m_network;
}
bool xmrig::Controller::isReady() const
{
return d_ptr->config && d_ptr->network;
}
xmrig::Config *xmrig::Controller::config() const
{
assert(d_ptr->config != nullptr);
return d_ptr->config;
return Base::isReady() && m_network;
}
@ -116,98 +55,36 @@ int xmrig::Controller::init()
{
Cpu::init();
d_ptr->config = Config::load(d_ptr->process, this);
if (!d_ptr->config) {
return 1;
const int rc = Base::init();
if (rc != 0) {
return rc;
}
# ifdef XMRIG_FEATURE_API
d_ptr->api = new Api(this);
# endif
Platform::init(config()->userAgent());
Platform::setProcessPriority(d_ptr->config->priority());
if (!config()->isBackground()) {
Log::add(new ConsoleLog());
}
if (config()->logFile()) {
Log::add(new FileLog(config()->logFile()));
}
# ifdef HAVE_SYSLOG_H
if (config()->isSyslog()) {
Log::add(new SysLog());
}
# endif
d_ptr->network = new Network(this);
m_network = new Network(this);
return 0;
}
xmrig::Network *xmrig::Controller::network() const
{
assert(d_ptr->network != nullptr);
return d_ptr->network;
}
void xmrig::Controller::addListener(IControllerListener *listener)
{
d_ptr->listeners.push_back(listener);
}
void xmrig::Controller::save()
{
if (!config()) {
return;
}
if (d_ptr->config->isShouldSave()) {
d_ptr->config->save();
}
ConfigLoader::watch(d_ptr->config);
}
void xmrig::Controller::onNewConfig(IConfig *config)
{
Config *previousConfig = d_ptr->config;
d_ptr->config = static_cast<Config*>(config);
for (IControllerListener *listener : d_ptr->listeners) {
listener->onConfigChanged(d_ptr->config, previousConfig);
}
delete previousConfig;
}
void xmrig::Controller::start()
{
Base::start();
network()->connect();
# ifdef XMRIG_FEATURE_API
api()->start();
# endif
save();
}
void xmrig::Controller::stop()
{
# ifdef XMRIG_FEATURE_API
api()->stop();
# endif
Base::stop();
ConfigLoader::release();
delete d_ptr->network;
d_ptr->network = nullptr;
delete m_network;
m_network = nullptr;
}
xmrig::Network *xmrig::Controller::network() const
{
assert(m_network != nullptr);
return m_network;
}

View file

@ -26,45 +26,35 @@
#define XMRIG_CONTROLLER_H
#include "base/kernel/interfaces/IConfigListener.h"
#include "base/kernel/Base.h"
namespace xmrig {
class Api;
class Config;
class ControllerPrivate;
class IControllerListener;
class Network;
class Process;
class Controller : public IConfigListener
class Controller : public Base
{
public:
Controller(Process *process);
~Controller() override;
Api *api() const;
bool isReady() const;
Config *config() const;
int init();
Network *network() const;
void addListener(IControllerListener *listener);
void save();
void start();
void stop();
bool isReady() const override;
int init() override;
void start() override;
void stop() override;
protected:
void onNewConfig(IConfig *config) override;
Network *network() const;
private:
ControllerPrivate *d_ptr;
Network *m_network;
};
} /* namespace xmrig */
} // namespace xmrig
#endif /* XMRIG_CONTROLLER_H */

View file

@ -28,7 +28,7 @@
#include "base/io/log/Log.h"
#include "common/config/ConfigLoader.h"
#include "base/kernel/interfaces/IJsonReader.h"
#include "common/cpu/Cpu.h"
#include "core/config/Config.h"
#include "crypto/Asm.h"
@ -42,7 +42,7 @@
static char affinity_tmp[20] = { 0 };
xmrig::Config::Config() : xmrig::CommonConfig(),
xmrig::Config::Config() :
m_aesMode(AES_AUTO),
m_algoVariant(AV_AUTO),
m_assembly(ASM_AUTO),
@ -55,9 +55,23 @@ xmrig::Config::Config() : xmrig::CommonConfig(),
}
bool xmrig::Config::reload(const rapidjson::Value &json)
bool xmrig::Config::read(const IJsonReader &reader, const char *fileName)
{
return xmrig::ConfigLoader::reload(this, json);
if (!BaseConfig::read(reader, fileName)) {
return false;
}
m_hugePages = reader.getBool("huge-pages", true);
m_safe = reader.getBool("safe");
setAesMode(reader.getValue("hw-aes"));
setAlgoVariant(reader.getInt("av"));
setAssembly(reader.getValue("asm"));
setMaxCpuUsage(reader.getInt("max-cpu-usage", 100));
setPriority(reader.getInt("cpu-priority", -1));
setThreads(reader.getValue("threads"));
return finalize();
}
@ -126,22 +140,8 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const
}
xmrig::Config *xmrig::Config::load(Process *process, IConfigListener *listener)
{
return static_cast<Config*>(ConfigLoader::load(process, listener));
}
bool xmrig::Config::finalize()
{
if (m_state != NoneState) {
return CommonConfig::finalize();
}
if (!CommonConfig::finalize()) {
return false;
}
if (!m_threads.cpu.empty()) {
m_threads.mode = Advanced;
const bool softAES = (m_aesMode == AES_AUTO ? (Cpu::info()->hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_SOFT;
@ -153,7 +153,7 @@ bool xmrig::Config::finalize()
return true;
}
const AlgoVariant av = getAlgoVariant();
const AlgoVariant av = getAlgoVariant();
m_threads.mode = m_threads.count ? Simple : Automatic;
const size_t size = CpuThread::multiway(av) * cn_select_memory(m_algorithm.algo()) / 1024;
@ -173,117 +173,54 @@ bool xmrig::Config::finalize()
}
m_shouldSave = m_threads.mode == Automatic;
return true;
}
bool xmrig::Config::parseBoolean(int key, bool enable)
{
if (!CommonConfig::parseBoolean(key, enable)) {
return false;
}
switch (key) {
case SafeKey: /* --safe */
m_safe = enable;
break;
case HugePagesKey: /* --no-huge-pages */
m_hugePages = enable;
break;
case HardwareAESKey: /* hw-aes config only */
m_aesMode = enable ? AES_HW : AES_SOFT;
break;
# ifndef XMRIG_NO_ASM
case AssemblyKey:
m_assembly = Asm::parse(enable);
break;
# endif
default:
break;
}
return true;
}
bool xmrig::Config::parseString(int key, const char *arg)
void xmrig::Config::setAesMode(const rapidjson::Value &aesMode)
{
if (!CommonConfig::parseString(key, arg)) {
return false;
if (aesMode.IsBool()) {
m_aesMode = aesMode.GetBool() ? AES_HW : AES_SOFT;
}
switch (key) {
case AVKey: /* --av */
case MaxCPUUsageKey: /* --max-cpu-usage */
case CPUPriorityKey: /* --cpu-priority */
return parseUint64(key, strtol(arg, nullptr, 10));
case SafeKey: /* --safe */
return parseBoolean(key, true);
case HugePagesKey: /* --no-huge-pages */
return parseBoolean(key, false);
case ThreadsKey: /* --threads */
if (strncmp(arg, "all", 3) == 0) {
m_threads.count = Cpu::info()->threads();
return true;
}
return parseUint64(key, strtol(arg, nullptr, 10));
case CPUAffinityKey: /* --cpu-affinity */
{
const char *p = strstr(arg, "0x");
return parseUint64(key, p ? strtoull(p, nullptr, 16) : strtoull(arg, nullptr, 10));
}
# ifndef XMRIG_NO_ASM
case AssemblyKey: /* --asm */
m_assembly = Asm::parse(arg);
break;
# endif
default:
break;
}
return true;
}
bool xmrig::Config::parseUint64(int key, uint64_t arg)
void xmrig::Config::setAlgoVariant(int av)
{
if (!CommonConfig::parseUint64(key, arg)) {
return false;
if (av >= AV_AUTO && av < AV_MAX) {
m_algoVariant = static_cast<AlgoVariant>(av);
}
switch (key) {
case CPUAffinityKey: /* --cpu-affinity */
if (arg) {
m_threads.mask = static_cast<int64_t>(arg);
}
break;
default:
return parseInt(key, static_cast<int>(arg));
}
return true;
}
void xmrig::Config::parseJSON(const rapidjson::Value &json)
void xmrig::Config::setAssembly(const rapidjson::Value &assembly)
{
CommonConfig::parseJSON(json);
m_assembly = Asm::parse(assembly);
}
const rapidjson::Value &threads = json["threads"];
void xmrig::Config::setMaxCpuUsage(int max)
{
if (max > 0 && max <= 100) {
m_maxCpuUsage = max;
}
}
void xmrig::Config::setPriority(int priority)
{
if (priority >= 0 && priority <= 5) {
m_priority = priority;
}
}
void xmrig::Config::setThreads(const rapidjson::Value &threads)
{
if (threads.IsArray()) {
m_threads.cpu.clear();
for (const rapidjson::Value &value : threads.GetArray()) {
if (!value.IsObject()) {
continue;
@ -298,41 +235,12 @@ void xmrig::Config::parseJSON(const rapidjson::Value &json)
}
}
}
}
bool xmrig::Config::parseInt(int key, int arg)
{
switch (key) {
case ThreadsKey: /* --threads */
if (arg >= 0 && arg < 1024) {
m_threads.count = arg;
else if (threads.IsUint()) {
const unsigned count = threads.GetUint();
if (count < 1024) {
m_threads.count = count;
}
break;
case AVKey: /* --av */
if (arg >= AV_AUTO && arg < AV_MAX) {
m_algoVariant = static_cast<AlgoVariant>(arg);
}
break;
case MaxCPUUsageKey: /* --max-cpu-usage */
if (m_maxCpuUsage > 0 && arg <= 100) {
m_maxCpuUsage = arg;
}
break;
case CPUPriorityKey: /* --cpu-priority */
if (arg >= 0 && arg <= 5) {
m_priority = arg;
}
break;
default:
break;
}
return true;
}

View file

@ -29,7 +29,7 @@
#include <vector>
#include "common/config/CommonConfig.h"
#include "base/kernel/config/BaseConfig.h"
#include "common/xmrig.h"
#include "rapidjson/fwd.h"
#include "workers/CpuThread.h"
@ -55,7 +55,7 @@ class Process;
* api/worker-id
* pools/
*/
class Config : public CommonConfig
class Config : public BaseConfig
{
public:
enum ThreadsMode {
@ -67,8 +67,7 @@ public:
Config();
bool reload(const rapidjson::Value &json);
bool read(const IJsonReader &reader, const char *fileName) override;
void getJSON(rapidjson::Document &doc) const override;
inline AesMode aesMode() const { return m_aesMode; }
@ -80,20 +79,16 @@ public:
inline int priority() const { return m_priority; }
inline int threadsCount() const { return static_cast<int>(m_threads.list.size()); }
inline int64_t affinity() const { return m_threads.mask; }
inline static IConfig *create() { return new Config(); }
inline ThreadsMode threadsMode() const { return m_threads.mode; }
static Config *load(Process *process, IConfigListener *listener);
protected:
bool finalize() override;
bool parseBoolean(int key, bool enable) override;
bool parseString(int key, const char *arg) override;
bool parseUint64(int key, uint64_t arg) override;
void parseJSON(const rapidjson::Value &json) override;
private:
bool parseInt(int key, int arg);
bool finalize();
void setAesMode(const rapidjson::Value &aesMode);
void setAlgoVariant(int av);
void setAssembly(const rapidjson::Value &assembly);
void setMaxCpuUsage(int max);
void setPriority(int priority);
void setThreads(const rapidjson::Value &threads);
AlgoVariant getAlgoVariant() const;
# ifndef XMRIG_NO_AEON

View file

@ -0,0 +1,106 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "core/config/ConfigTransform.h"
#include "base/kernel/interfaces/IConfig.h"
xmrig::ConfigTransform::ConfigTransform()
{
}
void xmrig::ConfigTransform::transform(rapidjson::Document &doc, int key, const char *arg)
{
BaseTransform::transform(doc, key, arg);
switch (key) {
case IConfig::AVKey: /* --av */
case IConfig::MaxCPUUsageKey: /* --max-cpu-usage */
case IConfig::CPUPriorityKey: /* --cpu-priority */
case IConfig::ThreadsKey: /* --threads */
return transformUint64(doc, key, static_cast<uint64_t>(strtol(arg, nullptr, 10)));
case IConfig::SafeKey: /* --safe */
return transformBoolean(doc, key, true);
case IConfig::HugePagesKey: /* --no-huge-pages */
return transformBoolean(doc, key, false);
case IConfig::CPUAffinityKey: /* --cpu-affinity */
{
const char *p = strstr(arg, "0x");
return transformUint64(doc, key, p ? strtoull(p, nullptr, 16) : strtoull(arg, nullptr, 10));
}
# ifndef XMRIG_NO_ASM
case IConfig::AssemblyKey: /* --asm */
return set<const char *>(doc, "asm", arg);
# endif
default:
break;
}
}
void xmrig::ConfigTransform::transformBoolean(rapidjson::Document &doc, int key, bool enable)
{
switch (key) {
case IConfig::SafeKey: /* --safe */
return set<bool>(doc, "safe", enable);
case IConfig::HugePagesKey: /* --no-huge-pages */
return set<bool>(doc, "huge-pages", enable);
default:
break;
}
}
void xmrig::ConfigTransform::transformUint64(rapidjson::Document &doc, int key, uint64_t arg)
{
switch (key) {
case IConfig::CPUAffinityKey: /* --cpu-affinity */
return set<int64_t>(doc, "cpu-affinity", static_cast<int64_t>(arg));
case IConfig::ThreadsKey: /* --threads */
return set<uint64_t>(doc, "threads", arg);
case IConfig::AVKey: /* --av */
return set<uint64_t>(doc, "av", arg);
case IConfig::MaxCPUUsageKey: /* --max-cpu-usage */
return set<uint64_t>(doc, "max-cpu-usage", arg);
case IConfig::CPUPriorityKey: /* --cpu-priority */
return set<uint64_t>(doc, "cpu-priority", arg);
default:
break;
}
}

View file

@ -0,0 +1,51 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* 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 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_CONFIGTRANSFORM_H
#define XMRIG_CONFIGTRANSFORM_H
#include "base/kernel/config/BaseTransform.h"
namespace xmrig {
class ConfigTransform : public BaseTransform
{
public:
ConfigTransform();
protected:
void transform(rapidjson::Document &doc, int key, const char *arg) override;
private:
void transformBoolean(rapidjson::Document &doc, int key, bool enable);
void transformUint64(rapidjson::Document &doc, int key, uint64_t arg);
};
} // namespace xmrig
#endif /* XMRIG_CONFIGTRANSFORM_H */

View file

@ -22,8 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_CONFIGLOADER_DEFAULT_H
#define XMRIG_CONFIGLOADER_DEFAULT_H
#ifndef XMRIG_CONFIG_DEFAULT_H
#define XMRIG_CONFIG_DEFAULT_H
namespace xmrig {
@ -85,6 +85,7 @@ R"===(
#endif
} /* namespace xmrig */
} // namespace xmrig
#endif /* XMRIG_CONFIGLOADER_DEFAULT_H */
#endif /* XMRIG_CONFIG_DEFAULT_H */

View file

@ -22,8 +22,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_CONFIGLOADER_PLATFORM_H
#define XMRIG_CONFIGLOADER_PLATFORM_H
#ifndef XMRIG_CONFIG_PLATFORM_H
#define XMRIG_CONFIG_PLATFORM_H
#ifdef _MSC_VER
@ -33,17 +33,17 @@
#endif
#include "common/interfaces/IConfig.h"
#include "base/kernel/interfaces/IConfig.h"
#include "version.h"
namespace xmrig {
static char const short_options[] = "a:c:kBp:Px:r:R:s:t:T:o:u:O:v:l:S";
static const char short_options[] = "a:c:kBp:Px:r:R:s:t:T:o:u:O:v:l:S";
static struct option const options[] = {
static const option options[] = {
{ "algo", 1, nullptr, IConfig::AlgorithmKey },
{ "api-worker-id", 1, nullptr, IConfig::ApiWorkerIdKey },
{ "api-id", 1, nullptr, IConfig::ApiIdKey },
@ -65,7 +65,6 @@ static struct option const options[] = {
{ "max-cpu-usage", 1, nullptr, IConfig::MaxCPUUsageKey },
{ "nicehash", 0, nullptr, IConfig::NicehashKey },
{ "no-color", 0, nullptr, IConfig::ColorKey },
{ "no-watch", 0, nullptr, IConfig::WatchKey },
{ "no-huge-pages", 0, nullptr, IConfig::HugePagesKey },
{ "variant", 1, nullptr, IConfig::VariantKey },
{ "pass", 1, nullptr, IConfig::PasswordKey },
@ -115,21 +114,12 @@ static struct option const config_options[] = {
{ "syslog", 0, nullptr, IConfig::SyslogKey },
{ "threads", 1, nullptr, IConfig::ThreadsKey },
{ "user-agent", 1, nullptr, IConfig::UserAgentKey },
{ "watch", 0, nullptr, IConfig::WatchKey },
{ "hw-aes", 0, nullptr, IConfig::HardwareAESKey },
{ "asm", 1, nullptr, IConfig::AssemblyKey },
{ "autosave", 0, nullptr, IConfig::AutoSaveKey },
{ nullptr, 0, nullptr, 0 }
};
static struct option const api_options[] = {
{ "worker-id", 1, nullptr, IConfig::ApiWorkerIdKey },
{ "id", 1, nullptr, IConfig::ApiIdKey },
{ nullptr, 0, nullptr, 0 }
};
} // namespace xmrig
} /* namespace xmrig */
#endif /* XMRIG_CONFIGLOADER_PLATFORM_H */
#endif /* XMRIG_CONFIG_PLATFORM_H */