RandomWOW support

This commit is contained in:
SChernykh 2019-06-15 22:53:03 +02:00
parent 277f188cd5
commit ac1b554282
14 changed files with 157 additions and 3 deletions

View file

@ -154,6 +154,7 @@ add_definitions(/DUNICODE)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
find_package(UV REQUIRED) find_package(UV REQUIRED)
find_package(RANDOMWOW REQUIRED)
include(cmake/flags.cmake) include(cmake/flags.cmake)
@ -219,6 +220,7 @@ endif()
include_directories(src) include_directories(src)
include_directories(src/3rdparty) include_directories(src/3rdparty)
include_directories(${UV_INCLUDE_DIR}) include_directories(${UV_INCLUDE_DIR})
include_directories(${RANDOMWOW_INCLUDE_DIR})
if (BUILD_STATIC) if (BUILD_STATIC)
set(CMAKE_EXE_LINKER_FLAGS " -static") set(CMAKE_EXE_LINKER_FLAGS " -static")
@ -229,4 +231,4 @@ if (WITH_DEBUG_LOG)
endif() endif()
add_executable(${CMAKE_PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTP_SOURCES} ${TLS_SOURCES} ${XMRIG_ASM_SOURCES} ${CN_GPU_SOURCES}) add_executable(${CMAKE_PROJECT_NAME} ${HEADERS} ${SOURCES} ${SOURCES_OS} ${SOURCES_CPUID} ${HEADERS_CRYPTO} ${SOURCES_CRYPTO} ${SOURCES_SYSLOG} ${HTTP_SOURCES} ${TLS_SOURCES} ${XMRIG_ASM_SOURCES} ${CN_GPU_SOURCES})
target_link_libraries(${CMAKE_PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${EXTRA_LIBS} ${CPUID_LIB}) target_link_libraries(${CMAKE_PROJECT_NAME} ${XMRIG_ASM_LIBRARY} ${OPENSSL_LIBRARIES} ${UV_LIBRARIES} ${RANDOMWOW_LIBRARIES} ${EXTRA_LIBS} ${CPUID_LIB})

25
cmake/FindRandomWOW.cmake Normal file
View file

@ -0,0 +1,25 @@
find_path(
RANDOMWOW_INCLUDE_DIR
NAMES randomwow.h
PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS"
PATH_SUFFIXES "include"
NO_DEFAULT_PATH
)
find_path(RANDOMWOW_INCLUDE_DIR NAMES randomwow.h)
find_library(
RANDOMWOW_LIBRARY
NAMES librandomwow.a randomwow librandomwow
PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS"
PATH_SUFFIXES "lib"
NO_DEFAULT_PATH
)
find_library(RANDOMWOW_LIBRARY NAMES librandomwow.a randomwow librandomwow)
set(RANDOMWOW_LIBRARIES ${RANDOMWOW_LIBRARY})
set(RANDOMWOW_INCLUDE_DIRS ${RANDOMWOW_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(RANDOMWOW DEFAULT_MSG RANDOMWOW_LIBRARY RANDOMWOW_INCLUDE_DIR)

View file

@ -344,6 +344,14 @@ bool xmrig::Client::parseJob(const rapidjson::Value &params, int *code)
} }
} }
if (params.HasMember("seed_hash")) {
const rapidjson::Value &variant = params["seed_hash"];
if (variant.IsString()) {
job.setSeedHash(variant.GetString());
}
}
if (params.HasMember("height")) { if (params.HasMember("height")) {
const rapidjson::Value &variant = params["height"]; const rapidjson::Value &variant = params["height"];

View file

@ -220,6 +220,8 @@ bool xmrig::DaemonClient::parseJob(const rapidjson::Value &params, int *code)
return false; return false;
} }
job.setSeedHash(Json::getString(params, "seed_hash"));
job.setHeight(Json::getUint64(params, kHeight)); job.setHeight(Json::getUint64(params, kHeight));
job.setDiff(Json::getUint64(params, "difficulty")); job.setDiff(Json::getUint64(params, "difficulty"));
job.setId(blocktemplate.data() + blocktemplate.size() - 32); job.setId(blocktemplate.data() + blocktemplate.size() - 32);

View file

@ -42,6 +42,7 @@ xmrig::Job::Job() :
m_diff(0), m_diff(0),
m_height(0), m_height(0),
m_target(0), m_target(0),
m_seedHash(),
m_blob() m_blob()
{ {
} }
@ -58,6 +59,7 @@ xmrig::Job::Job(int poolId, bool nicehash, const Algorithm &algorithm, const Str
m_diff(0), m_diff(0),
m_height(0), m_height(0),
m_target(0), m_target(0),
m_seedHash(),
m_blob() m_blob()
{ {
} }
@ -175,6 +177,15 @@ void xmrig::Job::setDiff(uint64_t diff)
} }
bool xmrig::Job::setSeedHash(const char *hash)
{
if (!hash || (strlen(hash) != sizeof(m_seedHash) * 2))
return false;
return Buffer::fromHex(hash, sizeof(m_seedHash) * 2, m_seedHash);
}
xmrig::Variant xmrig::Job::variant() const xmrig::Variant xmrig::Job::variant() const
{ {
switch (m_algorithm.algo()) { switch (m_algorithm.algo()) {

View file

@ -55,6 +55,7 @@ public:
bool setTarget(const char *target); bool setTarget(const char *target);
void setAlgorithm(const char *algo); void setAlgorithm(const char *algo);
void setDiff(uint64_t diff); void setDiff(uint64_t diff);
bool setSeedHash(const char *hash);
inline bool isNicehash() const { return m_nicehash; } inline bool isNicehash() const { return m_nicehash; }
inline bool isValid() const { return m_size > 0 && m_diff > 0; } inline bool isValid() const { return m_size > 0 && m_diff > 0; }
@ -64,6 +65,7 @@ public:
inline const String &id() const { return m_id; } inline const String &id() const { return m_id; }
inline const uint32_t *nonce() const { return reinterpret_cast<const uint32_t*>(m_blob + 39); } inline const uint32_t *nonce() const { return reinterpret_cast<const uint32_t*>(m_blob + 39); }
inline const uint8_t *blob() const { return m_blob; } inline const uint8_t *blob() const { return m_blob; }
inline const uint8_t *seed_hash() const { return m_seedHash; }
inline int poolId() const { return m_poolId; } inline int poolId() const { return m_poolId; }
inline int threadId() const { return m_threadId; } inline int threadId() const { return m_threadId; }
inline size_t size() const { return m_size; } inline size_t size() const { return m_size; }
@ -106,6 +108,7 @@ private:
uint64_t m_diff; uint64_t m_diff;
uint64_t m_height; uint64_t m_height;
uint64_t m_target; uint64_t m_target;
uint8_t m_seedHash[32];
uint8_t m_blob[kMaxBlobSize]; uint8_t m_blob[kMaxBlobSize];
# ifdef XMRIG_PROXY_PROJECT # ifdef XMRIG_PROXY_PROJECT

View file

@ -517,6 +517,7 @@ void xmrig::Pool::rebuild()
addVariant(VARIANT_RWZ); addVariant(VARIANT_RWZ);
addVariant(VARIANT_ZLS); addVariant(VARIANT_ZLS);
addVariant(VARIANT_DOUBLE); addVariant(VARIANT_DOUBLE);
addVariant(VARIANT_RX_WOW);
addVariant(VARIANT_AUTO); addVariant(VARIANT_AUTO);
# endif # endif
} }

View file

@ -69,6 +69,7 @@ static AlgoData const algorithms[] = {
{ "cryptonight/rwz", "cn/rwz", xmrig::CRYPTONIGHT, xmrig::VARIANT_RWZ }, { "cryptonight/rwz", "cn/rwz", xmrig::CRYPTONIGHT, xmrig::VARIANT_RWZ },
{ "cryptonight/zls", "cn/zls", xmrig::CRYPTONIGHT, xmrig::VARIANT_ZLS }, { "cryptonight/zls", "cn/zls", xmrig::CRYPTONIGHT, xmrig::VARIANT_ZLS },
{ "cryptonight/double", "cn/double", xmrig::CRYPTONIGHT, xmrig::VARIANT_DOUBLE }, { "cryptonight/double", "cn/double", xmrig::CRYPTONIGHT, xmrig::VARIANT_DOUBLE },
{ "randomx/wow", "rx/wow", xmrig::CRYPTONIGHT, xmrig::VARIANT_RX_WOW },
# ifndef XMRIG_NO_AEON # ifndef XMRIG_NO_AEON
{ "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO }, { "cryptonight-lite", "cn-lite", xmrig::CRYPTONIGHT_LITE, xmrig::VARIANT_AUTO },
@ -138,7 +139,8 @@ static const char *variants[] = {
"r", "r",
"rwz", "rwz",
"zls", "zls",
"double" "double",
"rx/wow",
}; };

View file

@ -79,6 +79,7 @@ enum Variant {
VARIANT_RWZ = 14, // CryptoNight variant 2 with 3/4 iterations and reversed shuffle operation (Graft) VARIANT_RWZ = 14, // CryptoNight variant 2 with 3/4 iterations and reversed shuffle operation (Graft)
VARIANT_ZLS = 15, // CryptoNight variant 2 with 3/4 iterations (Zelerius) VARIANT_ZLS = 15, // CryptoNight variant 2 with 3/4 iterations (Zelerius)
VARIANT_DOUBLE = 16, // CryptoNight variant 2 with double iterations (X-CASH) VARIANT_DOUBLE = 16, // CryptoNight variant 2 with double iterations (X-CASH)
VARIANT_RX_WOW = 17, // RandomX (Wownero)
VARIANT_MAX VARIANT_MAX
}; };

View file

@ -390,6 +390,8 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a
cryptonight_quad_hash<CRYPTONIGHT, true, VARIANT_DOUBLE>, cryptonight_quad_hash<CRYPTONIGHT, true, VARIANT_DOUBLE>,
cryptonight_penta_hash<CRYPTONIGHT, true, VARIANT_DOUBLE>, cryptonight_penta_hash<CRYPTONIGHT, true, VARIANT_DOUBLE>,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW
# ifndef XMRIG_NO_AEON # ifndef XMRIG_NO_AEON
cryptonight_single_hash<CRYPTONIGHT_LITE, false, VARIANT_0>, cryptonight_single_hash<CRYPTONIGHT_LITE, false, VARIANT_0>,
cryptonight_double_hash<CRYPTONIGHT_LITE, false, VARIANT_0>, cryptonight_double_hash<CRYPTONIGHT_LITE, false, VARIANT_0>,
@ -428,6 +430,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW
# else # else
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1
@ -446,6 +449,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW
# endif # endif
# ifndef XMRIG_NO_SUMO # ifndef XMRIG_NO_SUMO
@ -498,6 +502,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW
# else # else
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1
@ -516,6 +521,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW
# endif # endif
# ifndef XMRIG_NO_CN_PICO # ifndef XMRIG_NO_CN_PICO
@ -547,6 +553,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW
# else # else
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_0
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_1
@ -565,6 +572,7 @@ xmrig::CpuThread::cn_hash_fun xmrig::CpuThread::fn(Algo algorithm, AlgoVariant a
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RWZ
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_ZLS
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_DOUBLE
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, // VARIANT_RX_WOW
# endif # endif
}; };

View file

@ -38,6 +38,12 @@ MultiWorker<N>::MultiWorker(ThreadHandle *handle)
: Worker(handle) : Worker(handle)
{ {
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());
}
} }
@ -45,6 +51,7 @@ 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);
} }
@ -126,7 +133,14 @@ void MultiWorker<N>::start()
storeStats(); storeStats();
} }
m_thread->fn(m_state.job.algorithm().variant())(m_state.blob, m_state.job.size(), m_hash, m_ctx, m_state.job.height()); const xmrig::Variant v = m_state.job.algorithm().variant();
if (v == xmrig::VARIANT_RX_WOW) {
Workers::updateDataset(m_state.job.seed_hash(), m_totalWays);
randomx_calculate_hash(m_rx_vm, m_state.blob, m_state.job.size(), m_hash);
}
else {
m_thread->fn(v)(m_state.blob, m_state.job.size(), m_hash, m_ctx, m_state.job.height());
}
for (size_t i = 0; i < N; ++i) { for (size_t i = 0; i < N; ++i) {
if (*reinterpret_cast<uint64_t*>(m_hash + (i * 32) + 24) < m_state.job.target()) { if (*reinterpret_cast<uint64_t*>(m_hash + (i * 32) + 24) < m_state.job.target()) {

View file

@ -31,6 +31,7 @@
#include "Mem.h" #include "Mem.h"
#include "net/JobResult.h" #include "net/JobResult.h"
#include "workers/Worker.h" #include "workers/Worker.h"
#include "randomwow.h"
class Handle; class Handle;
@ -70,6 +71,8 @@ private:
State m_pausedState; State m_pausedState;
State m_state; State m_state;
uint8_t m_hash[N * 32]; uint8_t m_hash[N * 32];
randomx_vm* m_rx_vm;
}; };

View file

@ -59,6 +59,11 @@ uv_mutex_t Workers::m_mutex;
uv_rwlock_t Workers::m_rwlock; uv_rwlock_t Workers::m_rwlock;
uv_timer_t *Workers::m_timer = nullptr; uv_timer_t *Workers::m_timer = nullptr;
xmrig::Controller *Workers::m_controller = nullptr; xmrig::Controller *Workers::m_controller = nullptr;
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] = {};
std::atomic<uint32_t> Workers::m_rx_dataset_init_thread_counter = 0;
xmrig::Job Workers::job() xmrig::Job Workers::job()
@ -187,6 +192,7 @@ void Workers::start(xmrig::Controller *controller)
uv_mutex_init(&m_mutex); uv_mutex_init(&m_mutex);
uv_rwlock_init(&m_rwlock); uv_rwlock_init(&m_rwlock);
uv_rwlock_init(&m_rx_dataset_lock);
m_sequence = 1; m_sequence = 1;
m_paused = 1; m_paused = 1;
@ -356,3 +362,61 @@ void Workers::start(IWorker *worker)
worker->start(); worker->start();
} }
void Workers::updateDataset(const uint8_t* seed_hash, const uint32_t num_threads)
{
// Check if we need to update cache and dataset
if (memcmp(m_rx_seed_hash, seed_hash, sizeof(m_rx_seed_hash)) == 0)
return;
const uint32_t thread_id = m_rx_dataset_init_thread_counter++;
LOG_NOTICE("Thread %u started updating RandomX dataset", thread_id);
// Wait for all threads to get here
do {
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 (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_NOTICE("Thread %u finished updating RandomX dataset", thread_id);
// Wait for all threads to complete
--m_rx_dataset_init_thread_counter;
do {
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_flags>(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;
}

View file

@ -34,6 +34,7 @@
#include "base/net/stratum/Job.h" #include "base/net/stratum/Job.h"
#include "net/JobResult.h" #include "net/JobResult.h"
#include "rapidjson/fwd.h" #include "rapidjson/fwd.h"
#include "randomwow.h"
class Hashrate; class Hashrate;
@ -72,6 +73,9 @@ public:
static void threadsSummary(rapidjson::Document &doc); static void threadsSummary(rapidjson::Document &doc);
# endif # endif
static void updateDataset(const uint8_t* seed_hash, uint32_t num_threads);
static randomx_dataset* getDataset();
private: private:
static void onReady(void *arg); static void onReady(void *arg);
static void onResult(uv_async_t *handle); static void onResult(uv_async_t *handle);
@ -114,6 +118,12 @@ private:
static uv_rwlock_t m_rwlock; static uv_rwlock_t m_rwlock;
static uv_timer_t *m_timer; static uv_timer_t *m_timer;
static xmrig::Controller *m_controller; static xmrig::Controller *m_controller;
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 std::atomic<uint32_t> m_rx_dataset_init_thread_counter;
}; };