Added class CpuConfig.

This commit is contained in:
XMRig 2019-06-28 22:28:40 +07:00
parent 66d62de681
commit dd875c7c37
10 changed files with 233 additions and 133 deletions

View file

@ -18,11 +18,13 @@ option(WITH_EMBEDDED_CONFIG "Enable internal embedded JSON config" OFF)
include (CheckIncludeFile)
include (cmake/cpu.cmake)
include (src/base/base.cmake)
include (src/backend/cpu/cpu.cmake)
set(HEADERS
"${HEADERS_BASE}"
"${HEADERS_BASE_HTTP}"
"${HEADERS_CPU}"
src/api/interfaces/IApiListener.h
src/App.h
src/common/cpu/Cpu.h
@ -83,6 +85,7 @@ endif()
set(SOURCES
"${SOURCES_BASE}"
"${SOURCES_BASE_HTTP}"
"${SOURCES_CPU}"
src/App.cpp
src/common/Platform.cpp
src/core/config/Config.cpp

View file

@ -77,7 +77,7 @@ int xmrig::App::exec()
background();
Mem::init(m_controller->config()->isHugePages());
Mem::init(m_controller->config()->cpu().isHugePages());
Summary::print(m_controller);

View file

@ -86,9 +86,9 @@ static void print_threads(xmrig::Config *config)
{
if (config->threadsMode() != xmrig::Config::Advanced) {
char buf[32] = { 0 };
if (config->affinity() != -1L) {
snprintf(buf, sizeof buf, ", affinity=0x%" PRIX64, config->affinity());
}
// if (config->affinity() != -1L) {
// snprintf(buf, sizeof buf, ", affinity=0x%" PRIX64, config->affinity());
// }
xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s") CYAN_BOLD("%d") WHITE_BOLD(", av=%d, %sdonate=%d%%") WHITE_BOLD("%s"),
"THREADS",
@ -109,13 +109,13 @@ static void print_threads(xmrig::Config *config)
}
# ifdef XMRIG_FEATURE_ASM
if (config->assembly() == xmrig::Assembly::AUTO) {
if (config->cpu().assembly() == xmrig::Assembly::AUTO) {
const xmrig::Assembly assembly = xmrig::Cpu::info()->assembly();
xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13sauto:%s"), "ASSEMBLY", asmName(assembly));
}
else {
xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s"), "ASSEMBLY", asmName(config->assembly()));
xmrig::Log::print(GREEN_BOLD(" * ") WHITE_BOLD("%-13s%s"), "ASSEMBLY", asmName(config->cpu().assembly()));
}
# endif
}

View file

@ -0,0 +1,110 @@
/* 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 "backend/cpu/CpuConfig.h"
#include "base/io/json/Json.h"
#include "common/cpu/Cpu.h"
#include "rapidjson/document.h"
namespace xmrig {
static const char *kEnabled = "enabled";
static const char *kHugePages = "huge-pages";
static const char *kHwAes = "hw-aes";
static const char *kPriority = "priority";
#ifdef XMRIG_FEATURE_ASM
static const char *kAsm = "asm";
#endif
}
xmrig::CpuConfig::CpuConfig()
{
}
bool xmrig::CpuConfig::isHwAES() const
{
return (m_aes == AES_AUTO ? (Cpu::info()->hasAES() ? AES_HW : AES_SOFT) : m_aes) == AES_HW;
}
rapidjson::Value xmrig::CpuConfig::toJSON(rapidjson::Document &doc) const
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
Value obj(kObjectType);
obj.AddMember(StringRef(kEnabled), m_enabled, allocator);
obj.AddMember(StringRef(kHugePages), m_hugePages, allocator);
obj.AddMember(StringRef(kHwAes), m_aes == AES_AUTO ? Value(kNullType) : Value(m_aes == AES_HW), allocator);
obj.AddMember(StringRef(kPriority), priority() != -1 ? Value(priority()) : Value(kNullType), allocator);
# ifdef XMRIG_FEATURE_ASM
obj.AddMember(StringRef(kAsm), m_assembly.toJSON(), allocator);
# endif
return obj;
}
void xmrig::CpuConfig::read(const rapidjson::Value &value)
{
if (value.IsObject()) {
m_enabled = Json::getBool(value, kEnabled, m_enabled);
m_hugePages = Json::getBool(value, kHugePages, m_hugePages);
setAesMode(Json::getValue(value, kHwAes));
setPriority(Json::getInt(value, kPriority, -1));
# ifdef XMRIG_FEATURE_ASM
m_assembly = Json::getValue(value, kAsm);
# endif
}
}
void xmrig::CpuConfig::setAesMode(const rapidjson::Value &aesMode)
{
if (aesMode.IsBool()) {
m_aes = aesMode.GetBool() ? AES_HW : AES_SOFT;
}
else {
m_aes = AES_AUTO;
}
}
void xmrig::CpuConfig::setPriority(int priority)
{
m_priority = (priority >= -1 && priority <= 5) ? priority : -1;
}

View file

@ -0,0 +1,72 @@
/* 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/>.
*/
#ifndef XMRIG_CPUCONFIG_H
#define XMRIG_CPUCONFIG_H
#include "crypto/common/Assembly.h"
namespace xmrig {
class CpuConfig
{
public:
enum AesMode {
AES_AUTO,
AES_HW,
AES_SOFT
};
CpuConfig();
bool isHwAES() const;
rapidjson::Value toJSON(rapidjson::Document &doc) const;
void read(const rapidjson::Value &value);
inline bool isEnabled() const { return m_enabled; }
inline bool isHugePages() const { return m_hugePages; }
inline bool isShouldSave() const { return m_shouldSave; }
inline const Assembly &assembly() const { return m_assembly; }
inline int priority() const { return m_priority; }
private:
void setAesMode(const rapidjson::Value &aesMode);
void setPriority(int priority);
AesMode m_aes = AES_AUTO;
Assembly m_assembly;
bool m_enabled = true;
bool m_hugePages = true;
bool m_shouldSave = false;
int m_priority = -1;
};
} /* namespace xmrig */
#endif /* XMRIG_CPUCONFIG_H */

View file

@ -0,0 +1,7 @@
set(HEADERS_CPU
src/backend/cpu/CpuConfig.h
)
set(SOURCES_CPU
src/backend/cpu/CpuConfig.cpp
)

View file

@ -172,7 +172,7 @@ int xmrig::Base::init()
Platform::init(config()->userAgent());
# ifndef XMRIG_PROXY_PROJECT
Platform::setProcessPriority(config()->priority());
Platform::setProcessPriority(config()->cpu().priority());
# endif
if (!config()->isBackground()) {

View file

@ -39,46 +39,24 @@
#include "workers/CpuThread.h"
static char affinity_tmp[20] = { 0 };
xmrig::Config::Config() :
m_aesMode(AES_AUTO),
m_algoVariant(AV_AUTO),
m_hugePages(true),
m_safe(false),
m_shouldSave(false),
m_maxCpuUsage(100),
m_priority(-1)
m_shouldSave(false)
{
}
bool xmrig::Config::isHwAES() const
{
return (m_aesMode == AES_AUTO ? (Cpu::info()->hasAES() ? AES_HW : AES_SOFT) : m_aesMode) == AES_HW;
}
bool xmrig::Config::read(const IJsonReader &reader, const char *fileName)
{
if (!BaseConfig::read(reader, fileName)) {
return false;
}
m_hugePages = reader.getBool("huge-pages", true);
m_safe = reader.getBool("safe");
m_cpu.read(reader.getValue("cpu"));
setAesMode(reader.getValue("hw-aes"));
setAlgoVariant(reader.getInt("av"));
setMaxCpuUsage(reader.getInt("max-cpu-usage", 100));
setPriority(reader.getInt("cpu-priority", -1));
setThreads(reader.getValue("threads"));
# ifdef XMRIG_FEATURE_ASM
setAssembly(reader.getValue("asm"));
# endif
return finalize();
}
@ -96,36 +74,29 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const
api.AddMember("worker-id", m_apiWorkerId.toJSON(), allocator);
doc.AddMember("api", api, allocator);
doc.AddMember("http", m_http.toJSON(doc), allocator);
# ifdef XMRIG_FEATURE_ASM
doc.AddMember("asm", m_assembly.toJSON(), allocator);
# endif
doc.AddMember("autosave", isAutoSave(), allocator);
doc.AddMember("av", algoVariant(), allocator);
doc.AddMember("background", isBackground(), allocator);
doc.AddMember("colors", Log::colors, allocator);
if (affinity() != -1L) {
snprintf(affinity_tmp, sizeof(affinity_tmp) - 1, "0x%" PRIX64, affinity());
doc.AddMember("cpu-affinity", StringRef(affinity_tmp), allocator);
}
else {
doc.AddMember("cpu-affinity", kNullType, allocator);
}
// if (affinity() != -1L) {
// snprintf(affinity_tmp, sizeof(affinity_tmp) - 1, "0x%" PRIX64, affinity());
// doc.AddMember("cpu-affinity", StringRef(affinity_tmp), allocator);
// }
// else {
// doc.AddMember("cpu-affinity", kNullType, allocator);
// }
doc.AddMember("cpu", m_cpu.toJSON(doc), allocator);
doc.AddMember("cpu-priority", priority() != -1 ? Value(priority()) : Value(kNullType), allocator);
doc.AddMember("donate-level", m_pools.donateLevel(), allocator);
doc.AddMember("donate-over-proxy", m_pools.proxyDonate(), allocator);
doc.AddMember("huge-pages", isHugePages(), allocator);
doc.AddMember("hw-aes", m_aesMode == AES_AUTO ? Value(kNullType) : Value(m_aesMode == AES_HW), allocator);
doc.AddMember("log-file", m_logFile.toJSON(), allocator);
doc.AddMember("max-cpu-usage", m_maxCpuUsage, allocator);
doc.AddMember("pools", m_pools.toJSON(doc), allocator);
doc.AddMember("print-time", printTime(), allocator);
doc.AddMember("retries", m_pools.retries(), allocator);
doc.AddMember("retry-pause", m_pools.retryPause(), allocator);
doc.AddMember("safe", m_safe, allocator);
if (threadsMode() != Simple) {
Value threads(kArrayType);
@ -154,7 +125,7 @@ bool xmrig::Config::finalize()
m_threads.mode = Advanced;
for (size_t i = 0; i < m_threads.cpu.size(); ++i) {
m_threads.list.push_back(CpuThread::createFromData(i, algorithm, m_threads.cpu[i], m_priority, !isHwAES()));
m_threads.list.push_back(CpuThread::createFromData(i, algorithm, m_threads.cpu[i], m_cpu.priority(), !m_cpu.isHwAES()));
}
return true;
@ -166,17 +137,17 @@ bool xmrig::Config::finalize()
const size_t size = CpuThread::multiway(av) * CnAlgo<>::memory(algorithm) / 1024; // FIXME MEMORY
if (!m_threads.count) {
m_threads.count = Cpu::info()->optimalThreadsCount(size, m_maxCpuUsage);
}
else if (m_safe) {
const size_t count = Cpu::info()->optimalThreadsCount(size, m_maxCpuUsage);
if (m_threads.count > count) {
m_threads.count = count;
}
m_threads.count = Cpu::info()->optimalThreadsCount(size, 100);
}
// else if (m_safe) {
// const size_t count = Cpu::info()->optimalThreadsCount(size, m_maxCpuUsage);
// if (m_threads.count > count) {
// m_threads.count = count;
// }
// }
for (size_t i = 0; i < m_threads.count; ++i) {
m_threads.list.push_back(CpuThread::createFromAV(i, algorithm, av, m_threads.mask, m_priority, m_assembly));
m_threads.list.push_back(CpuThread::createFromAV(i, algorithm, av, m_threads.mask, m_cpu.priority(), m_cpu.assembly()));
}
m_shouldSave = m_threads.mode == Automatic;
@ -185,14 +156,6 @@ bool xmrig::Config::finalize()
}
void xmrig::Config::setAesMode(const rapidjson::Value &aesMode)
{
if (aesMode.IsBool()) {
m_aesMode = aesMode.GetBool() ? AES_HW : AES_SOFT;
}
}
void xmrig::Config::setAlgoVariant(int av)
{
if (av >= AV_AUTO && av < AV_MAX) {
@ -201,22 +164,6 @@ void xmrig::Config::setAlgoVariant(int av)
}
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()) {
@ -257,9 +204,9 @@ xmrig::AlgoVariant xmrig::Config::getAlgoVariant() const
return Cpu::info()->hasAES() ? AV_SINGLE : AV_SINGLE_SOFT;
}
if (m_safe && !Cpu::info()->hasAES() && m_algoVariant <= AV_DOUBLE) {
return static_cast<AlgoVariant>(m_algoVariant + 2);
}
// if (m_safe && !Cpu::info()->hasAES() && m_algoVariant <= AV_DOUBLE) {
// return static_cast<AlgoVariant>(m_algoVariant + 2);
// }
return m_algoVariant;
}
@ -272,18 +219,10 @@ xmrig::AlgoVariant xmrig::Config::getAlgoVariantLite() const
return Cpu::info()->hasAES() ? AV_DOUBLE : AV_DOUBLE_SOFT;
}
if (m_safe && !Cpu::info()->hasAES() && m_algoVariant <= AV_DOUBLE) {
return static_cast<AlgoVariant>(m_algoVariant + 2);
}
// if (m_safe && !Cpu::info()->hasAES() && m_algoVariant <= AV_DOUBLE) {
// return static_cast<AlgoVariant>(m_algoVariant + 2);
// }
return m_algoVariant;
}
#endif
#ifdef XMRIG_FEATURE_ASM
void xmrig::Config::setAssembly(const rapidjson::Value &assembly)
{
m_assembly = assembly;
}
#endif

View file

@ -5,7 +5,8 @@
* 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>
* 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
@ -29,6 +30,7 @@
#include <vector>
#include "backend/cpu/CpuConfig.h"
#include "base/kernel/config/BaseConfig.h"
#include "common/xmrig.h"
#include "rapidjson/fwd.h"
@ -38,23 +40,9 @@
namespace xmrig {
class ConfigLoader;
class IThread;
class IConfigListener;
class Process;
/**
* @brief The Config class
*
* Options with dynamic reload:
* colors
* debug
* verbose
* custom-diff (only for new connections)
* api/worker-id
* pools/
*/
class Config : public BaseConfig
{
public:
@ -67,26 +55,19 @@ public:
Config();
bool isHwAES() const;
bool read(const IJsonReader &reader, const char *fileName) override;
void getJSON(rapidjson::Document &doc) const override;
inline AlgoVariant algoVariant() const { return m_algoVariant; }
inline Assembly assembly() const { return m_assembly; }
inline bool isHugePages() const { return m_hugePages; }
inline bool isShouldSave() const { return (m_shouldSave || m_upgrade) && isAutoSave(); }
inline const CpuConfig &cpu() const { return m_cpu; }
inline const std::vector<IThread *> &threads() const { return m_threads.list; }
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 ThreadsMode threadsMode() const { return m_threads.mode; }
private:
bool finalize();
void setAesMode(const rapidjson::Value &aesMode);
void setAlgoVariant(int av);
void setMaxCpuUsage(int max);
void setPriority(int priority);
void setThreads(const rapidjson::Value &threads);
AlgoVariant getAlgoVariant() const;
@ -94,11 +75,6 @@ private:
AlgoVariant getAlgoVariantLite() const;
# endif
# ifdef XMRIG_FEATURE_ASM
void setAssembly(const rapidjson::Value &assembly);
# endif
struct Threads
{
inline Threads() : mask(-1L), count(0), mode(Automatic) {}
@ -111,18 +87,14 @@ private:
};
AesMode m_aesMode;
AlgoVariant m_algoVariant;
Assembly m_assembly;
bool m_hugePages;
bool m_safe;
bool m_shouldSave;
int m_maxCpuUsage;
int m_priority;
CpuConfig m_cpu;
Threads m_threads;
};
} /* namespace xmrig */
#endif /* XMRIG_CONFIG_H */

View file

@ -57,11 +57,8 @@ public:
const char *toString() const;
rapidjson::Value toJSON() const;
// inline static Assembly parse(bool enable) { return enable ? ASM_AUTO : ASM_NONE; }
inline bool isEqual(const Assembly &other) const { return m_id == other.m_id; }
inline bool operator!=(const Assembly &other) const { return !isEqual(other); }
inline bool operator!=(const Assembly::Id &id) const { return m_id != id; }
inline bool operator==(const Assembly &other) const { return isEqual(other); }