RandomX fixes

- Fixed possible infinite loop when updating RandomX dataset
- Fixed used memory display
- Allocate dataset and VM for RandomX only when it's actually used
This commit is contained in:
SChernykh 2019-06-18 19:53:11 +02:00
parent 50ace41766
commit e8ac01d289
4 changed files with 34 additions and 10 deletions

View file

@ -36,14 +36,9 @@
template<size_t N> template<size_t N>
MultiWorker<N>::MultiWorker(ThreadHandle *handle) MultiWorker<N>::MultiWorker(ThreadHandle *handle)
: Worker(handle) : Worker(handle)
, m_rx_vm(nullptr)
{ {
m_memory = Mem::create(m_ctx, m_thread->algorithm(), N); m_memory = Mem::create(m_ctx, m_thread->algorithm(), N);
const int flags = RANDOMX_FLAG_LARGE_PAGES | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT;
m_rx_vm = randomx_create_vm(static_cast<randomx_flags>(flags), nullptr, Workers::getDataset());
if (!m_rx_vm) {
m_rx_vm = randomx_create_vm(static_cast<randomx_flags>(flags - RANDOMX_FLAG_LARGE_PAGES), nullptr, Workers::getDataset());
}
} }
@ -51,7 +46,22 @@ template<size_t N>
MultiWorker<N>::~MultiWorker() MultiWorker<N>::~MultiWorker()
{ {
Mem::release(m_ctx, N, m_memory); Mem::release(m_ctx, N, m_memory);
randomx_destroy_vm(m_rx_vm); if (m_rx_vm) {
randomx_destroy_vm(m_rx_vm);
}
}
template<size_t N>
void MultiWorker<N>::allocateRandomX_VM()
{
if (!m_rx_vm) {
const int flags = RANDOMX_FLAG_LARGE_PAGES | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT;
m_rx_vm = randomx_create_vm(static_cast<randomx_flags>(flags), nullptr, Workers::getDataset());
if (!m_rx_vm) {
m_rx_vm = randomx_create_vm(static_cast<randomx_flags>(flags - RANDOMX_FLAG_LARGE_PAGES), nullptr, Workers::getDataset());
}
}
} }
@ -135,6 +145,7 @@ void MultiWorker<N>::start()
const xmrig::Variant v = m_state.job.algorithm().variant(); const xmrig::Variant v = m_state.job.algorithm().variant();
if (v == xmrig::VARIANT_RX_WOW) { if (v == xmrig::VARIANT_RX_WOW) {
allocateRandomX_VM();
Workers::updateDataset(m_state.job.seed_hash(), m_totalWays); Workers::updateDataset(m_state.job.seed_hash(), m_totalWays);
randomx_calculate_hash(m_rx_vm, m_state.blob, m_state.job.size(), m_hash); randomx_calculate_hash(m_rx_vm, m_state.blob, m_state.job.size(), m_hash);
} }

View file

@ -49,6 +49,8 @@ protected:
void start() override; void start() override;
private: private:
void allocateRandomX_VM();
bool resume(const xmrig::Job &job); bool resume(const xmrig::Job &job);
bool verify(xmrig::Variant variant, const uint8_t *referenceValue); bool verify(xmrig::Variant variant, const uint8_t *referenceValue);
bool verify2(xmrig::Variant variant, const uint8_t *referenceValue); bool verify2(xmrig::Variant variant, const uint8_t *referenceValue);

View file

@ -182,6 +182,7 @@ void Workers::start(xmrig::Controller *controller)
const std::vector<xmrig::IThread *> &threads = controller->config()->threads(); const std::vector<xmrig::IThread *> &threads = controller->config()->threads();
m_status.algo = controller->config()->algorithm().algo(); m_status.algo = controller->config()->algorithm().algo();
m_status.variant = controller->config()->algorithm().variant();
m_status.threads = threads.size(); m_status.threads = threads.size();
for (const xmrig::IThread *thread : threads) { for (const xmrig::IThread *thread : threads) {
@ -246,7 +247,7 @@ void Workers::threadsSummary(rapidjson::Document &doc)
{ {
uv_mutex_lock(&m_mutex); uv_mutex_lock(&m_mutex);
const uint64_t pages[2] = { m_status.hugePages, m_status.pages }; const uint64_t pages[2] = { m_status.hugePages, m_status.pages };
const uint64_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo); const uint64_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo, m_status.variant);
uv_mutex_unlock(&m_mutex); uv_mutex_unlock(&m_mutex);
auto &allocator = doc.GetAllocator(); auto &allocator = doc.GetAllocator();
@ -350,7 +351,7 @@ void Workers::start(IWorker *worker)
if (m_status.started == m_status.threads) { if (m_status.started == m_status.threads) {
const double percent = (double) m_status.hugePages / m_status.pages * 100.0; const double percent = (double) m_status.hugePages / m_status.pages * 100.0;
const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo) / 1024; const size_t memory = m_status.ways * xmrig::cn_select_memory(m_status.algo, m_status.variant) / 1024;
LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\x1B[0m memory " CYAN_BOLD("%zu KB") "", LOG_INFO(GREEN_BOLD("READY (CPU)") " threads " CYAN_BOLD("%zu(%zu)") " huge pages %s%zu/%zu %1.0f%%\x1B[0m memory " CYAN_BOLD("%zu KB") "",
m_status.threads, m_status.ways, m_status.threads, m_status.ways,
@ -374,6 +375,10 @@ void Workers::updateDataset(const uint8_t* seed_hash, const uint32_t num_threads
// Wait for all threads to get here // Wait for all threads to get here
do { do {
if (m_sequence.load(std::memory_order_relaxed) == 0) {
// Exit immediately if workers were stopped
return;
}
std::this_thread::yield(); std::this_thread::yield();
} while (m_rx_dataset_init_thread_counter.load() != num_threads); } while (m_rx_dataset_init_thread_counter.load() != num_threads);
@ -395,6 +400,10 @@ void Workers::updateDataset(const uint8_t* seed_hash, const uint32_t num_threads
// Wait for all threads to complete // Wait for all threads to complete
--m_rx_dataset_init_thread_counter; --m_rx_dataset_init_thread_counter;
do { do {
if (m_sequence.load(std::memory_order_relaxed) == 0) {
// Exit immediately if workers were stopped
return;
}
std::this_thread::yield(); std::this_thread::yield();
} while (m_rx_dataset_init_thread_counter.load() != 0); } while (m_rx_dataset_init_thread_counter.load() != 0);
} }

View file

@ -91,7 +91,8 @@ private:
started(0), started(0),
threads(0), threads(0),
ways(0), ways(0),
algo(xmrig::CRYPTONIGHT) algo(xmrig::CRYPTONIGHT),
variant(xmrig::VARIANT_AUTO)
{} {}
size_t hugePages; size_t hugePages;
@ -100,6 +101,7 @@ private:
size_t threads; size_t threads;
size_t ways; size_t ways;
xmrig::Algo algo; xmrig::Algo algo;
xmrig::Variant variant;
}; };
static bool m_active; static bool m_active;