diff --git a/CMakeLists.txt b/CMakeLists.txt index 33d3ad702..c315e1cf5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,24 +171,32 @@ if (WITH_RANDOMX) set(SOURCES_CRYPTO "${SOURCES_CRYPTO}" src/crypto/randomx/aes_hash.cpp + src/crypto/randomx/allocator.cpp + src/crypto/randomx/argon2_core.c src/crypto/randomx/argon2_ref.c + src/crypto/randomx/blake2_generator.cpp + src/crypto/randomx/blake2/blake2b.c src/crypto/randomx/bytecode_machine.cpp src/crypto/randomx/dataset.cpp - src/crypto/randomx/soft_aes.cpp - src/crypto/randomx/virtual_memory.cpp - src/crypto/randomx/vm_interpreted.cpp - src/crypto/randomx/allocator.cpp + src/crypto/randomx/instructions_portable.cpp src/crypto/randomx/randomx.cpp + src/crypto/randomx/reciprocal.c + src/crypto/randomx/soft_aes.cpp src/crypto/randomx/superscalar.cpp + src/crypto/randomx/virtual_machine.cpp + src/crypto/randomx/virtual_memory.cpp + src/crypto/randomx/vm_compiled_light.cpp src/crypto/randomx/vm_compiled.cpp src/crypto/randomx/vm_interpreted_light.cpp - src/crypto/randomx/argon2_core.c - src/crypto/randomx/blake2_generator.cpp - src/crypto/randomx/instructions_portable.cpp - src/crypto/randomx/reciprocal.c - src/crypto/randomx/virtual_machine.cpp - src/crypto/randomx/vm_compiled_light.cpp - src/crypto/randomx/blake2/blake2b.c + src/crypto/randomx/vm_interpreted.cpp + src/crypto/rx/Rx.cpp + src/crypto/rx/Rx.h + src/crypto/rx/RxAlgo.cpp + src/crypto/rx/RxAlgo.h + src/crypto/rx/RxCache.cpp + src/crypto/rx/RxCache.h + src/crypto/rx/RxDataset.cpp + src/crypto/rx/RxDataset.h ) if (NOT ARCH_ID) set(ARCH_ID ${CMAKE_HOST_SYSTEM_PROCESSOR}) diff --git a/src/base/io/log/Log.h b/src/base/io/log/Log.h index a14ffded4..962d1dbad 100644 --- a/src/base/io/log/Log.h +++ b/src/base/io/log/Log.h @@ -81,6 +81,9 @@ private: #define WHITE_S CSI "0;37m" // another name for LT.GRAY #define WHITE_BOLD_S CSI "1;37m" // actually white +#define BLUE_BG_S CSI "44m" +#define BLUE_BG_BOLD_S CSI "44;1m" + //color wrappings #define BLACK(x) BLACK_S x CLEAR #define BLACK_BOLD(x) BLACK_BOLD_S x CLEAR @@ -99,6 +102,9 @@ private: #define WHITE(x) WHITE_S x CLEAR #define WHITE_BOLD(x) WHITE_BOLD_S x CLEAR +#define BLUE_BG(x) BLUE_BG_S x CLEAR +#define BLUE_BG_BOLD(x) BLUE_BG_BOLD_S x CLEAR + #define LOG_EMERG(x, ...) xmrig::Log::print(xmrig::Log::EMERG, x, ##__VA_ARGS__) #define LOG_ALERT(x, ...) xmrig::Log::print(xmrig::Log::ALERT, x, ##__VA_ARGS__) diff --git a/src/core/config/Config.cpp b/src/core/config/Config.cpp index 87abbb913..784e171c7 100644 --- a/src/core/config/Config.cpp +++ b/src/core/config/Config.cpp @@ -119,7 +119,7 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const bool xmrig::Config::finalize() { - Algorithm algorithm(Algorithm::CN_0); // FIXME algo + Algorithm algorithm(Algorithm::RX_WOW); // FIXME algo if (!m_threads.cpu.empty()) { m_threads.mode = Advanced; diff --git a/src/crypto/common/Algorithm.cpp b/src/crypto/common/Algorithm.cpp index ab63204f3..2c259d321 100644 --- a/src/crypto/common/Algorithm.cpp +++ b/src/crypto/common/Algorithm.cpp @@ -32,6 +32,7 @@ #include "crypto/cn/CnAlgo.h" #include "crypto/common/Algorithm.h" +#include "crypto/rx/RxAlgo.h" #include "rapidjson/document.h" @@ -138,12 +139,12 @@ size_t xmrig::Algorithm::memory() const } # ifdef XMRIG_ALGO_RANDOMX - if (m_id == RX_WOW) { - return 0x100000; + if (f == RANDOM_X) { + return RxAlgo::l3(m_id); } # endif - return 0x200000; + return 0; } diff --git a/src/crypto/common/VirtualMemory.h b/src/crypto/common/VirtualMemory.h index e8acb017e..a83c35ed2 100644 --- a/src/crypto/common/VirtualMemory.h +++ b/src/crypto/common/VirtualMemory.h @@ -44,6 +44,8 @@ public: static void freeLargePagesMemory(void *p, size_t size); static void protectExecutableMemory(void *p, size_t size); static void unprotectExecutableMemory(void *p, size_t size); + + static inline constexpr size_t align(size_t pos, size_t align = 2097152) { return ((pos - 1) / align + 1) * align; } }; diff --git a/src/crypto/common/VirtualMemory_win.cpp b/src/crypto/common/VirtualMemory_win.cpp index dd6be14f4..7f1d6f436 100644 --- a/src/crypto/common/VirtualMemory_win.cpp +++ b/src/crypto/common/VirtualMemory_win.cpp @@ -32,15 +32,6 @@ #include "crypto/common/VirtualMemory.h" -namespace xmrig { - -constexpr size_t align(size_t pos, size_t align) { - return ((pos - 1) / align + 1) * align; -} - -} - - void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size) { return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); diff --git a/src/crypto/rx/Rx.cpp b/src/crypto/rx/Rx.cpp new file mode 100644 index 000000000..735169e2e --- /dev/null +++ b/src/crypto/rx/Rx.cpp @@ -0,0 +1,134 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 tevador + * 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 . + */ + + +#include +#include + + +#include "backend/cpu/Cpu.h" +#include "base/io/log/Log.h" +#include "base/tools/Buffer.h" +#include "base/tools/Chrono.h" +#include "crypto/rx/Rx.h" +#include "crypto/rx/RxCache.h" +#include "crypto/rx/RxDataset.h" + + +namespace xmrig { + + +class RxPrivate +{ +public: + inline RxPrivate() + { + uv_mutex_init(&mutex); + } + + + inline ~RxPrivate() + { + delete dataset; + uv_mutex_destroy(&mutex); + } + + + inline void lock() { uv_mutex_lock(&mutex); } + inline void unlock() { uv_mutex_unlock(&mutex); } + + + RxDataset *dataset = nullptr; + uint32_t initThreads = std::thread::hardware_concurrency(); + uv_mutex_t mutex; +}; + + +static RxPrivate *d_ptr = new RxPrivate(); +static const char *tag = BLUE_BG(" rx "); + + +} // namespace xmrig + + +xmrig::RxDataset *xmrig::Rx::dataset(const uint8_t *seed, const Algorithm &algorithm, bool hugePages) +{ + d_ptr->lock(); + + if (!d_ptr->dataset) { + const uint64_t ts = Chrono::steadyMSecs(); + + LOG_INFO("%s" MAGENTA_BOLD(" allocate") CYAN_BOLD(" %zu MiB") BLACK_BOLD(" (%zu+%zu) for RandomX dataset & cache"), + tag, + (RxDataset::size() + RxCache::size()) / 1024 / 1024, + RxDataset::size() / 1024 / 1024, + RxCache::size() / 1024 / 1024 + ); + + d_ptr->dataset = new RxDataset(hugePages); + + const auto hugePages = d_ptr->dataset->hugePages(); + const double percent = hugePages.first == 0 ? 0.0 : static_cast(hugePages.first) / hugePages.second * 100.0; + + LOG_INFO("%s" GREEN(" allocate done") " huge pages %s%u/%u %1.0f%%" CLEAR " %sJIT" BLACK_BOLD(" (%" PRIu64 " ms)"), + tag, + (hugePages.first == hugePages.second ? GREEN_BOLD_S : (hugePages.first == 0 ? RED_BOLD_S : YELLOW_BOLD_S)), + hugePages.first, + hugePages.second, + percent, + d_ptr->dataset->cache()->isJIT() ? GREEN_BOLD_S "+" : RED_BOLD_S "-", + Chrono::steadyMSecs() - ts + ); + } + + if (!d_ptr->dataset->isReady(seed, algorithm)) { + const uint64_t ts = Chrono::steadyMSecs(); + + LOG_INFO("%s" MAGENTA_BOLD(" init dataset") " algo " WHITE_BOLD("%s") " threads " WHITE_BOLD("%u") BLACK_BOLD(" seed %s..."), + tag, + algorithm.shortName(), + d_ptr->initThreads, + Buffer::toHex(seed, 8).data() + ); + + d_ptr->dataset->init(seed, algorithm, d_ptr->initThreads); + + LOG_INFO("%s" GREEN(" init done") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, Chrono::steadyMSecs() - ts); + } + + RxDataset *dataset = d_ptr->dataset; + d_ptr->unlock(); + + return dataset; +} + + +void xmrig::Rx::stop() +{ + delete d_ptr; + + d_ptr = nullptr; +} diff --git a/src/crypto/rx/Rx.h b/src/crypto/rx/Rx.h new file mode 100644 index 000000000..c9d068c67 --- /dev/null +++ b/src/crypto/rx/Rx.h @@ -0,0 +1,53 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 tevador + * 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_RX_H +#define XMRIG_RX_H + + +#include + + +namespace xmrig +{ + + +class Algorithm; +class RxDataset; + + +class Rx +{ +public: + static RxDataset *dataset(const uint8_t *seed, const Algorithm &algorithm, bool hugePages = true); + static void stop(); +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_RX_H */ diff --git a/src/crypto/rx/RxAlgo.cpp b/src/crypto/rx/RxAlgo.cpp new file mode 100644 index 000000000..b0e92e6e0 --- /dev/null +++ b/src/crypto/rx/RxAlgo.cpp @@ -0,0 +1,69 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 tevador + * 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 . + */ + + +#include "crypto/randomx/randomx.h" +#include "crypto/rx/RxAlgo.h" + + +xmrig::Algorithm::Id xmrig::RxAlgo::apply(Algorithm::Id algorithm) +{ + switch (algorithm) { + case Algorithm::RX_WOW: + randomx_apply_config(RandomX_WowneroConfig); + break; + + case Algorithm::RX_LOKI: + randomx_apply_config(RandomX_LokiConfig); + break; + + default: + randomx_apply_config(RandomX_MoneroConfig); + break; + } + + return algorithm; +} + + +size_t xmrig::RxAlgo::l3(Algorithm::Id algorithm) +{ + switch (algorithm) { + case Algorithm::RX_0: + return RandomX_MoneroConfig.ScratchpadL3_Size; + + case Algorithm::RX_WOW: + return RandomX_WowneroConfig.ScratchpadL3_Size; + + case Algorithm::RX_LOKI: + return RandomX_LokiConfig.ScratchpadL3_Size; + + default: + break; + } + + return 0; +} diff --git a/src/crypto/rx/RxAlgo.h b/src/crypto/rx/RxAlgo.h new file mode 100644 index 000000000..dd3f0aa73 --- /dev/null +++ b/src/crypto/rx/RxAlgo.h @@ -0,0 +1,56 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 tevador + * 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_RX_ALGO_H +#define XMRIG_RX_ALGO_H + + +#include +#include + + +#include "crypto/common/Algorithm.h" + + +struct RandomX_ConfigurationBase; + + +namespace xmrig +{ + + +class RxAlgo +{ +public: + static Algorithm::Id apply(Algorithm::Id algorithm); + static size_t l3(Algorithm::Id algorithm); +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_RX_ALGO_H */ diff --git a/src/crypto/rx/RxCache.cpp b/src/crypto/rx/RxCache.cpp new file mode 100644 index 000000000..a5e9efb3a --- /dev/null +++ b/src/crypto/rx/RxCache.cpp @@ -0,0 +1,81 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 tevador + * 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 . + */ + + +#include "crypto/randomx/randomx.h" +#include "crypto/rx/RxCache.h" + + +static_assert(RANDOMX_FLAG_JIT == 8, "RANDOMX_FLAG_JIT flag mismatch"); +static_assert(RANDOMX_FLAG_LARGE_PAGES == 1, "RANDOMX_FLAG_LARGE_PAGES flag mismatch"); + + + +xmrig::RxCache::RxCache(bool hugePages) : + m_seed() +{ + if (hugePages) { + m_flags = RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES; + m_cache = randomx_alloc_cache(static_cast(m_flags)); + } + + if (!m_cache) { + m_flags = RANDOMX_FLAG_JIT; + m_cache = randomx_alloc_cache(static_cast(m_flags)); + } + + if (!m_cache) { + m_flags = RANDOMX_FLAG_DEFAULT; + m_cache = randomx_alloc_cache(static_cast(m_flags)); + } +} + + +xmrig::RxCache::~RxCache() +{ + if (m_cache) { + randomx_release_cache(m_cache); + } +} + + +bool xmrig::RxCache::init(const void *seed) +{ + if (isReady(seed)) { + return false; + } + + memcpy(m_seed, seed, sizeof(m_seed)); + randomx_init_cache(m_cache, m_seed, sizeof(m_seed)); + + return true; +} + + +bool xmrig::RxCache::isReady(const void *seed) const +{ + return memcmp(m_seed, seed, sizeof(m_seed)) == 0; +} diff --git a/src/crypto/rx/RxCache.h b/src/crypto/rx/RxCache.h new file mode 100644 index 000000000..893ebf06e --- /dev/null +++ b/src/crypto/rx/RxCache.h @@ -0,0 +1,70 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 tevador + * 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_RX_CACHE_H +#define XMRIG_RX_CACHE_H + + +#include + + +#include "crypto/randomx/configuration.h" + + +struct randomx_cache; + + +namespace xmrig +{ + + +class RxCache +{ +public: + RxCache(bool hugePages = true); + ~RxCache(); + + inline bool isHugePages() const { return m_flags & 1; } + inline bool isJIT() const { return m_flags & 8; } + inline const uint8_t *seed() const { return m_seed; } + inline randomx_cache *get() const { return m_cache; } + + bool init(const void *seed); + bool isReady(const void *seed) const; + + static inline constexpr size_t size() { return RANDOMX_CACHE_MAX_SIZE; } + +private: + int m_flags = 0; + randomx_cache *m_cache = nullptr; + uint8_t m_seed[32]; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_RX_CACHE_H */ diff --git a/src/crypto/rx/RxDataset.cpp b/src/crypto/rx/RxDataset.cpp new file mode 100644 index 000000000..5c3b9f375 --- /dev/null +++ b/src/crypto/rx/RxDataset.cpp @@ -0,0 +1,124 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 tevador + * 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 . + */ + + +#include + + +#include "crypto/common/VirtualMemory.h" +#include "crypto/randomx/randomx.h" +#include "crypto/rx/RxAlgo.h" +#include "crypto/rx/RxCache.h" +#include "crypto/rx/RxDataset.h" + + +static_assert(RANDOMX_FLAG_LARGE_PAGES == 1, "RANDOMX_FLAG_LARGE_PAGES flag mismatch"); + + +xmrig::RxDataset::RxDataset(bool hugePages) +{ + if (hugePages) { + m_flags = RANDOMX_FLAG_LARGE_PAGES; + m_dataset = randomx_alloc_dataset(static_cast(m_flags)); + } + + if (!m_dataset) { + m_flags = RANDOMX_FLAG_DEFAULT; + m_dataset = randomx_alloc_dataset(static_cast(m_flags)); + } + + m_cache = new RxCache(hugePages); +} + + +xmrig::RxDataset::~RxDataset() +{ + if (m_dataset) { + randomx_release_dataset(m_dataset); + } + + delete m_cache; +} + + +bool xmrig::RxDataset::init(const void *seed, const Algorithm &algorithm, uint32_t numThreads) +{ + if (isReady(seed, algorithm)) { + return false; + } + + if (m_algorithm != algorithm) { + m_algorithm = RxAlgo::apply(algorithm); + } + + cache()->init(seed); + + const uint32_t datasetItemCount = randomx_dataset_item_count(); + + if (numThreads > 1) { + std::vector threads; + threads.reserve(numThreads); + + for (uint32_t i = 0; i < numThreads; ++i) { + const uint32_t a = (datasetItemCount * i) / numThreads; + const uint32_t b = (datasetItemCount * (i + 1)) / numThreads; + threads.emplace_back(randomx_init_dataset, m_dataset, m_cache->get(), a, b - a); + } + + for (uint32_t i = 0; i < numThreads; ++i) { + threads[i].join(); + } + } + else { + randomx_init_dataset(m_dataset, m_cache->get(), 0, datasetItemCount); + } + + return true; +} + + +bool xmrig::RxDataset::isReady(const void *seed, const Algorithm &algorithm) const +{ + return algorithm == m_algorithm && cache()->isReady(seed); +} + + +std::pair xmrig::RxDataset::hugePages() const +{ + constexpr size_t twoMiB = 2u * 1024u * 1024u; + constexpr const size_t total = (VirtualMemory::align(size(), twoMiB) + VirtualMemory::align(RxCache::size(), twoMiB)) / twoMiB; + + size_t count = 0; + if (isHugePages()) { + count += VirtualMemory::align(size(), twoMiB) / twoMiB; + } + + if (m_cache->isHugePages()) { + count += VirtualMemory::align(RxCache::size(), twoMiB) / twoMiB; + } + + return std::pair(count, total); +} diff --git a/src/crypto/rx/RxDataset.h b/src/crypto/rx/RxDataset.h new file mode 100644 index 000000000..7944d52c5 --- /dev/null +++ b/src/crypto/rx/RxDataset.h @@ -0,0 +1,72 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2019 XMR-Stak , + * Copyright 2018 Lee Clagett + * Copyright 2018-2019 tevador + * 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_RX_DATASET_H +#define XMRIG_RX_DATASET_H + + +#include "crypto/common/Algorithm.h" +#include "crypto/randomx/configuration.h" + + +struct randomx_dataset; + + +namespace xmrig +{ + + +class RxCache; + + +class RxDataset +{ +public: + RxDataset(bool hugePages = true); + ~RxDataset(); + + inline bool isHugePages() const { return m_flags & 1; } + inline randomx_dataset *get() const { return m_dataset; } + inline RxCache *cache() const { return m_cache; } + + bool init(const void *seed, const Algorithm &algorithm, uint32_t numThreads); + bool isReady(const void *seed, const Algorithm &algorithm) const; + std::pair hugePages() const; + + static inline constexpr size_t size() { return RANDOMX_DATASET_MAX_SIZE; } + +private: + Algorithm m_algorithm; + int m_flags = 0; + randomx_dataset *m_dataset = nullptr; + RxCache *m_cache = nullptr; +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_RX_DATASET_H */ diff --git a/src/net/JobResults.cpp b/src/net/JobResults.cpp index 8c65b5e4f..bf0b5e869 100644 --- a/src/net/JobResults.cpp +++ b/src/net/JobResults.cpp @@ -54,6 +54,8 @@ public: inline ~JobResultsPrivate() { Handle::close(m_async); + + uv_mutex_destroy(&m_mutex); } diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 2565e7c41..059a7171f 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -28,6 +28,8 @@ #include "crypto/cn/CryptoNight_test.h" +#include "crypto/rx/Rx.h" +#include "crypto/rx/RxDataset.h" #include "net/JobResults.h" #include "workers/CpuThreadLegacy.h" #include "workers/MultiWorker.h" @@ -67,9 +69,11 @@ void xmrig::MultiWorker::allocateRandomX_VM() flags |= RANDOMX_FLAG_HARD_AES; } - m_rx_vm = randomx_create_vm(static_cast(flags), nullptr, Workers::getDataset()); + RxDataset *dataset = Rx::dataset(m_state.job.seedHash(), m_state.job.algorithm()); + + m_rx_vm = randomx_create_vm(static_cast(flags), nullptr, dataset->get()); if (!m_rx_vm) { - m_rx_vm = randomx_create_vm(static_cast(flags - RANDOMX_FLAG_LARGE_PAGES), nullptr, Workers::getDataset()); + m_rx_vm = randomx_create_vm(static_cast(flags - RANDOMX_FLAG_LARGE_PAGES), nullptr, dataset->get()); } } } @@ -160,7 +164,6 @@ void xmrig::MultiWorker::start() # ifdef XMRIG_ALGO_RANDOMX if (m_state.job.algorithm().family() == Algorithm::RANDOM_X) { allocateRandomX_VM(); - Workers::updateDataset(m_state.job.seedHash(), m_totalWays, m_state.job.algorithm()); randomx_calculate_hash(m_rx_vm, m_state.blob, m_state.job.size(), m_hash); } else diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 88d73a0b6..58cccd9eb 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -29,9 +29,13 @@ #include "api/Api.h" #include "base/io/log/Log.h" +#include "base/tools/Chrono.h" #include "base/tools/Handle.h" #include "core/config/Config.h" #include "core/Controller.h" +#include "crypto/rx/RxAlgo.h" +#include "crypto/rx/RxCache.h" +#include "crypto/rx/RxDataset.h" #include "interfaces/IThread.h" #include "Mem.h" #include "rapidjson/document.h" @@ -55,15 +59,6 @@ uv_rwlock_t Workers::m_rwlock; uv_timer_t *Workers::m_timer = nullptr; xmrig::Controller *Workers::m_controller = nullptr; -#ifdef XMRIG_ALGO_RANDOMX -uv_rwlock_t Workers::m_rx_dataset_lock; -randomx_cache *Workers::m_rx_cache = nullptr; -randomx_dataset *Workers::m_rx_dataset = nullptr; -uint8_t Workers::m_rx_seed_hash[32] = {}; -xmrig::Algorithm Workers::m_rx_algo; -std::atomic Workers::m_rx_dataset_init_thread_counter = {}; -#endif - xmrig::Job Workers::job() { @@ -176,7 +171,7 @@ void Workers::start(xmrig::Controller *controller) m_controller = controller; const std::vector &threads = controller->config()->threads(); - m_status.algo = xmrig::Algorithm::CN_0; // FIXME algo + m_status.algo = xmrig::Algorithm::RX_WOW; // FIXME algo m_status.threads = threads.size(); for (const xmrig::IThread *thread : threads) { @@ -188,10 +183,6 @@ void Workers::start(xmrig::Controller *controller) uv_mutex_init(&m_mutex); uv_rwlock_init(&m_rwlock); -# ifdef XMRIG_ALGO_RANDOMX - uv_rwlock_init(&m_rx_dataset_lock); -# endif - m_sequence = 1; m_paused = 1; @@ -335,92 +326,3 @@ void Workers::start(IWorker *worker) worker->start(); } - - -#ifdef XMRIG_ALGO_RANDOMX -void Workers::updateDataset(const uint8_t* seed_hash, const uint32_t num_threads, const xmrig::Algorithm &algorithm) -{ - // Check if we need to update cache and dataset - if ((memcmp(m_rx_seed_hash, seed_hash, sizeof(m_rx_seed_hash)) == 0) && (m_rx_algo == algorithm)) - return; - - const uint32_t thread_id = m_rx_dataset_init_thread_counter++; - LOG_DEBUG("Thread %u started updating RandomX dataset", thread_id); - - // Wait for all threads to get here - do { - if (m_sequence.load(std::memory_order_relaxed) == 0) { - // Exit immediately if workers were stopped - return; - } - std::this_thread::yield(); - } while (m_rx_dataset_init_thread_counter.load() != num_threads); - - // One of the threads updates cache - uv_rwlock_wrlock(&m_rx_dataset_lock); - - if (m_rx_algo != algorithm) { - switch (algorithm) { - case xmrig::Algorithm::RX_WOW: - randomx_apply_config(RandomX_WowneroConfig); - break; - - case xmrig::Algorithm::RX_LOKI: - randomx_apply_config(RandomX_LokiConfig); - break; - - default: - randomx_apply_config(RandomX_MoneroConfig); - break; - } - - m_rx_algo = algorithm; - } - - if (memcmp(m_rx_seed_hash, seed_hash, sizeof(m_rx_seed_hash)) != 0) { - memcpy(m_rx_seed_hash, seed_hash, sizeof(m_rx_seed_hash)); - randomx_init_cache(m_rx_cache, m_rx_seed_hash, sizeof(m_rx_seed_hash)); - } - - uv_rwlock_wrunlock(&m_rx_dataset_lock); - - // All threads update dataset - const uint32_t a = (randomx_dataset_item_count() * thread_id) / num_threads; - const uint32_t b = (randomx_dataset_item_count() * (thread_id + 1)) / num_threads; - randomx_init_dataset(m_rx_dataset, m_rx_cache, a, b - a); - - LOG_DEBUG("Thread %u finished updating RandomX dataset", thread_id); - - // Wait for all threads to complete - --m_rx_dataset_init_thread_counter; - do { - if (m_sequence.load(std::memory_order_relaxed) == 0) { - // Exit immediately if workers were stopped - return; - } - std::this_thread::yield(); - } while (m_rx_dataset_init_thread_counter.load() != 0); -} - -randomx_dataset* Workers::getDataset() -{ - if (m_rx_dataset) - return m_rx_dataset; - - uv_rwlock_wrlock(&m_rx_dataset_lock); - if (!m_rx_dataset) { - randomx_dataset* dataset = randomx_alloc_dataset(RANDOMX_FLAG_LARGE_PAGES); - if (!dataset) { - dataset = randomx_alloc_dataset(RANDOMX_FLAG_DEFAULT); - } - m_rx_cache = randomx_alloc_cache(static_cast(RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES)); - if (!m_rx_cache) { - m_rx_cache = randomx_alloc_cache(RANDOMX_FLAG_JIT); - } - m_rx_dataset = dataset; - } - uv_rwlock_wrunlock(&m_rx_dataset_lock); - - return m_rx_dataset; -} -#endif diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 39e872b5a..8619f973f 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -73,11 +73,6 @@ public: static void threadsSummary(rapidjson::Document &doc); # endif -# ifdef XMRIG_ALGO_RANDOMX - static void updateDataset(const uint8_t* seed_hash, uint32_t num_threads, const xmrig::Algorithm &algorithm); - static randomx_dataset* getDataset(); -# endif - private: static void onReady(void *arg); static void onTick(uv_timer_t *handle); @@ -115,15 +110,6 @@ private: static uv_rwlock_t m_rwlock; static uv_timer_t *m_timer; static xmrig::Controller *m_controller; - -# ifdef XMRIG_ALGO_RANDOMX - static uv_rwlock_t m_rx_dataset_lock; - static randomx_cache *m_rx_cache; - static randomx_dataset *m_rx_dataset; - static uint8_t m_rx_seed_hash[32]; - static xmrig::Algorithm m_rx_algo; - static std::atomic m_rx_dataset_init_thread_counter; -# endif };