Merge branch 'optimize-numa' into evo

This commit is contained in:
XMRig 2019-10-06 11:21:31 +07:00
commit 759573ace3
34 changed files with 1472 additions and 431 deletions

View file

@ -4,9 +4,12 @@ if (WITH_RANDOMX)
list(APPEND HEADERS_CRYPTO
src/crypto/rx/Rx.h
src/crypto/rx/RxAlgo.h
src/crypto/rx/RxBasicStorage.h
src/crypto/rx/RxCache.h
src/crypto/rx/RxConfig.h
src/crypto/rx/RxDataset.h
src/crypto/rx/RxQueue.h
src/crypto/rx/RxSeed.h
src/crypto/rx/RxVm.h
)
@ -32,9 +35,11 @@ if (WITH_RANDOMX)
src/crypto/randomx/vm_interpreted.cpp
src/crypto/rx/Rx.cpp
src/crypto/rx/RxAlgo.cpp
src/crypto/rx/RxBasicStorage.cpp
src/crypto/rx/RxCache.cpp
src/crypto/rx/RxConfig.cpp
src/crypto/rx/RxDataset.cpp
src/crypto/rx/RxQueue.cpp
src/crypto/rx/RxVm.cpp
)
@ -63,6 +68,21 @@ if (WITH_RANDOMX)
if (CMAKE_CXX_COMPILER_ID MATCHES Clang)
set_source_files_properties(src/crypto/randomx/jit_compiler_x86.cpp PROPERTIES COMPILE_FLAGS -Wno-unused-const-variable)
endif()
if (WITH_HWLOC)
list(APPEND SOURCES_CRYPTO
src/crypto/rx/RxNUMAStorage.h
)
list(APPEND SOURCES_CRYPTO
src/crypto/rx/RxConfig_hwloc.cpp
src/crypto/rx/RxNUMAStorage.cpp
)
else()
list(APPEND SOURCES_CRYPTO
src/crypto/rx/RxConfig_basic.cpp
)
endif()
else()
remove_definitions(/DXMRIG_ALGO_RANDOMX)
endif()

View file

@ -38,6 +38,11 @@ const char *ocl_tag();
#endif
#ifdef XMRIG_ALGO_RANDOMX
const char *rx_tag();
#endif
} // namespace xmrig

View file

@ -3,6 +3,7 @@ set(HEADERS_BACKEND_COMMON
src/backend/common/Tags.h
src/backend/common/interfaces/IBackend.h
src/backend/common/interfaces/IRxListener.h
src/backend/common/interfaces/IRxStorage.h
src/backend/common/interfaces/IThread.h
src/backend/common/interfaces/IWorker.h
src/backend/common/misc/PciTopology.h

View file

@ -0,0 +1,53 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2018 XMRig <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_IRXSTORAGE_H
#define XMRIG_IRXSTORAGE_H
#include <cstdint>
#include <utility>
namespace xmrig {
class Job;
class RxDataset;
class RxSeed;
class IRxStorage
{
public:
virtual ~IRxStorage() = default;
virtual RxDataset *dataset(const Job &job, uint32_t nodeId) const = 0;
virtual std::pair<uint32_t, uint32_t> hugePages() const = 0;
virtual void init(const RxSeed &seed, uint32_t threads, bool hugePages) = 0;
};
} /* namespace xmrig */
#endif // XMRIG_IRXSTORAGE_H

View file

@ -111,13 +111,13 @@ public:
return;
}
LOG_INFO("%s" GREEN_BOLD(" READY") " threads %s%zu/%zu (%zu)" CLEAR " huge pages %s%zu/%zu %1.0f%%" CLEAR " memory " CYAN_BOLD("%zu KB") BLACK_BOLD(" (%" PRIu64 " ms)"),
LOG_INFO("%s" GREEN_BOLD(" READY") " threads %s%zu/%zu (%zu)" CLEAR " huge pages %s%1.0f%% %zu/%zu" CLEAR " memory " CYAN_BOLD("%zu KB") BLACK_BOLD(" (%" PRIu64 " ms)"),
tag,
m_errors == 0 ? CYAN_BOLD_S : YELLOW_BOLD_S,
m_started, m_threads, m_ways,
(m_hugePages == m_pages ? GREEN_BOLD_S : (m_hugePages == 0 ? RED_BOLD_S : YELLOW_BOLD_S)),
m_hugePages, m_pages,
m_hugePages == 0 ? 0.0 : static_cast<double>(m_hugePages) / m_pages * 100.0,
m_hugePages, m_pages,
memory() / 1024,
Chrono::steadyMSecs() - m_ts
);

View file

@ -69,12 +69,12 @@ xmrig::CpuWorker<N>::CpuWorker(size_t id, const CpuLaunchData &data) :
template<size_t N>
xmrig::CpuWorker<N>::~CpuWorker()
{
CnCtx::release(m_ctx, N);
delete m_memory;
# ifdef XMRIG_ALGO_RANDOMX
delete m_vm;
# endif
CnCtx::release(m_ctx, N);
delete m_memory;
}

View file

@ -46,7 +46,6 @@
namespace xmrig {
std::vector<uint32_t> HwlocCpuInfo::m_nodeIndexes;
uint32_t HwlocCpuInfo::m_features = 0;
@ -185,11 +184,11 @@ xmrig::HwlocCpuInfo::HwlocCpuInfo()
m_features |= SET_THISTHREAD_MEMBIND;
}
m_nodeIndexes.reserve(m_nodes);
m_nodeset.reserve(m_nodes);
hwloc_obj_t node = nullptr;
while ((node = hwloc_get_next_obj_by_type(m_topology, HWLOC_OBJ_NUMANODE, node)) != nullptr) {
m_nodeIndexes.emplace_back(node->os_index);
m_nodeset.emplace_back(node->os_index);
}
}
}
@ -201,6 +200,20 @@ xmrig::HwlocCpuInfo::~HwlocCpuInfo()
}
bool xmrig::HwlocCpuInfo::membind(hwloc_const_bitmap_t nodeset)
{
if (!hwloc_topology_get_support(m_topology)->membind->set_thisthread_membind) {
return false;
}
# if HWLOC_API_VERSION >= 0x20000
return hwloc_set_membind(m_topology, nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_THREAD | HWLOC_MEMBIND_BYNODESET) >= 0;
# else
return hwloc_set_membind_nodeset(m_topology, nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_THREAD) >= 0;
# endif
}
xmrig::CpuThreads xmrig::HwlocCpuInfo::threads(const Algorithm &algorithm, uint32_t limit) const
{
if (L2() == 0 && L3() == 0) {

View file

@ -30,6 +30,7 @@
#include "base/tools/Object.h"
using hwloc_const_bitmap_t = const struct hwloc_bitmap_s *;
using hwloc_obj_t = struct hwloc_obj *;
using hwloc_topology_t = struct hwloc_topology *;
@ -52,7 +53,11 @@ public:
~HwlocCpuInfo() override;
static inline bool has(Feature feature) { return m_features & feature; }
static inline const std::vector<uint32_t> &nodeIndexes() { return m_nodeIndexes; }
inline const std::vector<uint32_t> &nodeset() const { return m_nodeset; }
inline hwloc_topology_t topology() const { return m_topology; }
bool membind(hwloc_const_bitmap_t nodeset);
protected:
CpuThreads threads(const Algorithm &algorithm, uint32_t limit) const override;
@ -67,7 +72,7 @@ protected:
private:
void processTopLevelCache(hwloc_obj_t obj, const Algorithm &algorithm, CpuThreads &threads, size_t limit) const;
static std::vector<uint32_t> m_nodeIndexes;
static uint32_t m_features;
char m_backend[20] = { 0 };
@ -76,6 +81,7 @@ private:
size_t m_cores = 0;
size_t m_nodes = 0;
size_t m_packages = 0;
std::vector<uint32_t> m_nodeset;
};

View file

@ -122,7 +122,7 @@ void xmrig::OclRxBaseRunner::set(const Job &job, uint8_t *blob)
m_seed = job.seed();
auto dataset = Rx::dataset(job, 0);
enqueueWriteBuffer(m_dataset, CL_TRUE, 0, dataset->size(), dataset->raw());
enqueueWriteBuffer(m_dataset, CL_TRUE, 0, RxDataset::maxSize(), dataset->raw());
}
if (job.size() < Job::kMaxBlobSize) {

View file

@ -31,10 +31,10 @@
#include <algorithm>
#include <cstring>
#include <ctime>
#include <mutex>
#include <string.h>
#include <string>
#include <time.h>
#include <uv.h>
#include <vector>
@ -42,6 +42,7 @@
#include "base/io/log/Log.h"
#include "base/kernel/interfaces/ILogBackend.h"
#include "base/tools/Chrono.h"
#include "base/tools/Object.h"
namespace xmrig {
@ -67,10 +68,10 @@ static const char *colors_map[] = {
class LogPrivate
{
public:
inline LogPrivate() :
m_buf()
{
}
XMRIG_DISABLE_COPY_MOVE(LogPrivate)
LogPrivate() = default;
inline ~LogPrivate()
@ -134,7 +135,7 @@ private:
const uint64_t ms = Chrono::currentMSecsSinceEpoch();
time_t now = ms / 1000;
tm stime;
tm stime{};
# ifdef _WIN32
localtime_s(&stime, &now);
@ -188,7 +189,7 @@ private:
}
char m_buf[4096];
char m_buf[4096]{};
std::mutex m_mutex;
std::vector<ILogBackend*> m_backends;
};

View file

@ -47,7 +47,6 @@ xmrig::ConsoleLog::ConsoleLog()
}
uv_tty_set_mode(m_tty, UV_TTY_MODE_NORMAL);
m_stream = reinterpret_cast<uv_stream_t*>(m_tty);
# ifdef WIN32
HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
@ -68,26 +67,15 @@ xmrig::ConsoleLog::~ConsoleLog()
}
void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t size, bool colors)
void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t, bool colors)
{
if (!m_tty || Log::colors != colors) {
return;
}
# ifdef _WIN32
uv_buf_t buf = uv_buf_init(const_cast<char *>(line), static_cast<unsigned int>(size));
# else
uv_buf_t buf = uv_buf_init(const_cast<char *>(line), size);
# endif
if (!isWritable()) {
fputs(line, stdout);
fflush(stdout);
}
else {
uv_try_write(m_stream, &buf, 1);
}
}
bool xmrig::ConsoleLog::isSupported() const
@ -95,13 +83,3 @@ bool xmrig::ConsoleLog::isSupported() const
const uv_handle_type type = uv_guess_handle(1);
return type == UV_TTY || type == UV_NAMED_PIPE;
}
bool xmrig::ConsoleLog::isWritable() const
{
if (!m_stream || uv_is_writable(m_stream) != 1) {
return false;
}
return isSupported();
}

View file

@ -51,9 +51,7 @@ protected:
private:
bool isSupported() const;
bool isWritable() const;
uv_stream_t *m_stream = nullptr;
uv_tty_t *m_tty = nullptr;
};

View file

@ -72,12 +72,8 @@ class MinerPrivate
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(MinerPrivate)
inline MinerPrivate(Controller *controller) : controller(controller)
{
# ifdef XMRIG_ALGO_RANDOMX
Rx::init();
# endif
}
inline MinerPrivate(Controller *controller) : controller(controller) {}
inline ~MinerPrivate()
@ -232,9 +228,9 @@ public:
# ifdef XMRIG_ALGO_RANDOMX
bool initRX(IRxListener *listener)
inline bool initRX()
{
return Rx::init(job, controller->config()->rx().threads(), controller->config()->cpu().isHugePages(), controller->config()->rx().isNUMA(), listener);
return Rx::init(job, controller->config()->rx(), controller->config()->cpu().isHugePages());
}
# endif
@ -261,6 +257,10 @@ public:
xmrig::Miner::Miner(Controller *controller)
: d_ptr(new MinerPrivate(controller))
{
# ifdef XMRIG_ALGO_RANDOMX
Rx::init(this);
# endif
controller->addListener(this);
# ifdef XMRIG_FEATURE_API
@ -402,7 +402,7 @@ void xmrig::Miner::setJob(const Job &job, bool donate)
}
# ifdef XMRIG_ALGO_RANDOMX
const bool ready = d_ptr->initRX(this);
const bool ready = d_ptr->initRX();
# else
constexpr const bool ready = true;
# endif

View file

@ -33,6 +33,7 @@
#include "base/api/interfaces/IApiListener.h"
#include "base/kernel/interfaces/IBaseListener.h"
#include "base/kernel/interfaces/ITimerListener.h"
#include "base/tools/Object.h"
#include "crypto/common/Algorithm.h"
@ -48,6 +49,8 @@ class IBackend;
class Miner : public ITimerListener, public IBaseListener, public IApiListener, public IRxListener
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Miner)
Miner(Controller *controller);
~Miner() override;

View file

@ -23,9 +23,9 @@
*/
#include <algorithm>
#include <string.h>
#include <cstring>
#include <uv.h>
#include <inttypes.h>
#include <cinttypes>
#include "backend/cpu/Cpu.h"
@ -79,7 +79,7 @@ public:
}
xmrig::Config::Config() : BaseConfig(),
xmrig::Config::Config() :
d_ptr(new ConfigPrivate())
{
}

View file

@ -28,57 +28,34 @@
#ifdef XMRIG_FEATURE_HWLOC
# include <hwloc.h>
# include "backend/cpu/platform/HwlocCpuInfo.h"
#
# if HWLOC_API_VERSION < 0x00010b00
# define HWLOC_OBJ_NUMANODE HWLOC_OBJ_NODE
# endif
#endif
#include "base/io/log/Log.h"
#include "crypto/common/VirtualMemory.h"
#include "backend/cpu/Cpu.h"
#include "base/io/log/Log.h"
#include <cinttypes>
uint32_t xmrig::VirtualMemory::bindToNUMANode(int64_t affinity)
{
# ifdef XMRIG_FEATURE_HWLOC
if (affinity < 0 || !HwlocCpuInfo::has(HwlocCpuInfo::SET_THISTHREAD_MEMBIND)) {
if (affinity < 0 || Cpu::info()->nodes() < 2) {
return 0;
}
hwloc_topology_t topology;
hwloc_topology_init(&topology);
hwloc_topology_load(topology);
auto cpu = static_cast<HwlocCpuInfo *>(Cpu::info());
hwloc_obj_t pu = hwloc_get_pu_obj_by_os_index(cpu->topology(), static_cast<unsigned>(affinity));
const unsigned puId = static_cast<unsigned>(affinity);
if (pu == nullptr || !cpu->membind(pu->nodeset)) {
LOG_WARN("CPU #%02" PRId64 " warning: \"can't bind memory\"", affinity);
hwloc_obj_t pu = hwloc_get_pu_obj_by_os_index(topology, puId);
# if HWLOC_API_VERSION >= 0x20000
if (pu == nullptr || hwloc_set_membind(topology, pu->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_THREAD | HWLOC_MEMBIND_BYNODESET) < 0) {
# else
if (pu == nullptr || hwloc_set_membind_nodeset(topology, pu->nodeset, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_THREAD) < 0) {
# endif
LOG_WARN("CPU #%02u warning: \"can't bind memory\"", puId);
return 0;
}
uint32_t nodeId = 0;
if (pu) {
hwloc_obj_t node = nullptr;
while ((node = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_NUMANODE, node)) != nullptr) {
if (hwloc_bitmap_intersects(node->cpuset, pu->cpuset)) {
nodeId = node->os_index;
break;
}
}
}
hwloc_topology_destroy(topology);
return nodeId;
return hwloc_bitmap_first(pu->nodeset);
# else
return 0;
# endif

View file

@ -26,32 +26,10 @@
#include "crypto/rx/Rx.h"
#include "backend/common/interfaces/IRxListener.h"
#include "backend/cpu/Cpu.h"
#include "backend/common/Tags.h"
#include "base/io/log/Log.h"
#include "base/kernel/Platform.h"
#include "base/net/stratum/Job.h"
#include "base/tools/Buffer.h"
#include "base/tools/Chrono.h"
#include "base/tools/Handle.h"
#include "base/tools/Object.h"
#include "crypto/rx/RxAlgo.h"
#include "crypto/rx/RxCache.h"
#include "crypto/rx/RxDataset.h"
#ifdef XMRIG_FEATURE_HWLOC
# include <hwloc.h>
# include "backend/cpu/platform/HwlocCpuInfo.h"
#endif
#include <atomic>
#include <map>
#include <mutex>
#include <thread>
#include <uv.h>
#include "crypto/rx/RxConfig.h"
#include "crypto/rx/RxQueue.h"
namespace xmrig {
@ -62,239 +40,37 @@ class RxPrivate;
static const char *tag = BLUE_BG(WHITE_BOLD_S " rx ") " ";
static RxPrivate *d_ptr = nullptr;
static std::mutex mutex;
#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
{
public:
XMRIG_DISABLE_COPY_MOVE(RxPrivate)
inline RxPrivate(IRxListener *listener) : queue(listener) {}
inline RxPrivate() :
m_counter(0),
m_last(0)
{
m_async = new uv_async_t;
m_async->data = this;
uv_async_init(uv_default_loop(), m_async, [](uv_async_t *) { d_ptr->onReady(); });
# 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 });
}
}
inline ~RxPrivate()
{
Handle::close(m_async);
for (auto const &item : datasets) {
delete item.second;
}
datasets.clear();
}
inline bool isNUMA() const { return m_numa; }
inline bool isReady(const Job &job) const { return m_ready == count() && m_algorithm == job.algorithm() && m_seed == job.seed(); }
inline const Algorithm &algorithm() const { return m_algorithm; }
inline const Buffer &seed() const { return m_seed; }
inline size_t count() const { return isNUMA() ? datasets.size() : 1; }
inline uint64_t counter() { return m_counter.load(std::memory_order_relaxed); }
inline void asyncSend(uint64_t counter) { m_ready++; if (m_ready == count()) { m_last = counter; uv_async_send(m_async); } }
static void allocate(uint32_t nodeId)
{
const uint64_t ts = Chrono::steadyMSecs();
if (d_ptr->isNUMA()) {
bindToNUMANode(nodeId);
}
LOG_INFO("%s" CYAN_BOLD("#%u") MAGENTA_BOLD(" allocate") CYAN_BOLD(" %zu MB") BLACK_BOLD(" (%zu+%zu) for RandomX dataset & cache"),
tag,
nodeId,
(RxDataset::maxSize() + RxCache::maxSize()) / 1024 / 1024,
RxDataset::maxSize() / 1024 / 1024,
RxCache::maxSize() / 1024 / 1024
);
auto dataset = new RxDataset(d_ptr->m_hugePages);
d_ptr->datasets[nodeId] = dataset;
if (dataset->get() != nullptr) {
const auto hugePages = dataset->hugePages();
const double percent = hugePages.first == 0 ? 0.0 : static_cast<double>(hugePages.first) / hugePages.second * 100.0;
LOG_INFO("%s" CYAN_BOLD("#%u") GREEN(" allocate done") " huge pages %s%u/%u %1.0f%%" CLEAR " %sJIT" BLACK_BOLD(" (%" PRIu64 " ms)"),
tag,
nodeId,
(hugePages.first == hugePages.second ? GREEN_BOLD_S : (hugePages.first == 0 ? RED_BOLD_S : YELLOW_BOLD_S)),
hugePages.first,
hugePages.second,
percent,
dataset->cache()->isJIT() ? GREEN_BOLD_S "+" : RED_BOLD_S "-",
Chrono::steadyMSecs() - ts
);
}
else {
LOG_WARN(CLEAR "%s" CYAN_BOLD("#%u") YELLOW_BOLD_S " failed to allocate RandomX dataset, switching to slow mode", tag, nodeId);
}
}
static void initDataset(uint32_t nodeId, uint32_t threads, uint64_t counter)
{
std::lock_guard<std::mutex> lock(mutex);
const uint64_t ts = Chrono::steadyMSecs();
d_ptr->getOrAllocate(nodeId)->init(d_ptr->seed(), threads);
d_ptr->asyncSend(counter);
LOG_INFO("%s" CYAN_BOLD("#%u") GREEN(" init done ") CYAN_BOLD("%zu/%zu") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, nodeId, d_ptr->m_ready, d_ptr->count(), 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, IRxListener *listener)
{
if (m_algorithm != job.algorithm()) {
m_algorithm = RxAlgo::apply(job.algorithm());
}
m_ready = 0;
m_numa = numa && Cpu::info()->nodes() > 1;
m_hugePages = hugePages;
m_listener = listener;
m_seed = job.seed();
++m_counter;
}
std::map<uint32_t, RxDataset *> datasets;
private:
inline void onReady()
{
if (m_listener && counter() == m_last.load(std::memory_order_relaxed)) {
m_listener->onDatasetReady();
}
}
Algorithm m_algorithm;
bool m_hugePages = true;
bool m_numa = true;
Buffer m_seed;
IRxListener *m_listener = nullptr;
size_t m_ready = 0;
std::atomic<uint64_t> m_counter;
std::atomic<uint64_t> m_last;
uv_async_t *m_async = nullptr;
RxQueue queue;
};
} // namespace xmrig
bool xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa, IRxListener *listener)
const char *xmrig::rx_tag()
{
return tag;
}
bool xmrig::Rx::init(const Job &job, const RxConfig &config, bool hugePages)
{
if (job.algorithm().family() != Algorithm::RANDOM_X) {
return true;
}
std::lock_guard<std::mutex> lock(mutex);
if (d_ptr->isReady(job)) {
if (isReady(job)) {
return true;
}
d_ptr->setState(job, hugePages, numa, listener);
const uint32_t threads = initThreads < 1 ? static_cast<uint32_t>(Cpu::info()->threads()) : static_cast<uint32_t>(initThreads);
const String buf = Buffer::toHex(job.seed().data(), 8);
const uint64_t counter = d_ptr->counter();
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->isNUMA()) {
for (auto const &item : d_ptr->datasets) {
std::thread thread(RxPrivate::initDataset, item.first, threads, counter);
thread.detach();
}
}
else
# endif
{
std::thread thread(RxPrivate::initDataset, 0, threads, counter);
thread.detach();
}
d_ptr->queue.enqueue(job, config.nodeset(), config.threads(), hugePages);
return false;
}
@ -302,39 +78,19 @@ bool xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa,
bool xmrig::Rx::isReady(const Job &job)
{
std::lock_guard<std::mutex> lock(mutex);
return d_ptr->isReady(job);
return d_ptr->queue.isReady(job);
}
xmrig::RxDataset *xmrig::Rx::dataset(const Job &job, uint32_t nodeId)
{
std::lock_guard<std::mutex> lock(mutex);
if (!d_ptr->isReady(job)) {
return nullptr;
}
return d_ptr->datasets.at(d_ptr->isNUMA() ? (d_ptr->datasets.count(nodeId) ? nodeId : HwlocCpuInfo::nodeIndexes().front()) : 0);
return d_ptr->queue.dataset(job, nodeId);
}
std::pair<unsigned, unsigned> xmrig::Rx::hugePages()
std::pair<uint32_t, uint32_t> xmrig::Rx::hugePages()
{
std::pair<unsigned, unsigned> pages(0, 0);
std::lock_guard<std::mutex> lock(mutex);
for (auto const &item : d_ptr->datasets) {
if (!item.second) {
continue;
}
const auto p = item.second->hugePages();
pages.first += p.first;
pages.second += p.second;
}
return pages;
return d_ptr->queue.hugePages();
}
@ -346,7 +102,7 @@ void xmrig::Rx::destroy()
}
void xmrig::Rx::init()
void xmrig::Rx::init(IRxListener *listener)
{
d_ptr = new RxPrivate();
d_ptr = new RxPrivate(listener);
}

View file

@ -39,18 +39,19 @@ namespace xmrig
class Algorithm;
class IRxListener;
class Job;
class RxConfig;
class RxDataset;
class Rx
{
public:
static bool init(const Job &job, int initThreads, bool hugePages, bool numa, IRxListener *listener);
static bool init(const Job &job, const RxConfig &config, bool hugePages);
static bool isReady(const Job &job);
static RxDataset *dataset(const Job &job, uint32_t nodeId);
static std::pair<unsigned, unsigned> hugePages();
static std::pair<uint32_t, uint32_t> hugePages();
static void destroy();
static void init();
static void init(IRxListener *listener);
};

View file

@ -0,0 +1,169 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "crypto/rx/RxBasicStorage.h"
#include "backend/common/Tags.h"
#include "base/io/log/Log.h"
#include "base/tools/Chrono.h"
#include "base/tools/Object.h"
#include "crypto/rx/RxAlgo.h"
#include "crypto/rx/RxCache.h"
#include "crypto/rx/RxDataset.h"
#include "crypto/rx/RxSeed.h"
namespace xmrig {
constexpr size_t oneMiB = 1024 * 1024;
class RxBasicStoragePrivate
{
public:
XMRIG_DISABLE_COPY_MOVE(RxBasicStoragePrivate)
inline RxBasicStoragePrivate() = default;
inline ~RxBasicStoragePrivate()
{
delete m_dataset;
}
inline bool isReady(const Job &job) const { return m_ready && m_seed == job; }
inline RxDataset *dataset() const { return m_dataset; }
inline void setSeed(const RxSeed &seed)
{
m_ready = false;
if (m_seed.algorithm() != seed.algorithm()) {
RxAlgo::apply(seed.algorithm());
}
m_seed = seed;
}
inline void createDataset(bool hugePages)
{
const uint64_t ts = Chrono::steadyMSecs();
m_dataset = new RxDataset(hugePages, true);
printAllocStatus(ts);
}
inline void initDataset(uint32_t threads)
{
const uint64_t ts = Chrono::steadyMSecs();
m_dataset->init(m_seed.data(), threads);
LOG_INFO("%s" GREEN_BOLD("dataset ready") BLACK_BOLD(" (%" PRIu64 " ms)"), rx_tag(), Chrono::steadyMSecs() - ts);
m_ready = true;
}
private:
void printAllocStatus(uint64_t ts)
{
if (m_dataset->get() != nullptr) {
const auto pages = m_dataset->hugePages();
const double percent = pages.first == 0 ? 0.0 : static_cast<double>(pages.first) / pages.second * 100.0;
LOG_INFO("%s" GREEN_BOLD("allocated") CYAN_BOLD(" %zu MB") BLACK_BOLD(" (%zu+%zu)") " huge pages %s%1.0f%% %u/%u" CLEAR " %sJIT" BLACK_BOLD(" (%" PRIu64 " ms)"),
rx_tag(),
m_dataset->size() / oneMiB,
RxDataset::maxSize() / oneMiB,
RxCache::maxSize() / oneMiB,
(pages.first == pages.second ? GREEN_BOLD_S : (pages.first == 0 ? RED_BOLD_S : YELLOW_BOLD_S)),
percent,
pages.first,
pages.second,
m_dataset->cache()->isJIT() ? GREEN_BOLD_S "+" : RED_BOLD_S "-",
Chrono::steadyMSecs() - ts
);
}
else {
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "failed to allocate RandomX dataset, switching to slow mode" BLACK_BOLD(" (%" PRIu64 " ms)"), rx_tag(), Chrono::steadyMSecs() - ts);
}
}
bool m_ready = false;
RxDataset *m_dataset = nullptr;
RxSeed m_seed;
};
} // namespace xmrig
xmrig::RxBasicStorage::RxBasicStorage() :
d_ptr(new RxBasicStoragePrivate())
{
}
xmrig::RxBasicStorage::~RxBasicStorage()
{
delete d_ptr;
}
xmrig::RxDataset *xmrig::RxBasicStorage::dataset(const Job &job, uint32_t) const
{
if (!d_ptr->isReady(job)) {
return nullptr;
}
return d_ptr->dataset();
}
std::pair<uint32_t, uint32_t> xmrig::RxBasicStorage::hugePages() const
{
if (!d_ptr->dataset()) {
return { 0u, 0u };
}
return d_ptr->dataset()->hugePages();
}
void xmrig::RxBasicStorage::init(const RxSeed &seed, uint32_t threads, bool hugePages)
{
d_ptr->setSeed(seed);
if (!d_ptr->dataset()) {
d_ptr->createDataset(hugePages);
}
d_ptr->initDataset(threads);
}

View file

@ -0,0 +1,63 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_RX_BASICSTORAGE_H
#define XMRIG_RX_BASICSTORAGE_H
#include "backend/common/interfaces/IRxStorage.h"
#include "base/tools/Object.h"
namespace xmrig
{
class RxBasicStoragePrivate;
class RxBasicStorage : public IRxStorage
{
public:
XMRIG_DISABLE_COPY_MOVE(RxBasicStorage);
RxBasicStorage();
~RxBasicStorage() override;
protected:
RxDataset *dataset(const Job &job, uint32_t nodeId) const override;
std::pair<uint32_t, uint32_t> hugePages() const override;
void init(const RxSeed &seed, uint32_t threads, bool hugePages) override;
private:
RxBasicStoragePrivate *d_ptr;
};
} /* namespace xmrig */
#endif /* XMRIG_RX_BASICSTORAGE_H */

View file

@ -25,8 +25,9 @@
*/
#include "crypto/randomx/randomx.h"
#include "crypto/rx/RxCache.h"
#include "crypto/common/VirtualMemory.h"
#include "crypto/randomx/randomx.h"
static_assert(RANDOMX_FLAG_JIT == 8, "RANDOMX_FLAG_JIT flag mismatch");
@ -72,3 +73,17 @@ bool xmrig::RxCache::init(const Buffer &seed)
return true;
}
std::pair<uint32_t, uint32_t> xmrig::RxCache::hugePages() const
{
constexpr size_t twoMiB = 2u * 1024u * 1024u;
constexpr size_t total = VirtualMemory::align(maxSize(), twoMiB) / twoMiB;
uint32_t count = 0;
if (isHugePages()) {
count += total;
}
return { count, total };
}

View file

@ -55,8 +55,10 @@ public:
inline bool isJIT() const { return m_flags & 8; }
inline const Buffer &seed() const { return m_seed; }
inline randomx_cache *get() const { return m_cache; }
inline size_t size() const { return maxSize(); }
bool init(const Buffer &seed);
std::pair<uint32_t, uint32_t> hugePages() const;
static inline constexpr size_t maxSize() { return RANDOMX_CACHE_MAX_SIZE; }

View file

@ -23,41 +23,11 @@
*/
#include "base/io/json/Json.h"
#include "crypto/rx/RxConfig.h"
#include "rapidjson/document.h"
#include "backend/cpu/Cpu.h"
namespace xmrig {
static const char *kInit = "init";
static const char *kNUMA = "numa";
}
rapidjson::Value xmrig::RxConfig::toJSON(rapidjson::Document &doc) const
uint32_t xmrig::RxConfig::threads() const
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
Value obj(kObjectType);
obj.AddMember(StringRef(kInit), m_threads, allocator);
obj.AddMember(StringRef(kNUMA), m_numa, allocator);
return obj;
}
bool xmrig::RxConfig::read(const rapidjson::Value &value)
{
if (value.IsObject()) {
m_numa = Json::getBool(value, kNUMA, m_numa);
m_threads = Json::getInt(value, kInit, m_threads);
return true;
}
return false;
return m_threads < 1 ? static_cast<uint32_t>(Cpu::info()->threads()) : static_cast<uint32_t>(m_threads);
}

View file

@ -29,6 +29,9 @@
#include "rapidjson/fwd.h"
#include <vector>
namespace xmrig {
@ -38,12 +41,22 @@ public:
bool read(const rapidjson::Value &value);
rapidjson::Value toJSON(rapidjson::Document &doc) const;
inline bool isNUMA() const { return m_numa; }
inline int threads() const { return m_threads; }
# ifdef XMRIG_FEATURE_HWLOC
std::vector<uint32_t> nodeset() const;
# else
inline std::vector<uint32_t> nodeset() const { return std::vector<uint32_t>(); }
# endif
uint32_t threads() const;
private:
bool m_numa = true;
int m_threads = -1;
# ifdef XMRIG_FEATURE_HWLOC
std::vector<uint32_t> m_nodeset;
# endif
};

View file

@ -0,0 +1,59 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "crypto/rx/RxConfig.h"
#include "base/io/json/Json.h"
#include "rapidjson/document.h"
namespace xmrig {
static const char *kInit = "init";
}
rapidjson::Value xmrig::RxConfig::toJSON(rapidjson::Document &doc) const
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
Value obj(kObjectType);
obj.AddMember(StringRef(kInit), m_threads, allocator);
return obj;
}
bool xmrig::RxConfig::read(const rapidjson::Value &value)
{
if (value.IsObject()) {
m_threads = Json::getInt(value, kInit, m_threads);
return true;
}
return false;
}

View file

@ -0,0 +1,100 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "backend/cpu/Cpu.h"
#include "backend/cpu/platform/HwlocCpuInfo.h"
#include "base/io/json/Json.h"
#include "crypto/rx/RxConfig.h"
#include "rapidjson/document.h"
namespace xmrig {
static const char *kInit = "init";
static const char *kNUMA = "numa";
}
rapidjson::Value xmrig::RxConfig::toJSON(rapidjson::Document &doc) const
{
using namespace rapidjson;
auto &allocator = doc.GetAllocator();
Value obj(kObjectType);
obj.AddMember(StringRef(kInit), m_threads, allocator);
if (!m_nodeset.empty()) {
Value numa(kArrayType);
for (uint32_t i : m_nodeset) {
numa.PushBack(i, allocator);
}
obj.AddMember(StringRef(kNUMA), numa, allocator);
}
else {
obj.AddMember(StringRef(kNUMA), m_numa, allocator);
}
return obj;
}
bool xmrig::RxConfig::read(const rapidjson::Value &value)
{
if (value.IsObject()) {
m_threads = Json::getInt(value, kInit, m_threads);
const auto &numa = Json::getValue(value, kNUMA);
if (numa.IsArray()) {
m_nodeset.reserve(numa.Size());
for (const auto &node : numa.GetArray()) {
if (node.IsUint()) {
m_nodeset.emplace_back(node.GetUint());
}
}
}
else if (numa.IsBool()) {
m_numa = numa.GetBool();
}
return true;
}
return false;
}
std::vector<uint32_t> xmrig::RxConfig::nodeset() const
{
if (!m_nodeset.empty()) {
return m_nodeset;
}
return (m_numa && Cpu::info()->nodes() > 1) ? static_cast<HwlocCpuInfo *>(Cpu::info())->nodeset() : std::vector<uint32_t>();
}

View file

@ -25,33 +25,33 @@
*/
#include <thread>
#include "crypto/rx/RxDataset.h"
#include "crypto/common/VirtualMemory.h"
#include "crypto/randomx/randomx.h"
#include "crypto/rx/RxAlgo.h"
#include "crypto/rx/RxCache.h"
#include "crypto/rx/RxDataset.h"
#include <thread>
static_assert(RANDOMX_FLAG_LARGE_PAGES == 1, "RANDOMX_FLAG_LARGE_PAGES flag mismatch");
xmrig::RxDataset::RxDataset(bool hugePages)
xmrig::RxDataset::RxDataset(bool hugePages, bool cache)
{
if (hugePages) {
m_flags = RANDOMX_FLAG_LARGE_PAGES;
m_dataset = randomx_alloc_dataset(static_cast<randomx_flags>(m_flags));
}
if (!m_dataset) {
m_flags = RANDOMX_FLAG_DEFAULT;
m_dataset = randomx_alloc_dataset(static_cast<randomx_flags>(m_flags));
}
allocate(hugePages);
if (cache) {
m_cache = new RxCache(hugePages);
}
}
xmrig::RxDataset::RxDataset(RxCache *cache) :
m_cache(cache)
{
}
xmrig::RxDataset::~RxDataset()
@ -66,7 +66,11 @@ xmrig::RxDataset::~RxDataset()
bool xmrig::RxDataset::init(const Buffer &seed, uint32_t numThreads)
{
cache()->init(seed);
if (!m_cache) {
return false;
}
m_cache->init(seed);
if (!get()) {
return true;
@ -96,18 +100,39 @@ bool xmrig::RxDataset::init(const Buffer &seed, uint32_t numThreads)
}
std::pair<size_t, size_t> xmrig::RxDataset::hugePages() const
size_t xmrig::RxDataset::size(bool cache) const
{
constexpr size_t twoMiB = 2u * 1024u * 1024u;
constexpr const size_t total = (VirtualMemory::align(maxSize(), twoMiB) + VirtualMemory::align(RxCache::maxSize(), twoMiB)) / twoMiB;
size_t size = 0;
size_t count = 0;
if (isHugePages()) {
count += VirtualMemory::align(maxSize(), twoMiB) / twoMiB;
if (m_dataset) {
size += maxSize();
}
if (cache && m_cache) {
size += RxCache::maxSize();
}
return size;
}
std::pair<uint32_t, uint32_t> xmrig::RxDataset::hugePages(bool cache) const
{
constexpr size_t twoMiB = 2u * 1024u * 1024u;
constexpr size_t cacheSize = VirtualMemory::align(RxCache::maxSize(), twoMiB) / twoMiB;
size_t total = VirtualMemory::align(maxSize(), twoMiB) / twoMiB;
uint32_t count = 0;
if (isHugePages()) {
count += total;
}
if (cache && m_cache) {
total += cacheSize;
if (m_cache->isHugePages()) {
count += VirtualMemory::align(RxCache::maxSize(), twoMiB) / twoMiB;
count += cacheSize;
}
}
return { count, total };
@ -118,3 +143,27 @@ void *xmrig::RxDataset::raw() const
{
return m_dataset ? randomx_get_dataset_memory(m_dataset) : nullptr;
}
void xmrig::RxDataset::setRaw(const void *raw)
{
if (!m_dataset) {
return;
}
memcpy(randomx_get_dataset_memory(m_dataset), raw, maxSize());
}
void xmrig::RxDataset::allocate(bool hugePages)
{
if (hugePages) {
m_flags = RANDOMX_FLAG_LARGE_PAGES;
m_dataset = randomx_alloc_dataset(static_cast<randomx_flags>(m_flags));
}
if (!m_dataset) {
m_flags = RANDOMX_FLAG_DEFAULT;
m_dataset = randomx_alloc_dataset(static_cast<randomx_flags>(m_flags));
}
}

View file

@ -49,22 +49,26 @@ class RxDataset
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(RxDataset)
RxDataset(bool hugePages = true);
RxDataset(bool hugePages, bool cache);
RxDataset(RxCache *cache);
~RxDataset();
inline bool isHugePages() const { return m_flags & 1; }
inline randomx_dataset *get() const { return m_dataset; }
inline RxCache *cache() const { return m_cache; }
inline size_t size() const { return maxSize(); }
inline void setCache(RxCache *cache) { m_cache = cache; }
bool init(const Buffer &seed, uint32_t numThreads);
std::pair<size_t, size_t> hugePages() const;
size_t size(bool cache = true) const;
std::pair<uint32_t, uint32_t> hugePages(bool cache = true) const;
void *raw() const;
void setRaw(const void *raw);
static inline constexpr size_t maxSize() { return RANDOMX_DATASET_MAX_SIZE; }
private:
Algorithm m_algorithm;
void allocate(bool hugePages);
int m_flags = 0;
randomx_dataset *m_dataset = nullptr;
RxCache *m_cache = nullptr;

View file

@ -0,0 +1,358 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "crypto/rx/RxNUMAStorage.h"
#include "backend/common/Tags.h"
#include "backend/cpu/Cpu.h"
#include "backend/cpu/platform/HwlocCpuInfo.h"
#include "base/io/log/Log.h"
#include "base/kernel/Platform.h"
#include "base/tools/Chrono.h"
#include "base/tools/Object.h"
#include "crypto/rx/RxAlgo.h"
#include "crypto/rx/RxCache.h"
#include "crypto/rx/RxDataset.h"
#include "crypto/rx/RxSeed.h"
#include <map>
#include <mutex>
#include <hwloc.h>
#include <thread>
namespace xmrig {
constexpr size_t oneMiB = 1024 * 1024;
static std::mutex mutex;
static bool bindToNUMANode(uint32_t nodeId)
{
auto cpu = static_cast<HwlocCpuInfo *>(Cpu::info());
hwloc_obj_t node = hwloc_get_numanode_obj_by_os_index(cpu->topology(), nodeId);
if (!node) {
return false;
}
if (cpu->membind(node->nodeset)) {
Platform::setThreadAffinity(static_cast<uint64_t>(hwloc_bitmap_first(node->cpuset)));
return true;
}
return false;
}
static inline void printSkipped(uint32_t nodeId, const char *reason)
{
LOG_WARN("%s" CYAN_BOLD("#%u ") RED_BOLD("skipped") YELLOW(" (%s)"), rx_tag(), nodeId, reason);
}
static inline void printDatasetReady(uint32_t nodeId, uint64_t ts)
{
LOG_INFO("%s" CYAN_BOLD("#%u ") GREEN_BOLD("dataset ready") BLACK_BOLD(" (%" PRIu64 " ms)"), rx_tag(), nodeId, Chrono::steadyMSecs() - ts);
}
class RxNUMAStoragePrivate
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(RxNUMAStoragePrivate)
inline RxNUMAStoragePrivate(const std::vector<uint32_t> &nodeset) :
m_nodeset(nodeset)
{
m_threads.reserve(nodeset.size());
}
inline ~RxNUMAStoragePrivate()
{
join();
for (auto const &item : m_datasets) {
delete item.second;
}
}
inline bool isAllocated() const { return m_allocated; }
inline bool isReady(const Job &job) const { return m_ready && m_seed == job; }
inline RxDataset *dataset(uint32_t nodeId) const { return m_datasets.count(nodeId) ? m_datasets.at(nodeId) : m_datasets.at(m_nodeset.front()); }
inline void setSeed(const RxSeed &seed)
{
m_ready = false;
if (m_seed.algorithm() != seed.algorithm()) {
RxAlgo::apply(seed.algorithm());
}
m_seed = seed;
}
inline void createDatasets(bool hugePages)
{
const uint64_t ts = Chrono::steadyMSecs();
for (uint32_t node : m_nodeset) {
m_threads.emplace_back(allocate, this, node, hugePages);
}
join();
std::thread thread(allocateCache, this, m_nodeset.front(), hugePages);
thread.join();
if (m_datasets.empty()) {
m_datasets.insert({ m_nodeset.front(), new RxDataset(m_cache) });
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "failed to allocate RandomX datasets, switching to slow mode" BLACK_BOLD(" (%" PRIu64 " ms)"), rx_tag(), Chrono::steadyMSecs() - ts);
}
else {
dataset(m_nodeset.front())->setCache(m_cache);
printAllocStatus(ts);
}
m_allocated = true;
}
inline void initDatasets(uint32_t threads)
{
uint64_t ts = Chrono::steadyMSecs();
auto id = m_nodeset.front();
auto primary = dataset(id);
primary->init(m_seed.data(), threads);
printDatasetReady(id, ts);
if (m_datasets.size() > 1) {
for (auto const &item : m_datasets) {
if (item.first == id) {
continue;
}
m_threads.emplace_back(copyDataset, item.second, item.first, primary->raw());
}
join();
}
m_ready = true;
}
inline std::pair<uint32_t, uint32_t> hugePages() const
{
auto pages = m_cache->hugePages();
for (auto const &item : m_datasets) {
const auto p = item.second->hugePages(false);
pages.first += p.first;
pages.second += p.second;
}
return pages;
}
private:
static void allocate(RxNUMAStoragePrivate *d_ptr, uint32_t nodeId, bool hugePages)
{
const uint64_t ts = Chrono::steadyMSecs();
if (!bindToNUMANode(nodeId)) {
printSkipped(nodeId, "can't bind memory");
return;
}
auto dataset = new RxDataset(hugePages, false);
if (!dataset->get()) {
printSkipped(nodeId, "failed to allocate dataset");
delete dataset;
return;
}
std::lock_guard<std::mutex> lock(mutex);
d_ptr->m_datasets.insert({ nodeId, dataset });
d_ptr->printAllocStatus(dataset, nodeId, ts);
}
static void allocateCache(RxNUMAStoragePrivate *d_ptr, uint32_t nodeId, bool hugePages)
{
const uint64_t ts = Chrono::steadyMSecs();
bindToNUMANode(nodeId);
auto cache = new RxCache(hugePages);
std::lock_guard<std::mutex> lock(mutex);
d_ptr->m_cache = cache;
d_ptr->printAllocStatus(cache, nodeId, ts);
}
static void copyDataset(RxDataset *dst, uint32_t nodeId, const void *raw)
{
const uint64_t ts = Chrono::steadyMSecs();
dst->setRaw(raw);
printDatasetReady(nodeId, ts);
}
void printAllocStatus(RxDataset *dataset, uint32_t nodeId, uint64_t ts)
{
const auto pages = dataset->hugePages();
const double percent = pages.first == 0 ? 0.0 : static_cast<double>(pages.first) / pages.second * 100.0;
LOG_INFO("%s" CYAN_BOLD("#%u ") GREEN_BOLD("allocated") CYAN_BOLD(" %zu MB") " huge pages %s%3.0f%%" CLEAR BLACK_BOLD(" (%" PRIu64 " ms)"),
rx_tag(),
nodeId,
dataset->size() / oneMiB,
(pages.first == pages.second ? GREEN_BOLD_S : RED_BOLD_S),
percent,
Chrono::steadyMSecs() - ts
);
}
void printAllocStatus(RxCache *cache, uint32_t nodeId, uint64_t ts)
{
const auto pages = cache->hugePages();
const double percent = pages.first == 0 ? 0.0 : static_cast<double>(pages.first) / pages.second * 100.0;
LOG_INFO("%s" CYAN_BOLD("#%u ") GREEN_BOLD("allocated") CYAN_BOLD(" %4zu MB") " huge pages %s%3.0f%%" CLEAR " %sJIT" BLACK_BOLD(" (%" PRIu64 " ms)"),
rx_tag(),
nodeId,
cache->size() / oneMiB,
(pages.first == pages.second ? GREEN_BOLD_S : RED_BOLD_S),
percent,
cache->isJIT() ? GREEN_BOLD_S "+" : RED_BOLD_S "-",
Chrono::steadyMSecs() - ts
);
}
void printAllocStatus(uint64_t ts)
{
size_t memory = m_cache->size();
auto pages = hugePages();
const double percent = pages.first == 0 ? 0.0 : static_cast<double>(pages.first) / pages.second * 100.0;
for (auto const &item : m_datasets) {
memory += item.second->size(false);
}
LOG_INFO("%s" CYAN_BOLD("-- ") GREEN_BOLD("allocated") CYAN_BOLD(" %4zu MB") " huge pages %s%3.0f%% %u/%u" CLEAR BLACK_BOLD(" (%" PRIu64 " ms)"),
rx_tag(),
memory / oneMiB,
(pages.first == pages.second ? GREEN_BOLD_S : (pages.first == 0 ? RED_BOLD_S : YELLOW_BOLD_S)),
percent,
pages.first,
pages.second,
Chrono::steadyMSecs() - ts
);
}
inline void join()
{
for (auto &thread : m_threads) {
thread.join();
}
m_threads.clear();
}
bool m_allocated = false;
bool m_ready = false;
RxCache *m_cache = nullptr;
RxSeed m_seed;
std::map<uint32_t, RxDataset *> m_datasets;
std::vector<std::thread> m_threads;
std::vector<uint32_t> m_nodeset;
};
} // namespace xmrig
xmrig::RxNUMAStorage::RxNUMAStorage(const std::vector<uint32_t> &nodeset) :
d_ptr(new RxNUMAStoragePrivate(nodeset))
{
}
xmrig::RxNUMAStorage::~RxNUMAStorage()
{
delete d_ptr;
}
xmrig::RxDataset *xmrig::RxNUMAStorage::dataset(const Job &job, uint32_t nodeId) const
{
if (!d_ptr->isReady(job)) {
return nullptr;
}
return d_ptr->dataset(nodeId);
}
std::pair<uint32_t, uint32_t> xmrig::RxNUMAStorage::hugePages() const
{
if (!d_ptr->isAllocated()) {
return { 0u, 0u };
}
return d_ptr->hugePages();
}
void xmrig::RxNUMAStorage::init(const RxSeed &seed, uint32_t threads, bool hugePages)
{
d_ptr->setSeed(seed);
if (!d_ptr->isAllocated()) {
d_ptr->createDatasets(hugePages);
}
d_ptr->initDatasets(threads);
}

View file

@ -0,0 +1,66 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_RX_NUMASTORAGE_H
#define XMRIG_RX_NUMASTORAGE_H
#include "backend/common/interfaces/IRxStorage.h"
#include "base/tools/Object.h"
#include <vector>
namespace xmrig
{
class RxNUMAStoragePrivate;
class RxNUMAStorage : public IRxStorage
{
public:
XMRIG_DISABLE_COPY_MOVE(RxNUMAStorage);
RxNUMAStorage(const std::vector<uint32_t> &nodeset);
~RxNUMAStorage() override;
protected:
RxDataset *dataset(const Job &job, uint32_t nodeId) const override;
std::pair<uint32_t, uint32_t> hugePages() const override;
void init(const RxSeed &seed, uint32_t threads, bool hugePages) override;
private:
RxNUMAStoragePrivate *d_ptr;
};
} /* namespace xmrig */
#endif /* XMRIG_RX_NUMASTORAGE_H */

182
src/crypto/rx/RxQueue.cpp Normal file
View file

@ -0,0 +1,182 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "crypto/rx/RxQueue.h"
#include "backend/common/Tags.h"
#include "base/io/log/Log.h"
#include "crypto/rx/RxBasicStorage.h"
#include "base/tools/Handle.h"
#include "backend/common/interfaces/IRxListener.h"
#ifdef XMRIG_FEATURE_HWLOC
# include "crypto/rx/RxNUMAStorage.h"
#endif
xmrig::RxQueue::RxQueue(IRxListener *listener) :
m_listener(listener)
{
m_async = new uv_async_t;
m_async->data = this;
uv_async_init(uv_default_loop(), m_async, [](uv_async_t *handle) { static_cast<RxQueue *>(handle->data)->onReady(); });
m_thread = std::move(std::thread(&RxQueue::backgroundInit, this));
}
xmrig::RxQueue::~RxQueue()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_state = STATE_SHUTDOWN;
lock.unlock();
m_cv.notify_one();
m_thread.join();
delete m_storage;
Handle::close(m_async);
}
bool xmrig::RxQueue::isReady(const Job &job)
{
std::lock_guard<std::mutex> lock(m_mutex);
return isReadyUnsafe(job);
}
xmrig::RxDataset *xmrig::RxQueue::dataset(const Job &job, uint32_t nodeId)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (isReadyUnsafe(job)) {
return m_storage->dataset(job, nodeId);
}
return nullptr;
}
std::pair<uint32_t, uint32_t> xmrig::RxQueue::hugePages()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_storage && m_state == STATE_IDLE ? m_storage->hugePages() : std::pair<uint32_t, uint32_t>(0u, 0u);
}
void xmrig::RxQueue::enqueue(const RxSeed &seed, const std::vector<uint32_t> &nodeset, uint32_t threads, bool hugePages)
{
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_storage) {
# ifdef XMRIG_FEATURE_HWLOC
if (!nodeset.empty()) {
m_storage = new RxNUMAStorage(nodeset);
}
else
# endif
{
m_storage = new RxBasicStorage();
}
}
if (m_state == STATE_PENDING && m_seed == seed) {
return;
}
m_queue.emplace_back(seed, nodeset, threads, hugePages);
m_seed = seed;
m_state = STATE_PENDING;
lock.unlock();
m_cv.notify_one();
}
bool xmrig::RxQueue::isReadyUnsafe(const Job &job) const
{
return m_storage != nullptr && m_state == STATE_IDLE && m_seed == job;
}
void xmrig::RxQueue::backgroundInit()
{
while (m_state != STATE_SHUTDOWN) {
std::unique_lock<std::mutex> lock(m_mutex);
if (m_state == STATE_IDLE) {
m_cv.wait(lock, [this]{ return m_state != STATE_IDLE; });
}
if (m_state != STATE_PENDING) {
continue;
}
const auto item = m_queue.back();
m_queue.clear();
lock.unlock();
LOG_INFO("%s" MAGENTA_BOLD("init dataset%s") " algo " WHITE_BOLD("%s (") CYAN_BOLD("%u") WHITE_BOLD(" threads)") BLACK_BOLD(" seed %s..."),
rx_tag(),
item.nodeset.size() > 1 ? "s" : "",
item.seed.algorithm().shortName(),
item.threads,
Buffer::toHex(item.seed.data().data(), 8).data()
);
m_storage->init(item.seed, item.threads, item.hugePages);
lock = std::move(std::unique_lock<std::mutex>(m_mutex));
if (m_state == STATE_SHUTDOWN || !m_queue.empty()) {
continue;
}
m_state = STATE_IDLE;
uv_async_send(m_async);
}
}
void xmrig::RxQueue::onReady()
{
std::unique_lock<std::mutex> lock(m_mutex);
const bool ready = m_listener && m_state == STATE_IDLE;
lock.unlock();
if (ready) {
m_listener->onDatasetReady();
}
}

108
src/crypto/rx/RxQueue.h Normal file
View file

@ -0,0 +1,108 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_RX_QUEUE_H
#define XMRIG_RX_QUEUE_H
#include "base/tools/Object.h"
#include "crypto/rx/RxSeed.h"
#include <condition_variable>
#include <mutex>
#include <thread>
using uv_async_t = struct uv_async_s;
namespace xmrig
{
class IRxListener;
class IRxStorage;
class RxDataset;
class RxQueueItem
{
public:
RxQueueItem(const RxSeed &seed, const std::vector<uint32_t> &nodeset, uint32_t threads, bool hugePages) :
hugePages(hugePages),
seed(seed),
nodeset(nodeset),
threads(threads)
{}
const bool hugePages;
const RxSeed seed;
const std::vector<uint32_t> nodeset;
const uint32_t threads;
};
class RxQueue
{
public:
XMRIG_DISABLE_COPY_MOVE(RxQueue);
RxQueue(IRxListener *listener);
~RxQueue();
bool isReady(const Job &job);
RxDataset *dataset(const Job &job, uint32_t nodeId);
std::pair<uint32_t, uint32_t> hugePages();
void enqueue(const RxSeed &seed, const std::vector<uint32_t> &nodeset, uint32_t threads, bool hugePages);
private:
enum State {
STATE_IDLE,
STATE_PENDING,
STATE_SHUTDOWN
};
bool isReadyUnsafe(const Job &job) const;
void backgroundInit();
void onReady();
IRxListener *m_listener = nullptr;
IRxStorage *m_storage = nullptr;
RxSeed m_seed;
State m_state = STATE_IDLE;
std::condition_variable m_cv;
std::mutex m_mutex;
std::thread m_thread;
std::vector<RxQueueItem> m_queue;
uv_async_t *m_async = nullptr;
};
} /* namespace xmrig */
#endif /* XMRIG_RX_QUEUE_H */

69
src/crypto/rx/RxSeed.h Normal file
View file

@ -0,0 +1,69 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2019 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_RX_SEED_H
#define XMRIG_RX_SEED_H
#include "base/net/stratum/Job.h"
#include "base/tools/Buffer.h"
namespace xmrig
{
class RxSeed;
class RxSeed
{
public:
RxSeed() = default;
inline RxSeed(const Algorithm &algorithm, const Buffer &seed) : m_algorithm(algorithm), m_data(seed) {}
inline RxSeed(const Job &job) : m_algorithm(job.algorithm()), m_data(job.seed()) {}
inline bool isEqual(const Job &job) const { return m_algorithm == job.algorithm() && m_data == job.seed(); }
inline bool isEqual(const RxSeed &other) const { return m_algorithm == other.m_algorithm && m_data == other.m_data; }
inline const Algorithm &algorithm() const { return m_algorithm; }
inline const Buffer &data() const { return m_data; }
inline bool operator!=(const Job &job) const { return !isEqual(job); }
inline bool operator!=(const RxSeed &other) const { return !isEqual(other); }
inline bool operator==(const Job &job) const { return isEqual(job); }
inline bool operator==(const RxSeed &other) const { return isEqual(other); }
private:
Algorithm m_algorithm;
Buffer m_data;
};
} /* namespace xmrig */
#endif /* XMRIG_RX_CACHE_H */

View file

@ -41,12 +41,14 @@ xmrig::RxVm::RxVm(RxDataset *dataset, uint8_t *scratchpad, bool softAes)
m_flags |= RANDOMX_FLAG_FULL_MEM;
}
if (dataset->cache()->isJIT()) {
if (!dataset->cache() || dataset->cache()->isJIT()) {
m_flags |= RANDOMX_FLAG_JIT;
m_vm = randomx_create_vm(static_cast<randomx_flags>(m_flags), nullptr, dataset->get(), scratchpad);
}
else {
m_vm = randomx_create_vm(static_cast<randomx_flags>(m_flags), dataset->cache()->get(), dataset->get(), scratchpad);
}
}
xmrig::RxVm::~RxVm()