diff --git a/src/backend/cpu/CpuWorker.cpp b/src/backend/cpu/CpuWorker.cpp index cd804199..000d7061 100644 --- a/src/backend/cpu/CpuWorker.cpp +++ b/src/backend/cpu/CpuWorker.cpp @@ -82,7 +82,9 @@ xmrig::CpuWorker::~CpuWorker() template void xmrig::CpuWorker::allocateRandomX_VM() { - while (!Rx::isReady(m_job.currentJob(), m_node)) { + RxDataset *dataset = Rx::dataset(m_job.currentJob(), m_node); + + while (dataset == nullptr) { std::this_thread::sleep_for(std::chrono::milliseconds(200)); if (Nonce::sequence(Nonce::CPU) == 0) { @@ -90,13 +92,6 @@ void xmrig::CpuWorker::allocateRandomX_VM() } } - RxDataset *dataset = Rx::dataset(m_node); - assert(dataset != nullptr); - - if (!dataset) { - return; - } - if (!m_vm) { m_vm = new RxVm(dataset, m_memory->scratchpad(), !m_hwAES); } diff --git a/src/base/io/log/Log.cpp b/src/base/io/log/Log.cpp index 22972a7e..4e3bd5a5 100644 --- a/src/base/io/log/Log.cpp +++ b/src/base/io/log/Log.cpp @@ -31,6 +31,7 @@ #include +#include #include #include #include @@ -69,14 +70,11 @@ public: inline LogPrivate() : m_buf() { - uv_mutex_init(&m_mutex); } inline ~LogPrivate() { - uv_mutex_destroy(&m_mutex); - for (ILogBackend *backend : m_backends) { delete backend; } @@ -91,13 +89,14 @@ public: size_t size = 0; size_t offset = 0; - lock(); + std::lock_guard lock(m_mutex); + timestamp(level, size, offset); color(level, size); const int rc = vsnprintf(m_buf + size, sizeof (m_buf) - offset - 32, fmt, args); if (rc < 0) { - return unlock(); + return; } size += std::min(static_cast(rc), sizeof (m_buf) - offset - 32); @@ -119,16 +118,10 @@ public: fputs(txt.c_str(), stdout); fflush(stdout); } - - unlock(); } private: - inline void lock() { uv_mutex_lock(&m_mutex); } - inline void unlock() { uv_mutex_unlock(&m_mutex); } - - inline void timestamp(Log::Level level, size_t &size, size_t &offset) { if (level == Log::NONE) { @@ -192,8 +185,8 @@ private: char m_buf[4096]; + std::mutex m_mutex; std::vector m_backends; - uv_mutex_t m_mutex; }; diff --git a/src/base/tools/Buffer.h b/src/base/tools/Buffer.h index 6b720357..28f92b9e 100644 --- a/src/base/tools/Buffer.h +++ b/src/base/tools/Buffer.h @@ -43,17 +43,20 @@ public: ~Buffer(); - inline char *data() { return m_data; } - inline const char *data() const { return m_data; } - inline size_t size() const { return m_size; } - inline void from(const Buffer &other) { from(other.data(), other.size()); } + inline bool isEqual(const Buffer &other) const { return m_size == other.m_size && (m_size == 0 || memcmp(m_data, other.m_data, m_size) == 0); } + inline char *data() { return m_data; } + inline const char *data() const { return m_data; } + inline size_t size() const { return m_size; } + inline void from(const Buffer &other) { from(other.data(), other.size()); } void from(const char *data, size_t size); - inline Buffer &operator=(const Buffer &other) { from(other); return *this; } - inline Buffer &operator=(Buffer &&other) { move(std::move(other)); return *this; } + inline bool operator!=(const Buffer &other) const { return !isEqual(other); } + inline bool operator==(const Buffer &other) const { return isEqual(other); } + inline Buffer &operator=(Buffer &&other) { move(std::move(other)); return *this; } + inline Buffer &operator=(const Buffer &other) { from(other); return *this; } static Buffer allocUnsafe(size_t size); diff --git a/src/core/Miner.cpp b/src/core/Miner.cpp index 1bca9fc8..ab4a8ef6 100644 --- a/src/core/Miner.cpp +++ b/src/core/Miner.cpp @@ -380,7 +380,7 @@ void xmrig::Miner::setJob(const Job &job, bool donate) } # ifdef XMRIG_ALGO_RANDOMX - Rx::init(job, + Rx::init(d_ptr->job, d_ptr->controller->config()->rx().threads(), d_ptr->controller->config()->cpu().isHugePages(), d_ptr->controller->config()->rx().isNUMA() diff --git a/src/crypto/rx/Rx.cpp b/src/crypto/rx/Rx.cpp index 676eca2c..5f5414c9 100644 --- a/src/crypto/rx/Rx.cpp +++ b/src/crypto/rx/Rx.cpp @@ -43,6 +43,7 @@ #include "base/tools/Buffer.h" #include "base/tools/Chrono.h" #include "crypto/rx/Rx.h" +#include "crypto/rx/RxAlgo.h" #include "crypto/rx/RxCache.h" #include "crypto/rx/RxDataset.h" @@ -57,11 +58,50 @@ static const char *tag = BLUE_BG(WHITE_BOLD_S " rx ") " "; static RxPrivate *d_ptr = nullptr; +#ifdef XMRIG_FEATURE_HWLOC +static void bindToNUMANode(uint32_t nodeId) +{ + hwloc_topology_t topology; + hwloc_topology_init(&topology); + hwloc_topology_load(topology); + + hwloc_obj_t node = hwloc_get_numanode_obj_by_os_index(topology, nodeId); + if (node) { + if (HwlocCpuInfo::has(HwlocCpuInfo::SET_THISTHREAD_MEMBIND)) { +# if HWLOC_API_VERSION >= 0x20000 + hwloc_set_membind(topology, node->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_THREAD | HWLOC_MEMBIND_BYNODESET); +# else + hwloc_set_membind_nodeset(topology, node->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_THREAD); +# endif + } + + Platform::setThreadAffinity(static_cast(hwloc_bitmap_first(node->cpuset))); + } + + hwloc_topology_destroy(topology); +} +#else +inline static void bindToNUMANode(uint32_t) {} +#endif + + class RxPrivate { public: - inline RxPrivate() + inline RxPrivate() : + m_seed() { +# ifdef XMRIG_FEATURE_HWLOC + if (Cpu::info()->nodes() > 1) { + for (uint32_t nodeId : HwlocCpuInfo::nodeIndexes()) { + datasets.insert({ nodeId, nullptr }); + } + } + else +# endif + { + datasets.insert({ 0, nullptr }); + } } @@ -75,32 +115,19 @@ public: } + inline bool isNUMA() const { return m_numa; } + inline const Algorithm &algorithm() const { return m_algorithm; } + inline const uint8_t *seed() const { return m_seed; } + inline size_t count() const { return isNUMA() ? datasets.size() : 1; } + + static void allocate(uint32_t nodeId) { const uint64_t ts = Chrono::steadyMSecs(); -# ifdef XMRIG_FEATURE_HWLOC - if (d_ptr->numa) { - hwloc_topology_t topology; - hwloc_topology_init(&topology); - hwloc_topology_load(topology); - - hwloc_obj_t node = hwloc_get_numanode_obj_by_os_index(topology, nodeId); - if (node) { - if (HwlocCpuInfo::has(HwlocCpuInfo::SET_THISTHREAD_MEMBIND)) { -# if HWLOC_API_VERSION >= 0x20000 - hwloc_set_membind(topology, node->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_THREAD | HWLOC_MEMBIND_BYNODESET); -# else - hwloc_set_membind_nodeset(topology, node->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_THREAD); -# endif - } - - Platform::setThreadAffinity(static_cast(hwloc_bitmap_first(node->cpuset))); - } - - hwloc_topology_destroy(topology); + if (d_ptr->isNUMA()) { + bindToNUMANode(nodeId); } -# endif LOG_INFO("%s" CYAN_BOLD("#%u") MAGENTA_BOLD(" allocate") CYAN_BOLD(" %zu MB") BLACK_BOLD(" (%zu+%zu) for RandomX dataset & cache"), tag, @@ -110,7 +137,7 @@ public: RxCache::size() / 1024 / 1024 ); - RxDataset *dataset = new RxDataset(d_ptr->hugePages); + RxDataset *dataset = new RxDataset(d_ptr->m_hugePages); d_ptr->datasets[nodeId] = dataset; if (dataset->get() != nullptr) { @@ -134,29 +161,84 @@ public: } - bool hugePages = true; - bool numa = true; + static void initDataset(uint32_t nodeId, uint32_t threads) + { + std::lock_guard lock(d_ptr->mutex); + + const uint64_t ts = Chrono::steadyMSecs(); + + d_ptr->getOrAllocate(nodeId)->init(d_ptr->seed(), threads); + d_ptr->m_ready++; + + LOG_INFO("%s" CYAN_BOLD("#%u") GREEN(" init done") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, nodeId, Chrono::steadyMSecs() - ts); + } + + + inline RxDataset *getOrAllocate(uint32_t nodeId) + { + RxDataset *dataset = datasets.at(nodeId); + + if (dataset == nullptr) { + # ifdef XMRIG_FEATURE_HWLOC + if (d_ptr->isNUMA()) { + std::thread thread(allocate, nodeId); + thread.join(); + } else + # endif + { + allocate(nodeId); + } + + dataset = datasets.at(nodeId); + } + + return dataset; + } + + + inline void setState(const Job &job, bool hugePages, bool numa) + { + if (m_algorithm != job.algorithm()) { + m_algorithm = RxAlgo::apply(job.algorithm()); + } + + m_ready = 0; + m_numa = numa && Cpu::info()->nodes() > 1; + m_hugePages = hugePages; + + memcpy(m_seed, job.seedHash(), sizeof(m_seed)); + } + + + inline bool isReady(const Job &job) + { + return m_ready == count() && m_algorithm == job.algorithm() && memcmp(m_seed, job.seedHash(), sizeof(m_seed)) == 0; + } + + std::map datasets; std::mutex mutex; + +private: + bool m_hugePages = true; + bool m_numa = true; + Algorithm m_algorithm; + size_t m_ready = 0; + uint8_t m_seed[32]; }; } // namespace xmrig -bool xmrig::Rx::isReady(const Job &job, uint32_t nodeId) +xmrig::RxDataset *xmrig::Rx::dataset(const Job &job, uint32_t nodeId) { std::lock_guard lock(d_ptr->mutex); + if (!d_ptr->isReady(job)) { + return nullptr; + } - return isReady(job.seedHash(), job.algorithm(), d_ptr->numa ? nodeId : 0); -} - - -xmrig::RxDataset *xmrig::Rx::dataset(uint32_t nodeId) -{ - std::lock_guard lock(d_ptr->mutex); - - return d_ptr->datasets[d_ptr->numa ? nodeId : 0]; + return d_ptr->datasets.at(d_ptr->isNUMA() ? nodeId : 0); } @@ -201,88 +283,33 @@ void xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa) std::lock_guard lock(d_ptr->mutex); - size_t ready = 0; - - for (auto const &item : d_ptr->datasets) { - if (isReady(job.seedHash(), job.algorithm(), item.first)) { - ready++; - } - } - - if (!d_ptr->datasets.empty() && ready == d_ptr->datasets.size()) { + if (d_ptr->isReady(job)) { return; } - d_ptr->hugePages = hugePages; - d_ptr->numa = numa && Cpu::info()->nodes() > 1; - const uint32_t threads = initThreads < 1 ? static_cast(Cpu::info()->threads()) - : static_cast(initThreads); + d_ptr->setState(job, hugePages, numa); + const uint32_t threads = initThreads < 1 ? static_cast(Cpu::info()->threads()) : static_cast(initThreads); + const String buf = Buffer::toHex(job.seedHash(), 8); + + LOG_INFO("%s" MAGENTA_BOLD("init dataset%s") " algo " WHITE_BOLD("%s (") CYAN_BOLD("%u") WHITE_BOLD(" threads)") BLACK_BOLD(" seed %s..."), + tag, + d_ptr->count() > 1 ? "s" : "", + job.algorithm().shortName(), + threads, + buf.data() + ); # ifdef XMRIG_FEATURE_HWLOC - if (d_ptr->numa) { - for (uint32_t nodeId : HwlocCpuInfo::nodeIndexes()) { - std::thread thread(initDataset, nodeId, job.seedHash(), job.algorithm(), threads); + if (d_ptr->isNUMA()) { + for (auto const &item : d_ptr->datasets) { + std::thread thread(RxPrivate::initDataset, item.first, threads); thread.detach(); } } else # endif { - std::thread thread(initDataset, 0, job.seedHash(), job.algorithm(), threads); + std::thread thread(RxPrivate::initDataset, 0, threads); thread.detach(); } } - - -bool xmrig::Rx::isReady(const uint8_t *seed, const Algorithm &algorithm, uint32_t nodeId) -{ - return !d_ptr->datasets.empty() && d_ptr->datasets[nodeId] != nullptr && d_ptr->datasets[nodeId]->isReady(seed, algorithm); -} - - -void xmrig::Rx::initDataset(uint32_t nodeId, const uint8_t *seed, const Algorithm &algorithm, uint32_t threads) -{ - std::lock_guard lock(d_ptr->mutex); - - RxDataset *dataset = d_ptr->datasets[nodeId]; - - if (!dataset) { -# ifdef XMRIG_FEATURE_HWLOC - if (d_ptr->numa) { - std::thread thread(RxPrivate::allocate, nodeId); - thread.join(); - } else -# endif - { - RxPrivate::allocate(nodeId); - } - - dataset = d_ptr->datasets[nodeId]; - } - - if (!dataset->isReady(seed, algorithm)) { - const uint64_t ts = Chrono::steadyMSecs(); - - if (dataset->get() != nullptr) { - LOG_INFO("%s" CYAN_BOLD("#%u") MAGENTA_BOLD(" init dataset") " algo " WHITE_BOLD("%s (") CYAN_BOLD("%u") WHITE_BOLD(" threads)") BLACK_BOLD(" seed %s..."), - tag, - nodeId, - algorithm.shortName(), - threads, - Buffer::toHex(seed, 8).data() - ); - } - else { - LOG_INFO("%s" CYAN_BOLD("#%u") MAGENTA_BOLD(" init cache") " algo " WHITE_BOLD("%s") BLACK_BOLD(" seed %s..."), - tag, - nodeId, - algorithm.shortName(), - Buffer::toHex(seed, 8).data() - ); - } - - dataset->init(seed, algorithm, threads); - - LOG_INFO("%s" CYAN_BOLD("#%u") GREEN(" init done") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, nodeId, Chrono::steadyMSecs() - ts); - } -} diff --git a/src/crypto/rx/Rx.h b/src/crypto/rx/Rx.h index c484c3b4..1ba6397e 100644 --- a/src/crypto/rx/Rx.h +++ b/src/crypto/rx/Rx.h @@ -44,16 +44,11 @@ class Job; class Rx { public: - static bool isReady(const Job &job, uint32_t nodeId); - static RxDataset *dataset(uint32_t nodeId); + static RxDataset *dataset(const Job &job, uint32_t nodeId); static std::pair hugePages(); static void destroy(); static void init(); static void init(const Job &job, int initThreads, bool hugePages, bool numa); - -private: - static bool isReady(const uint8_t *seed, const Algorithm &algorithm, uint32_t nodeId); - static void initDataset(uint32_t nodeId, const uint8_t *seed, const Algorithm &algorithm, uint32_t threads); }; diff --git a/src/crypto/rx/RxCache.h b/src/crypto/rx/RxCache.h index c48924a1..80c1faba 100644 --- a/src/crypto/rx/RxCache.h +++ b/src/crypto/rx/RxCache.h @@ -53,11 +53,12 @@ public: 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: + bool isReady(const void *seed) const; + int m_flags = 0; randomx_cache *m_cache = nullptr; uint8_t m_seed[32]; diff --git a/src/crypto/rx/RxDataset.cpp b/src/crypto/rx/RxDataset.cpp index 617b9200..7d498c4d 100644 --- a/src/crypto/rx/RxDataset.cpp +++ b/src/crypto/rx/RxDataset.cpp @@ -64,16 +64,8 @@ xmrig::RxDataset::~RxDataset() } -bool xmrig::RxDataset::init(const void *seed, const Algorithm &algorithm, uint32_t numThreads) +bool xmrig::RxDataset::init(const void *seed, uint32_t numThreads) { - if (isReady(seed, algorithm)) { - return false; - } - - if (m_algorithm != algorithm) { - m_algorithm = RxAlgo::apply(algorithm); - } - cache()->init(seed); if (!get()) { @@ -104,12 +96,6 @@ bool xmrig::RxDataset::init(const void *seed, const Algorithm &algorithm, uint32 } -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; diff --git a/src/crypto/rx/RxDataset.h b/src/crypto/rx/RxDataset.h index 7944d52c..d3488668 100644 --- a/src/crypto/rx/RxDataset.h +++ b/src/crypto/rx/RxDataset.h @@ -52,8 +52,7 @@ public: 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; + bool init(const void *seed, uint32_t numThreads); std::pair hugePages() const; static inline constexpr size_t size() { return RANDOMX_DATASET_MAX_SIZE; }