From 6f27037f0759133aea66a366093744e983b49d9b Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 11 Jul 2019 16:15:51 +0700 Subject: [PATCH] Added new nonce allocation method for dynamic/variable threads. --- CMakeLists.txt | 3 + src/core/WorkerJob.h | 143 ++++++++++++++++++++++++++++++++++++ src/crypto/common/Nonce.cpp | 83 +++++++++++++++++++++ src/crypto/common/Nonce.h | 57 ++++++++++++++ src/workers/MultiWorker.cpp | 88 ++++++---------------- src/workers/MultiWorker.h | 19 +---- src/workers/Worker.cpp | 5 +- src/workers/Worker.h | 5 +- src/workers/Workers.cpp | 21 ++++-- src/workers/Workers.h | 7 +- 10 files changed, 336 insertions(+), 95 deletions(-) create mode 100644 src/core/WorkerJob.h create mode 100644 src/crypto/common/Nonce.cpp create mode 100644 src/crypto/common/Nonce.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9094d381..13f787f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ set(HEADERS src/core/config/ConfigTransform.h src/core/config/usage.h src/core/Controller.h + src/core/WorkerJob.h src/interfaces/IThread.h src/interfaces/IWorker.h src/Mem.h @@ -71,6 +72,7 @@ set(HEADERS_CRYPTO src/crypto/cn/soft_aes.h src/crypto/common/Algorithm.h src/crypto/common/keccak.h + src/crypto/common/Nonce.h src/crypto/common/portable/mm_malloc.h src/crypto/common/VirtualMemory.h ) @@ -113,6 +115,7 @@ set(SOURCES_CRYPTO src/crypto/cn/CnHash.cpp src/crypto/common/Algorithm.cpp src/crypto/common/keccak.cpp + src/crypto/common/Nonce.cpp ) if (WIN32) diff --git a/src/core/WorkerJob.h b/src/core/WorkerJob.h new file mode 100644 index 00000000..004c5533 --- /dev/null +++ b/src/core/WorkerJob.h @@ -0,0 +1,143 @@ +/* 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_WORKERJOB_H +#define XMRIG_WORKERJOB_H + + +#include +#include + + +#include "base/net/stratum/Job.h" +#include "crypto/common/Nonce.h" + + +namespace xmrig { + + +template +class WorkerJob +{ +public: + inline const Job ¤tJob() const { return m_jobs[index()]; } + inline uint32_t *nonce(size_t i = 0) { return reinterpret_cast(blob() + (i * currentJob().size()) + 39); } + inline uint64_t sequence() const { return m_sequence; } + inline uint8_t *blob() { return m_blobs[index()]; } + inline uint8_t index() const { return m_index; } + + + inline void add(const Job &job, uint64_t sequence, uint32_t reserveCount) + { + m_sequence = sequence; + + if (currentJob() == job) { + return; + } + + if (index() == 1 && job.poolId() >= 0 && job == m_jobs[0]) { + return; + } + + save(job, reserveCount); + } + + + inline void nextRound(uint32_t reserveCount) + { + m_rounds[index()]++; + + if ((m_rounds[index()] % reserveCount) == 0) { + for (size_t i = 0; i < N; ++i) { + *nonce(i) = Nonce::next(index(), *nonce(i), reserveCount, currentJob().isNicehash()); + } + } + else { + for (size_t i = 0; i < N; ++i) { + *nonce(i) += 1; + } + } + } + + +private: + inline void save(const Job &job, uint32_t reserveCount) + { + m_index = job.poolId() == -1 ? 1 : 0; + const size_t size = job.size(); + m_jobs[index()] = job; + m_rounds[index()] = 0; + + for (size_t i = 0; i < N; ++i) { + memcpy(m_blobs[index()] + (i * size), job.blob(), size); + *nonce(i) = Nonce::next(index(), *nonce(i), reserveCount, job.isNicehash()); + } + } + + + alignas(16) uint8_t m_blobs[2][Job::kMaxBlobSize * N]; + Job m_jobs[2]; + uint32_t m_rounds[2] = { 0, 0 }; + uint64_t m_sequence = 0; + uint8_t m_index = 0; +}; + + +template<> +inline uint32_t *xmrig::WorkerJob<1>::nonce(size_t) +{ + return reinterpret_cast(blob() + 39); +} + + +template<> +inline void xmrig::WorkerJob<1>::nextRound(uint32_t reserveCount) +{ + m_rounds[index()]++; + + if ((m_rounds[index()] % reserveCount) == 0) { + *nonce() = Nonce::next(index(), *nonce(), reserveCount, currentJob().isNicehash()); + } + else { + *nonce() += 1; + } +} + + +template<> +inline void xmrig::WorkerJob<1>::save(const Job &job, uint32_t reserveCount) +{ + m_index = job.poolId() == -1 ? 1 : 0; + m_jobs[index()] = job; + m_rounds[index()] = 0; + + memcpy(blob(), job.blob(), job.size()); + *nonce() = Nonce::next(index(), *nonce(), reserveCount, currentJob().isNicehash()); +} + + +} // namespace xmrig + + +#endif /* XMRIG_WORKERJOB_H */ diff --git a/src/crypto/common/Nonce.cpp b/src/crypto/common/Nonce.cpp new file mode 100644 index 00000000..6670308a --- /dev/null +++ b/src/crypto/common/Nonce.cpp @@ -0,0 +1,83 @@ +/* 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 . + */ + + +#include + + +#include "crypto/common/Nonce.h" + + +namespace xmrig { + + +std::atomic Nonce::m_sequence; +uint32_t Nonce::m_nonces[2] = { 0, 0 }; + + +static uv_mutex_t mutex; +static Nonce nonce; + + +} // namespace xmrig + + +xmrig::Nonce::Nonce() +{ + m_sequence = 1; + + uv_mutex_init(&mutex); +} + + +uint32_t xmrig::Nonce::next(uint8_t index, uint32_t nonce, uint32_t reserveCount, bool nicehash) +{ + uint32_t next; + + uv_mutex_lock(&mutex); + + if (nicehash) { + next = (nonce & 0xFF000000) | m_nonces[index]; + } + else { + next = m_nonces[index]; + } + + m_nonces[index] += reserveCount; + + uv_mutex_unlock(&mutex); + + return next; +} + + +void xmrig::Nonce::reset(uint8_t index) +{ + uv_mutex_lock(&mutex); + + m_nonces[index] = 0; + m_sequence++; + + uv_mutex_unlock(&mutex); +} diff --git a/src/crypto/common/Nonce.h b/src/crypto/common/Nonce.h new file mode 100644 index 00000000..ea843bc9 --- /dev/null +++ b/src/crypto/common/Nonce.h @@ -0,0 +1,57 @@ +/* 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_NONCE_H +#define XMRIG_NONCE_H + + +#include + + +namespace xmrig { + + +class Nonce +{ +public: + Nonce(); + + static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } + static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } + static inline void stop() { m_sequence = 0; } + static inline void touch() { m_sequence++; } + + static uint32_t next(uint8_t index, uint32_t nonce, uint32_t reserveCount, bool nicehash); + static void reset(uint8_t index); + +private: + static uint32_t m_nonces[2]; + static std::atomic m_sequence; +}; + + +} // namespace xmrig + + +#endif /* XMRIG_NONCE_H */ diff --git a/src/workers/MultiWorker.cpp b/src/workers/MultiWorker.cpp index 684d92f9..daae9230 100644 --- a/src/workers/MultiWorker.cpp +++ b/src/workers/MultiWorker.cpp @@ -28,6 +28,7 @@ #include "crypto/cn/CryptoNight_test.h" +#include "crypto/common/Nonce.h" #include "crypto/rx/Rx.h" #include "crypto/rx/RxVm.h" #include "net/JobResults.h" @@ -36,6 +37,13 @@ #include "workers/Workers.h" +namespace xmrig { + +static constexpr uint32_t kReserveCount = 4096; + +} // namespace xmrig + + template xmrig::MultiWorker::MultiWorker(ThreadHandle *handle) : Worker(handle) @@ -62,7 +70,7 @@ template void xmrig::MultiWorker::allocateRandomX_VM() { if (!m_vm) { - RxDataset *dataset = Rx::dataset(m_state.job.seedHash(), m_state.job.algorithm()); + RxDataset *dataset = Rx::dataset(m_job.currentJob().seedHash(), m_job.currentJob().algorithm()); m_vm = new RxVm(dataset, true, m_thread->isSoftAES()); } } @@ -131,44 +139,45 @@ bool xmrig::MultiWorker::selfTest() template void xmrig::MultiWorker::start() { - while (Workers::sequence() > 0) { + while (Nonce::sequence() > 0) { if (Workers::isPaused()) { do { std::this_thread::sleep_for(std::chrono::milliseconds(200)); } while (Workers::isPaused()); - if (Workers::sequence() == 0) { + if (Nonce::sequence() == 0) { break; } consumeJob(); } - while (!Workers::isOutdated(m_sequence)) { + while (!Nonce::isOutdated(m_job.sequence())) { if ((m_count & 0x7) == 0) { storeStats(); } + const Job &job = m_job.currentJob(); + # ifdef XMRIG_ALGO_RANDOMX - if (m_state.job.algorithm().family() == Algorithm::RANDOM_X) { + if (job.algorithm().family() == Algorithm::RANDOM_X) { allocateRandomX_VM(); - randomx_calculate_hash(m_vm->get(), m_state.blob, m_state.job.size(), m_hash); + randomx_calculate_hash(m_vm->get(), m_job.blob(), job.size(), m_hash); } else # endif { - m_thread->fn(m_state.job.algorithm())(m_state.blob, m_state.job.size(), m_hash, m_ctx, m_state.job.height()); + m_thread->fn(job.algorithm())(m_job.blob(), job.size(), m_hash, m_ctx, job.height()); } for (size_t i = 0; i < N; ++i) { - if (*reinterpret_cast(m_hash + (i * 32) + 24) < m_state.job.target()) { - JobResults::submit(JobResult(m_state.job.poolId(), m_state.job.id(), m_state.job.clientId(), *nonce(i), m_hash + (i * 32), m_state.job.diff(), m_state.job.algorithm())); + if (*reinterpret_cast(m_hash + (i * 32) + 24) < job.target()) { + JobResults::submit(JobResult(job.poolId(), job.id(), job.clientId(), *m_job.nonce(i), m_hash + (i * 32), job.diff(), job.algorithm())); } - - *nonce(i) += 1; } + m_job.nextRound(kReserveCount); m_count += N; std::this_thread::yield(); @@ -179,18 +188,6 @@ void xmrig::MultiWorker::start() } -template -bool xmrig::MultiWorker::resume(const xmrig::Job &job) -{ - if (m_state.job.poolId() == -1 && job.poolId() >= 0 && job.id() == m_pausedState.job.id()) { - m_state = m_pausedState; - return true; - } - - return false; -} - - template bool xmrig::MultiWorker::verify(const Algorithm &algorithm, const uint8_t *referenceValue) { @@ -215,10 +212,10 @@ bool xmrig::MultiWorker::verify2(const Algorithm &algorithm, const uint8_t *r for (size_t i = 0; i < (sizeof(cn_r_test_input) / sizeof(cn_r_test_input[0])); ++i) { const size_t size = cn_r_test_input[i].size; for (size_t k = 0; k < N; ++k) { - memcpy(m_state.blob + (k * size), cn_r_test_input[i].data, size); + memcpy(m_job.blob() + (k * size), cn_r_test_input[i].data, size); } - func(m_state.blob, size, m_hash, m_ctx, cn_r_test_input[i].height); + func(m_job.blob(), size, m_hash, m_ctx, cn_r_test_input[i].height); for (size_t k = 0; k < N; ++k) { if (memcmp(m_hash + k * 32, referenceValue + i * 32, sizeof m_hash / N) != 0) { @@ -258,46 +255,7 @@ bool MultiWorker<1>::verify2(const Algorithm &algorithm, const uint8_t *referenc template void xmrig::MultiWorker::consumeJob() { - Job job = Workers::job(); - m_sequence = Workers::sequence(); - if (m_state.job == job) { - return; - } - - save(job); - - if (resume(job)) { - return; - } - - m_state.job = job; - - const size_t size = m_state.job.size(); - memcpy(m_state.blob, m_state.job.blob(), m_state.job.size()); - - if (N > 1) { - for (size_t i = 1; i < N; ++i) { - memcpy(m_state.blob + (i * size), m_state.blob, size); - } - } - - for (size_t i = 0; i < N; ++i) { - if (m_state.job.isNicehash()) { - *nonce(i) = (*nonce(i) & 0xff000000U) + (0xffffffU / m_totalWays * (m_offset + i)); - } - else { - *nonce(i) = 0xffffffffU / m_totalWays * (m_offset + i); - } - } -} - - -template -void xmrig::MultiWorker::save(const Job &job) -{ - if (job.poolId() == -1 && m_state.job.poolId() >= 0) { - m_pausedState = m_state; - } + m_job.add(Workers::job(), Nonce::sequence(), kReserveCount); } diff --git a/src/workers/MultiWorker.h b/src/workers/MultiWorker.h index 0502ad84..2bcb2333 100644 --- a/src/workers/MultiWorker.h +++ b/src/workers/MultiWorker.h @@ -28,6 +28,7 @@ #include "base/net/stratum/Job.h" +#include "core/WorkerJob.h" #include "Mem.h" #include "net/JobResult.h" #include "workers/Worker.h" @@ -55,29 +56,15 @@ private: void allocateRandomX_VM(); # endif - bool resume(const Job &job); bool verify(const Algorithm &algorithm, const uint8_t *referenceValue); bool verify2(const Algorithm &algorithm, const uint8_t *referenceValue); void consumeJob(); - void save(const Job &job); - - inline uint32_t *nonce(size_t index) - { - return reinterpret_cast(m_state.blob + (index * m_state.job.size()) + 39); - } - - struct State - { - alignas(16) uint8_t blob[Job::kMaxBlobSize * N]; - Job job; - }; - cryptonight_ctx *m_ctx[N]; - State m_pausedState; - State m_state; uint8_t m_hash[N * 32]; + WorkerJob m_job; + # ifdef XMRIG_ALGO_RANDOMX RxVm *m_vm = nullptr; # endif diff --git a/src/workers/Worker.cpp b/src/workers/Worker.cpp index 4f69d905..0c61b3cb 100644 --- a/src/workers/Worker.cpp +++ b/src/workers/Worker.cpp @@ -5,7 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018 Lee Clagett + * 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 @@ -38,7 +40,6 @@ Worker::Worker(ThreadHandle *handle) : m_hashCount(0), m_timestamp(0), m_count(0), - m_sequence(0), m_thread(static_cast(handle->config())) { if (xmrig::Cpu::info()->threads() > 1 && m_thread->affinity() != -1L) { diff --git a/src/workers/Worker.h b/src/workers/Worker.h index 3d40257d..13e437d3 100644 --- a/src/workers/Worker.h +++ b/src/workers/Worker.h @@ -5,7 +5,9 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018 Lee Clagett + * 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 @@ -61,7 +63,6 @@ protected: std::atomic m_hashCount; std::atomic m_timestamp; uint64_t m_count; - uint64_t m_sequence; xmrig::CpuThreadLegacy *m_thread; }; diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 58cccd9e..1ed27c40 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -33,6 +33,7 @@ #include "base/tools/Handle.h" #include "core/config/Config.h" #include "core/Controller.h" +#include "crypto/common/Nonce.h" #include "crypto/rx/RxAlgo.h" #include "crypto/rx/RxCache.h" #include "crypto/rx/RxDataset.h" @@ -51,7 +52,6 @@ Hashrate *Workers::m_hashrate = nullptr; xmrig::Job Workers::m_job; Workers::LaunchStatus Workers::m_status; std::atomic Workers::m_paused; -std::atomic Workers::m_sequence; std::vector Workers::m_workers; uint64_t Workers::m_ticks = 0; uv_mutex_t Workers::m_mutex; @@ -90,6 +90,15 @@ size_t Workers::threads() } +void Workers::pause() +{ + m_active = false; + m_paused = 1; + + xmrig::Nonce::touch(); +} + + void Workers::printHashrate(bool detail) { assert(m_controller != nullptr); @@ -134,7 +143,7 @@ void Workers::setEnabled(bool enabled) } m_paused = enabled ? 0 : 1; - m_sequence++; + xmrig::Nonce::touch(); } @@ -146,6 +155,9 @@ void Workers::setJob(const xmrig::Job &job, bool donate) if (donate) { m_job.setPoolId(-1); } + + xmrig::Nonce::reset(donate ? 1 : 0); + uv_rwlock_wrunlock(&m_rwlock); m_active = true; @@ -153,7 +165,6 @@ void Workers::setJob(const xmrig::Job &job, bool donate) return; } - m_sequence++; m_paused = 0; } @@ -183,7 +194,6 @@ void Workers::start(xmrig::Controller *controller) uv_mutex_init(&m_mutex); uv_rwlock_init(&m_rwlock); - m_sequence = 1; m_paused = 1; m_timer = new uv_timer_t; @@ -208,7 +218,8 @@ void Workers::stop() m_hashrate->stop(); m_paused = 0; - m_sequence = 0; + + xmrig::Nonce::stop(); for (size_t i = 0; i < m_workers.size(); ++i) { m_workers[i]->join(); diff --git a/src/workers/Workers.h b/src/workers/Workers.h index 8619f973..83777d0d 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -53,21 +53,19 @@ namespace xmrig { class Workers { public: - static xmrig::Job job(); static size_t hugePages(); static size_t threads(); + static void pause(); static void printHashrate(bool detail); static void setEnabled(bool enabled); static void setJob(const xmrig::Job &job, bool donate); static void start(xmrig::Controller *controller); static void stop(); + static xmrig::Job job(); static inline bool isEnabled() { return m_enabled; } - static inline bool isOutdated(uint64_t sequence) { return m_sequence.load(std::memory_order_relaxed) != sequence; } static inline bool isPaused() { return m_paused.load(std::memory_order_relaxed) == 1; } static inline Hashrate *hashrate() { return m_hashrate; } - static inline uint64_t sequence() { return m_sequence.load(std::memory_order_relaxed); } - static inline void pause() { m_active = false; m_paused = 1; m_sequence++; } # ifdef XMRIG_FEATURE_API static void threadsSummary(rapidjson::Document &doc); @@ -103,7 +101,6 @@ private: static xmrig::Job m_job; static LaunchStatus m_status; static std::atomic m_paused; - static std::atomic m_sequence; static std::vector m_workers; static uint64_t m_ticks; static uv_mutex_t m_mutex;