/* XMRig * Copyright 2010 Jeff Garzik * Copyright 2012-2014 pooler * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2019 XMR-Stak , * Copyright 2018 Lee Clagett * Copyright 2018-2019 tevador * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "crypto/rx/Rx.h" #include "backend/common/interfaces/IRxListener.h" #include "backend/common/interfaces/IRxStorage.h" #include "backend/common/Tags.h" #include "backend/cpu/Cpu.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/RxBasicStorage.h" #include "crypto/rx/RxCache.h" #include "crypto/rx/RxConfig.h" #include "crypto/rx/RxDataset.h" #include "crypto/rx/RxSeed.h" #ifdef XMRIG_FEATURE_HWLOC # include "crypto/rx/RxNUMAStorage.h" #endif #include #include #include #include #include namespace xmrig { class RxPrivate; static const char *tag = BLUE_BG(WHITE_BOLD_S " rx ") " "; static RxPrivate *d_ptr = nullptr; static std::mutex mutex; class RxPrivate { public: XMRIG_DISABLE_COPY_MOVE(RxPrivate) inline RxPrivate() : m_pending(0) { m_async = new uv_async_t; m_async->data = this; uv_async_init(uv_default_loop(), m_async, [](uv_async_t *handle) { static_cast(handle->data)->onReady(); }); } inline ~RxPrivate() { m_pending = std::numeric_limits::max(); std::lock_guard lock(mutex); Handle::close(m_async); delete m_storage; } inline bool isReady(const Job &job) const { return pending() == 0 && m_seed == job; } inline RxDataset *dataset(const Job &job, uint32_t nodeId) { return m_storage ? m_storage->dataset(job, nodeId) : nullptr; } inline std::pair hugePages() { return m_storage ? m_storage->hugePages() : std::pair(0u, 0u); } inline uint64_t pending() const { return m_pending.load(std::memory_order_relaxed); } inline void asyncSend() { --m_pending; if (pending() == 0) { uv_async_send(m_async); } } inline IRxStorage *storage(const std::vector &nodeset) { if (!m_storage) { # ifdef XMRIG_FEATURE_HWLOC if (!nodeset.empty()) { m_storage = new RxNUMAStorage(nodeset); } else # endif { m_storage = new RxBasicStorage(); } } return m_storage; } static void initDataset(const RxSeed &seed, const std::vector &nodeset, uint32_t threads, bool hugePages) { std::lock_guard lock(mutex); if (d_ptr->pending() > std::numeric_limits::max()) { return; } LOG_INFO("%s" MAGENTA_BOLD("init dataset%s") " algo " WHITE_BOLD("%s (") CYAN_BOLD("%u") WHITE_BOLD(" threads)") BLACK_BOLD(" seed %s..."), tag, nodeset.size() > 1 ? "s" : "", seed.algorithm().shortName(), threads, Buffer::toHex(seed.data().data(), 8).data() ); d_ptr->storage(nodeset)->init(seed, threads, hugePages); d_ptr->asyncSend(); } inline void setState(const Job &job, IRxListener *listener) { m_listener = listener; m_seed = job; ++m_pending; } private: inline void onReady() { if (m_listener && pending() == 0) { m_listener->onDatasetReady(); } } IRxListener *m_listener = nullptr; IRxStorage *m_storage = nullptr; RxSeed m_seed; std::atomic m_pending; uv_async_t *m_async = nullptr; }; } // namespace xmrig const char *xmrig::rx_tag() { return tag; } bool xmrig::Rx::init(const Job &job, const RxConfig &config, bool hugePages, IRxListener *listener) { if (job.algorithm().family() != Algorithm::RANDOM_X) { return true; } if (d_ptr->isReady(job)) { return true; } d_ptr->setState(job, listener); std::thread thread(RxPrivate::initDataset, job, config.nodeset(), config.threads(), hugePages); thread.detach(); return false; } bool xmrig::Rx::isReady(const Job &job) { return d_ptr->isReady(job); } xmrig::RxDataset *xmrig::Rx::dataset(const Job &job, uint32_t nodeId) { std::lock_guard lock(mutex); return d_ptr->dataset(job, nodeId); } std::pair xmrig::Rx::hugePages() { std::lock_guard lock(mutex); return d_ptr->hugePages(); } void xmrig::Rx::destroy() { delete d_ptr; d_ptr = nullptr; } void xmrig::Rx::init() { d_ptr = new RxPrivate(); }