Optimized RandomX initialization and switching.

This commit is contained in:
XMRig 2019-08-01 20:37:05 +07:00
parent ab0d3b8919
commit 84ff8af4bd
9 changed files with 156 additions and 157 deletions

View file

@ -82,7 +82,9 @@ xmrig::CpuWorker<N>::~CpuWorker()
template<size_t N> template<size_t N>
void xmrig::CpuWorker<N>::allocateRandomX_VM() void xmrig::CpuWorker<N>::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)); std::this_thread::sleep_for(std::chrono::milliseconds(200));
if (Nonce::sequence(Nonce::CPU) == 0) { if (Nonce::sequence(Nonce::CPU) == 0) {
@ -90,13 +92,6 @@ void xmrig::CpuWorker<N>::allocateRandomX_VM()
} }
} }
RxDataset *dataset = Rx::dataset(m_node);
assert(dataset != nullptr);
if (!dataset) {
return;
}
if (!m_vm) { if (!m_vm) {
m_vm = new RxVm(dataset, m_memory->scratchpad(), !m_hwAES); m_vm = new RxVm(dataset, m_memory->scratchpad(), !m_hwAES);
} }

View file

@ -31,6 +31,7 @@
#include <algorithm> #include <algorithm>
#include <mutex>
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <time.h> #include <time.h>
@ -69,14 +70,11 @@ public:
inline LogPrivate() : inline LogPrivate() :
m_buf() m_buf()
{ {
uv_mutex_init(&m_mutex);
} }
inline ~LogPrivate() inline ~LogPrivate()
{ {
uv_mutex_destroy(&m_mutex);
for (ILogBackend *backend : m_backends) { for (ILogBackend *backend : m_backends) {
delete backend; delete backend;
} }
@ -91,13 +89,14 @@ public:
size_t size = 0; size_t size = 0;
size_t offset = 0; size_t offset = 0;
lock(); std::lock_guard<std::mutex> lock(m_mutex);
timestamp(level, size, offset); timestamp(level, size, offset);
color(level, size); color(level, size);
const int rc = vsnprintf(m_buf + size, sizeof (m_buf) - offset - 32, fmt, args); const int rc = vsnprintf(m_buf + size, sizeof (m_buf) - offset - 32, fmt, args);
if (rc < 0) { if (rc < 0) {
return unlock(); return;
} }
size += std::min(static_cast<size_t>(rc), sizeof (m_buf) - offset - 32); size += std::min(static_cast<size_t>(rc), sizeof (m_buf) - offset - 32);
@ -119,16 +118,10 @@ public:
fputs(txt.c_str(), stdout); fputs(txt.c_str(), stdout);
fflush(stdout); fflush(stdout);
} }
unlock();
} }
private: 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) inline void timestamp(Log::Level level, size_t &size, size_t &offset)
{ {
if (level == Log::NONE) { if (level == Log::NONE) {
@ -192,8 +185,8 @@ private:
char m_buf[4096]; char m_buf[4096];
std::mutex m_mutex;
std::vector<ILogBackend*> m_backends; std::vector<ILogBackend*> m_backends;
uv_mutex_t m_mutex;
}; };

View file

@ -43,17 +43,20 @@ public:
~Buffer(); ~Buffer();
inline char *data() { return m_data; } 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 const char *data() const { return m_data; } inline char *data() { return m_data; }
inline size_t size() const { return m_size; } inline const char *data() const { return m_data; }
inline void from(const Buffer &other) { from(other.data(), other.size()); } 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); void from(const char *data, size_t size);
inline Buffer &operator=(const Buffer &other) { from(other); return *this; } inline bool operator!=(const Buffer &other) const { return !isEqual(other); }
inline Buffer &operator=(Buffer &&other) { move(std::move(other)); return *this; } 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); static Buffer allocUnsafe(size_t size);

View file

@ -380,7 +380,7 @@ void xmrig::Miner::setJob(const Job &job, bool donate)
} }
# ifdef XMRIG_ALGO_RANDOMX # ifdef XMRIG_ALGO_RANDOMX
Rx::init(job, Rx::init(d_ptr->job,
d_ptr->controller->config()->rx().threads(), d_ptr->controller->config()->rx().threads(),
d_ptr->controller->config()->cpu().isHugePages(), d_ptr->controller->config()->cpu().isHugePages(),
d_ptr->controller->config()->rx().isNUMA() d_ptr->controller->config()->rx().isNUMA()

View file

@ -43,6 +43,7 @@
#include "base/tools/Buffer.h" #include "base/tools/Buffer.h"
#include "base/tools/Chrono.h" #include "base/tools/Chrono.h"
#include "crypto/rx/Rx.h" #include "crypto/rx/Rx.h"
#include "crypto/rx/RxAlgo.h"
#include "crypto/rx/RxCache.h" #include "crypto/rx/RxCache.h"
#include "crypto/rx/RxDataset.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; 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<uint64_t>(hwloc_bitmap_first(node->cpuset)));
}
hwloc_topology_destroy(topology);
}
#else
inline static void bindToNUMANode(uint32_t) {}
#endif
class RxPrivate class RxPrivate
{ {
public: 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) static void allocate(uint32_t nodeId)
{ {
const uint64_t ts = Chrono::steadyMSecs(); const uint64_t ts = Chrono::steadyMSecs();
# ifdef XMRIG_FEATURE_HWLOC if (d_ptr->isNUMA()) {
if (d_ptr->numa) { bindToNUMANode(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<uint64_t>(hwloc_bitmap_first(node->cpuset)));
}
hwloc_topology_destroy(topology);
} }
# endif
LOG_INFO("%s" CYAN_BOLD("#%u") MAGENTA_BOLD(" allocate") CYAN_BOLD(" %zu MB") BLACK_BOLD(" (%zu+%zu) for RandomX dataset & cache"), LOG_INFO("%s" CYAN_BOLD("#%u") MAGENTA_BOLD(" allocate") CYAN_BOLD(" %zu MB") BLACK_BOLD(" (%zu+%zu) for RandomX dataset & cache"),
tag, tag,
@ -110,7 +137,7 @@ public:
RxCache::size() / 1024 / 1024 RxCache::size() / 1024 / 1024
); );
RxDataset *dataset = new RxDataset(d_ptr->hugePages); RxDataset *dataset = new RxDataset(d_ptr->m_hugePages);
d_ptr->datasets[nodeId] = dataset; d_ptr->datasets[nodeId] = dataset;
if (dataset->get() != nullptr) { if (dataset->get() != nullptr) {
@ -134,29 +161,84 @@ public:
} }
bool hugePages = true; static void initDataset(uint32_t nodeId, uint32_t threads)
bool numa = true; {
std::lock_guard<std::mutex> 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<uint32_t, RxDataset *> datasets; std::map<uint32_t, RxDataset *> datasets;
std::mutex mutex; 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 } // 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<std::mutex> lock(d_ptr->mutex); std::lock_guard<std::mutex> lock(d_ptr->mutex);
if (!d_ptr->isReady(job)) {
return nullptr;
}
return isReady(job.seedHash(), job.algorithm(), d_ptr->numa ? nodeId : 0); return d_ptr->datasets.at(d_ptr->isNUMA() ? nodeId : 0);
}
xmrig::RxDataset *xmrig::Rx::dataset(uint32_t nodeId)
{
std::lock_guard<std::mutex> lock(d_ptr->mutex);
return d_ptr->datasets[d_ptr->numa ? nodeId : 0];
} }
@ -201,88 +283,33 @@ void xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa)
std::lock_guard<std::mutex> lock(d_ptr->mutex); std::lock_guard<std::mutex> lock(d_ptr->mutex);
size_t ready = 0; if (d_ptr->isReady(job)) {
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()) {
return; return;
} }
d_ptr->hugePages = hugePages; d_ptr->setState(job, hugePages, numa);
d_ptr->numa = numa && Cpu::info()->nodes() > 1; const uint32_t threads = initThreads < 1 ? static_cast<uint32_t>(Cpu::info()->threads()) : static_cast<uint32_t>(initThreads);
const uint32_t threads = initThreads < 1 ? static_cast<uint32_t>(Cpu::info()->threads()) const String buf = Buffer::toHex(job.seedHash(), 8);
: static_cast<uint32_t>(initThreads);
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 # ifdef XMRIG_FEATURE_HWLOC
if (d_ptr->numa) { if (d_ptr->isNUMA()) {
for (uint32_t nodeId : HwlocCpuInfo::nodeIndexes()) { for (auto const &item : d_ptr->datasets) {
std::thread thread(initDataset, nodeId, job.seedHash(), job.algorithm(), threads); std::thread thread(RxPrivate::initDataset, item.first, threads);
thread.detach(); thread.detach();
} }
} }
else else
# endif # endif
{ {
std::thread thread(initDataset, 0, job.seedHash(), job.algorithm(), threads); std::thread thread(RxPrivate::initDataset, 0, threads);
thread.detach(); 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<std::mutex> 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);
}
}

View file

@ -44,16 +44,11 @@ class Job;
class Rx class Rx
{ {
public: public:
static bool isReady(const Job &job, uint32_t nodeId); static RxDataset *dataset(const Job &job, uint32_t nodeId);
static RxDataset *dataset(uint32_t nodeId);
static std::pair<size_t, size_t> hugePages(); static std::pair<size_t, size_t> hugePages();
static void destroy(); static void destroy();
static void init(); static void init();
static void init(const Job &job, int initThreads, bool hugePages, bool numa); 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);
}; };

View file

@ -53,11 +53,12 @@ public:
inline randomx_cache *get() const { return m_cache; } inline randomx_cache *get() const { return m_cache; }
bool init(const void *seed); bool init(const void *seed);
bool isReady(const void *seed) const;
static inline constexpr size_t size() { return RANDOMX_CACHE_MAX_SIZE; } static inline constexpr size_t size() { return RANDOMX_CACHE_MAX_SIZE; }
private: private:
bool isReady(const void *seed) const;
int m_flags = 0; int m_flags = 0;
randomx_cache *m_cache = nullptr; randomx_cache *m_cache = nullptr;
uint8_t m_seed[32]; uint8_t m_seed[32];

View file

@ -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); cache()->init(seed);
if (!get()) { 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<size_t, size_t> xmrig::RxDataset::hugePages() const std::pair<size_t, size_t> xmrig::RxDataset::hugePages() const
{ {
constexpr size_t twoMiB = 2u * 1024u * 1024u; constexpr size_t twoMiB = 2u * 1024u * 1024u;

View file

@ -52,8 +52,7 @@ public:
inline randomx_dataset *get() const { return m_dataset; } inline randomx_dataset *get() const { return m_dataset; }
inline RxCache *cache() const { return m_cache; } inline RxCache *cache() const { return m_cache; }
bool init(const void *seed, const Algorithm &algorithm, uint32_t numThreads); bool init(const void *seed, uint32_t numThreads);
bool isReady(const void *seed, const Algorithm &algorithm) const;
std::pair<size_t, size_t> hugePages() const; std::pair<size_t, size_t> hugePages() const;
static inline constexpr size_t size() { return RANDOMX_DATASET_MAX_SIZE; } static inline constexpr size_t size() { return RANDOMX_DATASET_MAX_SIZE; }