2018-03-31 06:48:06 +00:00
|
|
|
/* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <uv.h>
|
2018-03-31 12:00:31 +00:00
|
|
|
#include <inttypes.h>
|
2018-03-31 06:48:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "core/Config.h"
|
|
|
|
#include "core/ConfigCreator.h"
|
|
|
|
#include "core/ConfigLoader.h"
|
|
|
|
#include "Cpu.h"
|
2018-04-10 23:09:34 +00:00
|
|
|
#include "net/Pool.h"
|
2018-03-31 06:48:06 +00:00
|
|
|
#include "rapidjson/document.h"
|
|
|
|
#include "rapidjson/filewritestream.h"
|
|
|
|
#include "rapidjson/prettywriter.h"
|
2018-04-01 15:49:21 +00:00
|
|
|
#include "workers/CpuThread.h"
|
2018-03-31 06:48:06 +00:00
|
|
|
#include "xmrig.h"
|
|
|
|
|
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
static char affinity_tmp[20] = { 0 };
|
|
|
|
|
|
|
|
|
2018-03-31 06:48:06 +00:00
|
|
|
xmrig::Config::Config() : xmrig::CommonConfig(),
|
2018-04-01 15:49:21 +00:00
|
|
|
m_algoVariant(AV_AUTO),
|
2018-03-31 06:48:06 +00:00
|
|
|
m_doubleHash(false),
|
|
|
|
m_dryRun(false),
|
|
|
|
m_hugePages(true),
|
|
|
|
m_safe(false),
|
|
|
|
m_maxCpuUsage(75),
|
|
|
|
m_printTime(60),
|
|
|
|
m_priority(-1),
|
2018-04-01 15:49:21 +00:00
|
|
|
m_affinity(-1L),
|
|
|
|
m_threadsCount(0)
|
2018-03-31 06:48:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xmrig::Config::~Config()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool xmrig::Config::reload(const char *json)
|
|
|
|
{
|
|
|
|
return xmrig::ConfigLoader::reload(this, json);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void xmrig::Config::getJSON(rapidjson::Document &doc) const
|
|
|
|
{
|
|
|
|
doc.SetObject();
|
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
auto &allocator = doc.GetAllocator();
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
doc.AddMember("algo", rapidjson::StringRef(algoName()), allocator);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
rapidjson::Value api(rapidjson::kObjectType);
|
|
|
|
api.AddMember("port", apiPort(), allocator);
|
|
|
|
api.AddMember("access-token", apiToken() ? rapidjson::Value(rapidjson::StringRef(apiToken())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator);
|
|
|
|
api.AddMember("worker-id", apiWorkerId() ? rapidjson::Value(rapidjson::StringRef(apiWorkerId())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator);
|
|
|
|
api.AddMember("ipv6", isApiIPv6(), allocator);
|
|
|
|
api.AddMember("restricted", isApiRestricted(), allocator);
|
|
|
|
doc.AddMember("api", api, allocator);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
doc.AddMember("av", algoVariant(), allocator);
|
|
|
|
doc.AddMember("background", isBackground(), allocator);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
doc.AddMember("colors", isColors(), allocator);
|
|
|
|
|
|
|
|
if (affinity() != -1L) {
|
|
|
|
snprintf(affinity_tmp, sizeof(affinity_tmp) - 1, "0x%" PRIX64, affinity());
|
|
|
|
doc.AddMember("cpu-affinity", rapidjson::StringRef(affinity_tmp), allocator);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
doc.AddMember("cpu-affinity", rapidjson::kNullType, allocator);
|
|
|
|
}
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
if (priority() != -1) {
|
|
|
|
doc.AddMember("cpu-priority", priority(), allocator);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
doc.AddMember("cpu-priority", rapidjson::kNullType, allocator);
|
|
|
|
}
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
doc.AddMember("donate-level", donateLevel(), allocator);
|
|
|
|
doc.AddMember("huge-pages", isHugePages(), allocator);
|
|
|
|
doc.AddMember("log-file", logFile() ? rapidjson::Value(rapidjson::StringRef(logFile())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator);
|
|
|
|
doc.AddMember("max-cpu-usage", m_maxCpuUsage, allocator);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
rapidjson::Value pools(rapidjson::kArrayType);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-04-10 23:09:34 +00:00
|
|
|
for (const Pool &pool : m_pools) {
|
2018-03-31 12:00:31 +00:00
|
|
|
rapidjson::Value obj(rapidjson::kObjectType);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-04-10 23:09:34 +00:00
|
|
|
obj.AddMember("url", rapidjson::StringRef(pool.url()), allocator);
|
|
|
|
obj.AddMember("user", rapidjson::StringRef(pool.user()), allocator);
|
|
|
|
obj.AddMember("pass", rapidjson::StringRef(pool.password()), allocator);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-04-10 23:09:34 +00:00
|
|
|
if (pool.keepAlive() == 0 || pool.keepAlive() == Pool::kKeepAliveTimeout) {
|
|
|
|
obj.AddMember("keepalive", pool.keepAlive() > 0, allocator);
|
2018-03-31 12:00:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-04-10 23:09:34 +00:00
|
|
|
obj.AddMember("keepalive", pool.keepAlive(), allocator);
|
2018-03-31 12:00:31 +00:00
|
|
|
}
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-04-10 23:09:34 +00:00
|
|
|
obj.AddMember("nicehash", pool.isNicehash(), allocator);
|
|
|
|
obj.AddMember("variant", pool.variant(), allocator);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
pools.PushBack(obj, allocator);
|
|
|
|
}
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
doc.AddMember("pools", pools, allocator);
|
|
|
|
doc.AddMember("print-time", printTime(), allocator);
|
|
|
|
doc.AddMember("retries", retries(), allocator);
|
|
|
|
doc.AddMember("retry-pause", retryPause(), allocator);
|
|
|
|
doc.AddMember("safe", m_safe, allocator);
|
2018-04-01 15:49:21 +00:00
|
|
|
doc.AddMember("threads", threadsCount(), allocator);
|
2018-03-31 12:00:31 +00:00
|
|
|
doc.AddMember("user-agent", userAgent() ? rapidjson::Value(rapidjson::StringRef(userAgent())).Move() : rapidjson::Value(rapidjson::kNullType).Move(), allocator);
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
# ifdef HAVE_SYSLOG_H
|
2018-03-31 13:26:07 +00:00
|
|
|
doc.AddMember("syslog", isSyslog(), allocator);
|
2018-03-31 12:00:31 +00:00
|
|
|
# endif
|
2018-03-31 06:48:06 +00:00
|
|
|
|
2018-03-31 12:00:31 +00:00
|
|
|
doc.AddMember("watch", m_watch, allocator);
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xmrig::Config *xmrig::Config::load(int argc, char **argv, IWatcherListener *listener)
|
|
|
|
{
|
|
|
|
return static_cast<Config*>(ConfigLoader::load(argc, argv, new ConfigCreator(), listener));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool xmrig::Config::adjust()
|
|
|
|
{
|
|
|
|
if (!CommonConfig::adjust()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_algoVariant = getAlgoVariant();
|
2018-04-01 15:49:21 +00:00
|
|
|
if (m_algoVariant == AV_DOUBLE || m_algoVariant == AV_DOUBLE_SOFT) {
|
2018-03-31 06:48:06 +00:00
|
|
|
m_doubleHash = true;
|
|
|
|
}
|
|
|
|
|
2018-04-01 15:49:21 +00:00
|
|
|
if (!m_threadsCount) {
|
|
|
|
m_threadsCount = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage);
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
else if (m_safe) {
|
2018-04-01 15:49:21 +00:00
|
|
|
const size_t count = Cpu::optimalThreadsCount(m_algorithm, m_doubleHash, m_maxCpuUsage);
|
|
|
|
if (m_threadsCount > count) {
|
|
|
|
m_threadsCount = count;
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 15:49:21 +00:00
|
|
|
for (size_t i = 0; i < m_threadsCount; ++i) {
|
|
|
|
m_threads.push_back(CpuThread::createFromAV(i, m_algorithm, m_algoVariant, m_affinity, m_priority));
|
|
|
|
}
|
|
|
|
|
2018-03-31 06:48:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool xmrig::Config::parseBoolean(int key, bool enable)
|
|
|
|
{
|
|
|
|
if (!CommonConfig::parseBoolean(key, enable)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case xmrig::IConfig::SafeKey: /* --safe */
|
|
|
|
m_safe = enable;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case xmrig::IConfig::HugePagesKey: /* --no-huge-pages */
|
|
|
|
m_hugePages = enable;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case xmrig::IConfig::DryRunKey: /* --dry-run */
|
|
|
|
m_dryRun = enable;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool xmrig::Config::parseString(int key, const char *arg)
|
|
|
|
{
|
|
|
|
if (!CommonConfig::parseString(key, arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case xmrig::IConfig::AVKey: /* --av */
|
|
|
|
case xmrig::IConfig::MaxCPUUsageKey: /* --max-cpu-usage */
|
|
|
|
case xmrig::IConfig::CPUPriorityKey: /* --cpu-priority */
|
|
|
|
return parseUint64(key, strtol(arg, nullptr, 10));
|
|
|
|
|
|
|
|
case xmrig::IConfig::SafeKey: /* --safe */
|
|
|
|
case xmrig::IConfig::DryRunKey: /* --dry-run */
|
|
|
|
return parseBoolean(key, true);
|
|
|
|
|
|
|
|
case xmrig::IConfig::HugePagesKey: /* --no-huge-pages */
|
|
|
|
return parseBoolean(key, false);
|
|
|
|
|
|
|
|
case xmrig::IConfig::ThreadsKey: /* --threads */
|
|
|
|
if (strncmp(arg, "all", 3) == 0) {
|
2018-04-01 15:49:21 +00:00
|
|
|
m_threadsCount = Cpu::threads();
|
2018-03-31 06:48:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseUint64(key, strtol(arg, nullptr, 10));
|
|
|
|
|
|
|
|
case xmrig::IConfig::CPUAffinityKey: /* --cpu-affinity */
|
|
|
|
{
|
|
|
|
const char *p = strstr(arg, "0x");
|
|
|
|
return parseUint64(key, p ? strtoull(p, nullptr, 16) : strtoull(arg, nullptr, 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool xmrig::Config::parseUint64(int key, uint64_t arg)
|
|
|
|
{
|
|
|
|
if (!CommonConfig::parseUint64(key, arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case xmrig::IConfig::CPUAffinityKey: /* --cpu-affinity */
|
|
|
|
if (arg) {
|
|
|
|
m_affinity = arg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return parseInt(key, static_cast<int>(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void xmrig::Config::parseJSON(const rapidjson::Document &doc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool xmrig::Config::parseInt(int key, int arg)
|
|
|
|
{
|
|
|
|
switch (key) {
|
|
|
|
case xmrig::IConfig::ThreadsKey: /* --threads */
|
2018-04-01 15:49:21 +00:00
|
|
|
if (m_threadsCount >= 0 && arg < 1024) {
|
|
|
|
m_threadsCount = arg;
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case xmrig::IConfig::AVKey: /* --av */
|
2018-04-01 15:49:21 +00:00
|
|
|
if (arg >= AV_AUTO && arg < AV_MAX) {
|
|
|
|
m_algoVariant = static_cast<AlgoVariant>(arg);
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case xmrig::IConfig::MaxCPUUsageKey: /* --max-cpu-usage */
|
|
|
|
if (m_maxCpuUsage > 0 && arg <= 100) {
|
|
|
|
m_maxCpuUsage = arg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case xmrig::IConfig::CPUPriorityKey: /* --cpu-priority */
|
|
|
|
if (arg >= 0 && arg <= 5) {
|
|
|
|
m_priority = arg;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-01 15:49:21 +00:00
|
|
|
xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const
|
2018-03-31 06:48:06 +00:00
|
|
|
{
|
|
|
|
# ifndef XMRIG_NO_AEON
|
2018-04-01 15:49:21 +00:00
|
|
|
if (m_algorithm == xmrig::CRYPTONIGHT_LITE) {
|
2018-03-31 06:48:06 +00:00
|
|
|
return getAlgoVariantLite();
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2018-04-01 15:49:21 +00:00
|
|
|
if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) {
|
|
|
|
return Cpu::hasAES() ? AV_SINGLE : AV_SINGLE_SOFT;
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 15:49:21 +00:00
|
|
|
if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV_DOUBLE) {
|
|
|
|
return static_cast<AlgoVariant>(m_algoVariant + 2);
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_algoVariant;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef XMRIG_NO_AEON
|
2018-04-01 15:49:21 +00:00
|
|
|
xmrig::AlgoVariant xmrig::Config::getAlgoVariantLite() const
|
2018-03-31 06:48:06 +00:00
|
|
|
{
|
2018-04-01 15:49:21 +00:00
|
|
|
if (m_algoVariant <= AV_AUTO || m_algoVariant >= AV_MAX) {
|
|
|
|
return Cpu::hasAES() ? AV_DOUBLE : AV_DOUBLE_SOFT;
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 15:49:21 +00:00
|
|
|
if (m_safe && !Cpu::hasAES() && m_algoVariant <= AV_DOUBLE) {
|
|
|
|
return static_cast<AlgoVariant>(m_algoVariant + 2);
|
2018-03-31 06:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_algoVariant;
|
|
|
|
}
|
|
|
|
#endif
|