From 61ab47cc95f06c4ab7d5978f1ef0afb15d354881 Mon Sep 17 00:00:00 2001 From: XMRig Date: Wed, 9 Oct 2019 12:58:11 +0700 Subject: [PATCH] Improved CPU profile generation. --- src/backend/common/Threads.h | 1 + src/backend/cpu/CpuConfig.cpp | 87 ++++------------ src/backend/cpu/CpuConfig.h | 3 +- src/backend/cpu/CpuConfig_gen.h | 149 ++++++++++++++++++++++++++++ src/backend/cpu/CpuThreads.cpp | 10 ++ src/backend/cpu/CpuThreads.h | 6 +- src/backend/cpu/cpu.cmake | 1 + src/backend/opencl/OclConfig_gen.h | 2 +- src/base/kernel/config/BaseConfig.h | 1 - src/core/config/Config.cpp | 10 +- 10 files changed, 190 insertions(+), 80 deletions(-) create mode 100644 src/backend/cpu/CpuConfig_gen.h diff --git a/src/backend/common/Threads.h b/src/backend/common/Threads.h index 7a01bad7..59b04fc9 100644 --- a/src/backend/common/Threads.h +++ b/src/backend/common/Threads.h @@ -48,6 +48,7 @@ public: inline bool isExist(const Algorithm &algo) const { return isDisabled(algo) || m_aliases.count(algo) > 0 || has(algo.shortName()); } inline const T &get(const Algorithm &algo, bool strict = false) const { return get(profileName(algo, strict)); } inline void disable(const Algorithm &algo) { m_disabled.insert(algo); } + inline void setAlias(const Algorithm &algo, const char *profile) { m_aliases[algo] = profile; } inline size_t move(const char *profile, T &&threads) { diff --git a/src/backend/cpu/CpuConfig.cpp b/src/backend/cpu/CpuConfig.cpp index adda40dc..e9abd9e6 100644 --- a/src/backend/cpu/CpuConfig.cpp +++ b/src/backend/cpu/CpuConfig.cpp @@ -23,15 +23,15 @@ */ -#include "backend/cpu/Cpu.h" #include "backend/cpu/CpuConfig.h" +#include "backend/cpu/CpuConfig_gen.h" +#include "backend/cpu/Cpu.h" #include "base/io/json/Json.h" #include "rapidjson/document.h" namespace xmrig { -static const char *kCn = "cn"; static const char *kEnabled = "enabled"; static const char *kHugePages = "huge-pages"; static const char *kHwAes = "hw-aes"; @@ -43,29 +43,7 @@ static const char *kPriority = "priority"; static const char *kAsm = "asm"; #endif -#ifdef XMRIG_ALGO_CN_GPU -static const char *kCnGPU = "cn/gpu"; -#endif - -#ifdef XMRIG_ALGO_CN_LITE -static const char *kCnLite = "cn-lite"; -#endif - -#ifdef XMRIG_ALGO_CN_HEAVY -static const char *kCnHeavy = "cn-heavy"; -#endif - -#ifdef XMRIG_ALGO_CN_PICO -static const char *kCnPico = "cn-pico"; -#endif - -#ifdef XMRIG_ALGO_RANDOMX -static const char *kRx = "rx"; -static const char *kRxWOW = "rx/wow"; -#endif - #ifdef XMRIG_ALGO_ARGON2 -static const char *kArgon2 = "argon2"; static const char *kArgon2Impl = "argon2-impl"; #endif @@ -136,7 +114,7 @@ std::vector xmrig::CpuConfig::get(const Miner *miner, cons } -void xmrig::CpuConfig::read(const rapidjson::Value &value, uint32_t version) +void xmrig::CpuConfig::read(const rapidjson::Value &value) { if (value.IsObject()) { m_enabled = Json::getBool(value, kEnabled, m_enabled); @@ -155,16 +133,14 @@ void xmrig::CpuConfig::read(const rapidjson::Value &value, uint32_t version) m_argon2Impl = Json::getString(value, kArgon2Impl); # endif - if (!m_threads.read(value)) { - generate(); - } + m_threads.read(value); - if (version == 0) { - generateArgon2(); - } + generate(); } - else if (value.IsBool() && value.IsFalse()) { - m_enabled = false; + else if (value.IsBool()) { + m_enabled = value.GetBool(); + + generate(); } else { generate(); @@ -174,43 +150,20 @@ void xmrig::CpuConfig::read(const rapidjson::Value &value, uint32_t version) void xmrig::CpuConfig::generate() { - m_shouldSave = true; - ICpuInfo *cpu = Cpu::info(); + if (!isEnabled() || m_threads.has("*")) { + return; + } - m_threads.disable(Algorithm::CN_0); - m_threads.move(kCn, cpu->threads(Algorithm::CN_0, m_limit)); + size_t count = 0; -# ifdef XMRIG_ALGO_CN_GPU - m_threads.move(kCnGPU, cpu->threads(Algorithm::CN_GPU, m_limit)); -# endif + count += xmrig::generate(m_threads, m_limit); + count += xmrig::generate(m_threads, m_limit); + count += xmrig::generate(m_threads, m_limit); + count += xmrig::generate(m_threads, m_limit); + count += xmrig::generate(m_threads, m_limit); + count += xmrig::generate(m_threads, m_limit); -# ifdef XMRIG_ALGO_CN_LITE - m_threads.disable(Algorithm::CN_LITE_0); - m_threads.move(kCnLite, cpu->threads(Algorithm::CN_LITE_1, m_limit)); -# endif - -# ifdef XMRIG_ALGO_CN_HEAVY - m_threads.move(kCnHeavy, cpu->threads(Algorithm::CN_HEAVY_0, m_limit)); -# endif - -# ifdef XMRIG_ALGO_CN_PICO - m_threads.move(kCnPico, cpu->threads(Algorithm::CN_PICO_0, m_limit)); -# endif - -# ifdef XMRIG_ALGO_RANDOMX - m_threads.move(kRx, cpu->threads(Algorithm::RX_0, m_limit)); - m_threads.move(kRxWOW, cpu->threads(Algorithm::RX_WOW, m_limit)); -# endif - - generateArgon2(); -} - - -void xmrig::CpuConfig::generateArgon2() -{ -# ifdef XMRIG_ALGO_ARGON2 - m_threads.move(kArgon2, Cpu::info()->threads(Algorithm::AR2_CHUKWA, m_limit)); -# endif + m_shouldSave = count > 0; } diff --git a/src/backend/cpu/CpuConfig.h b/src/backend/cpu/CpuConfig.h index 60d0a1c5..fa48e07b 100644 --- a/src/backend/cpu/CpuConfig.h +++ b/src/backend/cpu/CpuConfig.h @@ -50,7 +50,7 @@ public: rapidjson::Value toJSON(rapidjson::Document &doc) const; size_t memPoolSize() const; std::vector get(const Miner *miner, const Algorithm &algorithm) const; - void read(const rapidjson::Value &value, uint32_t version); + void read(const rapidjson::Value &value); inline bool isEnabled() const { return m_enabled; } inline bool isHugePages() const { return m_hugePages; } @@ -62,7 +62,6 @@ public: private: void generate(); - void generateArgon2(); void setAesMode(const rapidjson::Value &value); void setMemoryPool(const rapidjson::Value &value); diff --git a/src/backend/cpu/CpuConfig_gen.h b/src/backend/cpu/CpuConfig_gen.h new file mode 100644 index 00000000..be5f9efc --- /dev/null +++ b/src/backend/cpu/CpuConfig_gen.h @@ -0,0 +1,149 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 . + */ + +#ifndef XMRIG_CPUCONFIG_GEN_H +#define XMRIG_CPUCONFIG_GEN_H + + +#include "backend/common/Threads.h" +#include "backend/cpu/Cpu.h" +#include "backend/cpu/CpuThreads.h" + + +namespace xmrig { + + +static inline size_t generate(const char *key, Threads &threads, const Algorithm &algorithm, uint32_t limit) +{ + if (threads.isExist(algorithm) || threads.has(key)) { + return 0; + } + + return threads.move(key, Cpu::info()->threads(algorithm, limit)); +} + + +template +static inline size_t generate(Threads &, uint32_t) { return 0; } + + +template<> +size_t inline generate(Threads &threads, uint32_t limit) +{ + size_t count = 0; + + count += generate("cn", threads, Algorithm::CN_0, limit); + + if (!threads.isExist(Algorithm::CN_0)) { + threads.disable(Algorithm::CN_0); + ++count; + } + +# ifdef XMRIG_ALGO_CN_GPU + count += generate("cn/gpu", threads, Algorithm::CN_GPU, limit); +# endif + + return count; +} + + +#ifdef XMRIG_ALGO_CN_LITE +template<> +size_t inline generate(Threads &threads, uint32_t limit) +{ + size_t count = 0; + + count += generate("cn-lite", threads, Algorithm::CN_LITE_1, limit); + + if (!threads.isExist(Algorithm::CN_LITE_0)) { + threads.disable(Algorithm::CN_LITE_0); + ++count; + } + + return count; +} +#endif + + +#ifdef XMRIG_ALGO_CN_HEAVY +template<> +size_t inline generate(Threads &threads, uint32_t limit) +{ + return generate("cn-heavy", threads, Algorithm::CN_HEAVY_0, limit); +} +#endif + + +#ifdef XMRIG_ALGO_CN_PICO +template<> +size_t inline generate(Threads &threads, uint32_t limit) +{ + return generate("cn-pico", threads, Algorithm::CN_PICO_0, limit); +} +#endif + + +#ifdef XMRIG_ALGO_RANDOMX +template<> +size_t inline generate(Threads &threads, uint32_t limit) +{ + size_t count = 0; + + auto wow = Cpu::info()->threads(Algorithm::RX_WOW, limit); + + if (!threads.isExist(Algorithm::RX_ARQ)) { + auto arq = Cpu::info()->threads(Algorithm::RX_ARQ, limit); + if (arq == wow) { + threads.setAlias(Algorithm::RX_ARQ, "rx/wow"); + ++count; + } + else { + count += threads.move("rx/arq", std::move(arq)); + } + } + + if (!threads.isExist(Algorithm::RX_WOW)) { + count += threads.move("rx/wow", std::move(wow)); + } + + count += generate("rx", threads, Algorithm::RX_0, limit); + + return count; +} +#endif + + +#ifdef XMRIG_ALGO_ARGON2 +template<> +size_t inline generate(Threads &threads, uint32_t limit) +{ + return generate("argon2", threads, Algorithm::AR2_CHUKWA, limit); +} +#endif + + +} /* namespace xmrig */ + + +#endif /* XMRIG_CPUCONFIG_GEN_H */ diff --git a/src/backend/cpu/CpuThreads.cpp b/src/backend/cpu/CpuThreads.cpp index 5bd9cca9..416b4ecb 100644 --- a/src/backend/cpu/CpuThreads.cpp +++ b/src/backend/cpu/CpuThreads.cpp @@ -120,6 +120,16 @@ xmrig::CpuThreads::CpuThreads(size_t count, uint32_t intensity) } +bool xmrig::CpuThreads::isEqual(const CpuThreads &other) const +{ + if (isEmpty() && other.isEmpty()) { + return true; + } + + return count() == other.count() && std::equal(m_data.begin(), m_data.end(), other.m_data.begin()); +} + + rapidjson::Value xmrig::CpuThreads::toJSON(rapidjson::Document &doc) const { using namespace rapidjson; diff --git a/src/backend/cpu/CpuThreads.h b/src/backend/cpu/CpuThreads.h index 13cd725f..076670cd 100644 --- a/src/backend/cpu/CpuThreads.h +++ b/src/backend/cpu/CpuThreads.h @@ -38,7 +38,7 @@ namespace xmrig { class CpuThreads { public: - inline CpuThreads() {} + inline CpuThreads() = default; inline CpuThreads(size_t count) : m_data(count) {} CpuThreads(const rapidjson::Value &value); @@ -51,6 +51,10 @@ public: inline void add(int64_t affinity, uint32_t intensity) { add(CpuThread(affinity, intensity)); } inline void reserve(size_t capacity) { m_data.reserve(capacity); } + inline bool operator!=(const CpuThreads &other) const { return !isEqual(other); } + inline bool operator==(const CpuThreads &other) const { return isEqual(other); } + + bool isEqual(const CpuThreads &other) const; rapidjson::Value toJSON(rapidjson::Document &doc) const; private: diff --git a/src/backend/cpu/cpu.cmake b/src/backend/cpu/cpu.cmake index b6c8915b..cb543174 100644 --- a/src/backend/cpu/cpu.cmake +++ b/src/backend/cpu/cpu.cmake @@ -2,6 +2,7 @@ set(HEADERS_BACKEND_CPU src/backend/cpu/Cpu.h src/backend/cpu/CpuBackend.h src/backend/cpu/CpuConfig.h + src/backend/cpu/CpuConfig_gen.h src/backend/cpu/CpuLaunchData.cpp src/backend/cpu/CpuThread.h src/backend/cpu/CpuThreads.h diff --git a/src/backend/opencl/OclConfig_gen.h b/src/backend/opencl/OclConfig_gen.h index 593b43a0..d91e50e5 100644 --- a/src/backend/opencl/OclConfig_gen.h +++ b/src/backend/opencl/OclConfig_gen.h @@ -38,7 +38,7 @@ namespace xmrig { static inline size_t generate(const char *key, Threads &threads, const Algorithm &algorithm, const std::vector &devices) { - if (threads.isExist(algorithm)) { + if (threads.isExist(algorithm) || threads.has(key)) { return 0; } diff --git a/src/base/kernel/config/BaseConfig.h b/src/base/kernel/config/BaseConfig.h index b58c47b6..2e10c3ff 100644 --- a/src/base/kernel/config/BaseConfig.h +++ b/src/base/kernel/config/BaseConfig.h @@ -56,7 +56,6 @@ public: inline const String &apiId() const { return m_apiId; } inline const String &apiWorkerId() const { return m_apiWorkerId; } inline uint32_t printTime() const { return m_printTime; } - inline uint32_t version() const { return m_version; } inline bool isWatch() const override { return m_watch && !m_fileName.isNull(); } inline const String &fileName() const override { return m_fileName; } diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 17b0e166..7ef7197f 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -50,8 +50,7 @@ namespace xmrig { -static const char *kCPU = "cpu"; -static constexpr const uint32_t kVersion = 1; +static const char *kCPU = "cpu"; #ifdef XMRIG_ALGO_RANDOMX static const char *kRandomX = "randomx"; @@ -119,10 +118,6 @@ bool xmrig::Config::isShouldSave() const return false; } - if (version() < kVersion) { - return true; - } - # ifdef XMRIG_FEATURE_OPENCL if (cl().isShouldSave()) { return true; @@ -139,7 +134,7 @@ bool xmrig::Config::read(const IJsonReader &reader, const char *fileName) return false; } - d_ptr->cpu.read(reader.getValue(kCPU), version()); + d_ptr->cpu.read(reader.getValue(kCPU)); # ifdef XMRIG_ALGO_RANDOMX if (!d_ptr->rx.read(reader.getValue(kRandomX))) { @@ -170,7 +165,6 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const doc.AddMember("api", api, allocator); doc.AddMember("http", m_http.toJSON(doc), allocator); doc.AddMember("autosave", isAutoSave(), allocator); - doc.AddMember("version", kVersion, allocator); doc.AddMember("background", isBackground(), allocator); doc.AddMember("colors", Log::colors, allocator);