Added classes Rx, RxAlgo, RxCache, RxDataset.

This commit is contained in:
XMRig 2019-07-10 01:53:05 +07:00
parent ea1149a971
commit f42adafee0
18 changed files with 704 additions and 144 deletions

View file

@ -32,6 +32,7 @@
#include "crypto/cn/CnAlgo.h"
#include "crypto/common/Algorithm.h"
#include "crypto/rx/RxAlgo.h"
#include "rapidjson/document.h"
@ -138,12 +139,12 @@ size_t xmrig::Algorithm::memory() const
}
# ifdef XMRIG_ALGO_RANDOMX
if (m_id == RX_WOW) {
return 0x100000;
if (f == RANDOM_X) {
return RxAlgo::l3(m_id);
}
# endif
return 0x200000;
return 0;
}

View file

@ -44,6 +44,8 @@ public:
static void freeLargePagesMemory(void *p, size_t size);
static void protectExecutableMemory(void *p, size_t size);
static void unprotectExecutableMemory(void *p, size_t size);
static inline constexpr size_t align(size_t pos, size_t align = 2097152) { return ((pos - 1) / align + 1) * align; }
};

View file

@ -32,15 +32,6 @@
#include "crypto/common/VirtualMemory.h"
namespace xmrig {
constexpr size_t align(size_t pos, size_t align) {
return ((pos - 1) / align + 1) * align;
}
}
void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size)
{
return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);

134
src/crypto/rx/Rx.cpp Normal file
View file

@ -0,0 +1,134 @@
/* 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 <thread>
#include <uv.h>
#include "backend/cpu/Cpu.h"
#include "base/io/log/Log.h"
#include "base/tools/Buffer.h"
#include "base/tools/Chrono.h"
#include "crypto/rx/Rx.h"
#include "crypto/rx/RxCache.h"
#include "crypto/rx/RxDataset.h"
namespace xmrig {
class RxPrivate
{
public:
inline RxPrivate()
{
uv_mutex_init(&mutex);
}
inline ~RxPrivate()
{
delete dataset;
uv_mutex_destroy(&mutex);
}
inline void lock() { uv_mutex_lock(&mutex); }
inline void unlock() { uv_mutex_unlock(&mutex); }
RxDataset *dataset = nullptr;
uint32_t initThreads = std::thread::hardware_concurrency();
uv_mutex_t mutex;
};
static RxPrivate *d_ptr = new RxPrivate();
static const char *tag = BLUE_BG(" rx ");
} // namespace xmrig
xmrig::RxDataset *xmrig::Rx::dataset(const uint8_t *seed, const Algorithm &algorithm, bool hugePages)
{
d_ptr->lock();
if (!d_ptr->dataset) {
const uint64_t ts = Chrono::steadyMSecs();
LOG_INFO("%s" MAGENTA_BOLD(" allocate") CYAN_BOLD(" %zu MiB") BLACK_BOLD(" (%zu+%zu) for RandomX dataset & cache"),
tag,
(RxDataset::size() + RxCache::size()) / 1024 / 1024,
RxDataset::size() / 1024 / 1024,
RxCache::size() / 1024 / 1024
);
d_ptr->dataset = new RxDataset(hugePages);
const auto hugePages = d_ptr->dataset->hugePages();
const double percent = hugePages.first == 0 ? 0.0 : static_cast<double>(hugePages.first) / hugePages.second * 100.0;
LOG_INFO("%s" GREEN(" allocate done") " huge pages %s%u/%u %1.0f%%" CLEAR " %sJIT" BLACK_BOLD(" (%" PRIu64 " ms)"),
tag,
(hugePages.first == hugePages.second ? GREEN_BOLD_S : (hugePages.first == 0 ? RED_BOLD_S : YELLOW_BOLD_S)),
hugePages.first,
hugePages.second,
percent,
d_ptr->dataset->cache()->isJIT() ? GREEN_BOLD_S "+" : RED_BOLD_S "-",
Chrono::steadyMSecs() - ts
);
}
if (!d_ptr->dataset->isReady(seed, algorithm)) {
const uint64_t ts = Chrono::steadyMSecs();
LOG_INFO("%s" MAGENTA_BOLD(" init dataset") " algo " WHITE_BOLD("%s") " threads " WHITE_BOLD("%u") BLACK_BOLD(" seed %s..."),
tag,
algorithm.shortName(),
d_ptr->initThreads,
Buffer::toHex(seed, 8).data()
);
d_ptr->dataset->init(seed, algorithm, d_ptr->initThreads);
LOG_INFO("%s" GREEN(" init done") BLACK_BOLD(" (%" PRIu64 " ms)"), tag, Chrono::steadyMSecs() - ts);
}
RxDataset *dataset = d_ptr->dataset;
d_ptr->unlock();
return dataset;
}
void xmrig::Rx::stop()
{
delete d_ptr;
d_ptr = nullptr;
}

53
src/crypto/rx/Rx.h Normal file
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 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_H
#define XMRIG_RX_H
#include <stdint.h>
namespace xmrig
{
class Algorithm;
class RxDataset;
class Rx
{
public:
static RxDataset *dataset(const uint8_t *seed, const Algorithm &algorithm, bool hugePages = true);
static void stop();
};
} /* namespace xmrig */
#endif /* XMRIG_RX_H */

69
src/crypto/rx/RxAlgo.cpp 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/>.
*/
#include "crypto/randomx/randomx.h"
#include "crypto/rx/RxAlgo.h"
xmrig::Algorithm::Id xmrig::RxAlgo::apply(Algorithm::Id algorithm)
{
switch (algorithm) {
case Algorithm::RX_WOW:
randomx_apply_config(RandomX_WowneroConfig);
break;
case Algorithm::RX_LOKI:
randomx_apply_config(RandomX_LokiConfig);
break;
default:
randomx_apply_config(RandomX_MoneroConfig);
break;
}
return algorithm;
}
size_t xmrig::RxAlgo::l3(Algorithm::Id algorithm)
{
switch (algorithm) {
case Algorithm::RX_0:
return RandomX_MoneroConfig.ScratchpadL3_Size;
case Algorithm::RX_WOW:
return RandomX_WowneroConfig.ScratchpadL3_Size;
case Algorithm::RX_LOKI:
return RandomX_LokiConfig.ScratchpadL3_Size;
default:
break;
}
return 0;
}

56
src/crypto/rx/RxAlgo.h Normal file
View file

@ -0,0 +1,56 @@
/* 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_ALGO_H
#define XMRIG_RX_ALGO_H
#include <stddef.h>
#include <stdint.h>
#include "crypto/common/Algorithm.h"
struct RandomX_ConfigurationBase;
namespace xmrig
{
class RxAlgo
{
public:
static Algorithm::Id apply(Algorithm::Id algorithm);
static size_t l3(Algorithm::Id algorithm);
};
} /* namespace xmrig */
#endif /* XMRIG_RX_ALGO_H */

81
src/crypto/rx/RxCache.cpp Normal file
View file

@ -0,0 +1,81 @@
/* 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/randomx/randomx.h"
#include "crypto/rx/RxCache.h"
static_assert(RANDOMX_FLAG_JIT == 8, "RANDOMX_FLAG_JIT flag mismatch");
static_assert(RANDOMX_FLAG_LARGE_PAGES == 1, "RANDOMX_FLAG_LARGE_PAGES flag mismatch");
xmrig::RxCache::RxCache(bool hugePages) :
m_seed()
{
if (hugePages) {
m_flags = RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES;
m_cache = randomx_alloc_cache(static_cast<randomx_flags>(m_flags));
}
if (!m_cache) {
m_flags = RANDOMX_FLAG_JIT;
m_cache = randomx_alloc_cache(static_cast<randomx_flags>(m_flags));
}
if (!m_cache) {
m_flags = RANDOMX_FLAG_DEFAULT;
m_cache = randomx_alloc_cache(static_cast<randomx_flags>(m_flags));
}
}
xmrig::RxCache::~RxCache()
{
if (m_cache) {
randomx_release_cache(m_cache);
}
}
bool xmrig::RxCache::init(const void *seed)
{
if (isReady(seed)) {
return false;
}
memcpy(m_seed, seed, sizeof(m_seed));
randomx_init_cache(m_cache, m_seed, sizeof(m_seed));
return true;
}
bool xmrig::RxCache::isReady(const void *seed) const
{
return memcmp(m_seed, seed, sizeof(m_seed)) == 0;
}

70
src/crypto/rx/RxCache.h Normal file
View file

@ -0,0 +1,70 @@
/* 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_CACHE_H
#define XMRIG_RX_CACHE_H
#include <stdint.h>
#include "crypto/randomx/configuration.h"
struct randomx_cache;
namespace xmrig
{
class RxCache
{
public:
RxCache(bool hugePages = true);
~RxCache();
inline bool isHugePages() const { return m_flags & 1; }
inline bool isJIT() const { return m_flags & 8; }
inline const uint8_t *seed() const { return m_seed; }
inline randomx_cache *get() const { return m_cache; }
bool init(const void *seed);
bool isReady(const void *seed) const;
static inline constexpr size_t size() { return RANDOMX_CACHE_MAX_SIZE; }
private:
int m_flags = 0;
randomx_cache *m_cache = nullptr;
uint8_t m_seed[32];
};
} /* namespace xmrig */
#endif /* XMRIG_RX_CACHE_H */

124
src/crypto/rx/RxDataset.cpp Normal file
View file

@ -0,0 +1,124 @@
/* 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 <thread>
#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"
static_assert(RANDOMX_FLAG_LARGE_PAGES == 1, "RANDOMX_FLAG_LARGE_PAGES flag mismatch");
xmrig::RxDataset::RxDataset(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));
}
m_cache = new RxCache(hugePages);
}
xmrig::RxDataset::~RxDataset()
{
if (m_dataset) {
randomx_release_dataset(m_dataset);
}
delete m_cache;
}
bool xmrig::RxDataset::init(const void *seed, const Algorithm &algorithm, uint32_t numThreads)
{
if (isReady(seed, algorithm)) {
return false;
}
if (m_algorithm != algorithm) {
m_algorithm = RxAlgo::apply(algorithm);
}
cache()->init(seed);
const uint32_t datasetItemCount = randomx_dataset_item_count();
if (numThreads > 1) {
std::vector<std::thread> threads;
threads.reserve(numThreads);
for (uint32_t i = 0; i < numThreads; ++i) {
const uint32_t a = (datasetItemCount * i) / numThreads;
const uint32_t b = (datasetItemCount * (i + 1)) / numThreads;
threads.emplace_back(randomx_init_dataset, m_dataset, m_cache->get(), a, b - a);
}
for (uint32_t i = 0; i < numThreads; ++i) {
threads[i].join();
}
}
else {
randomx_init_dataset(m_dataset, m_cache->get(), 0, datasetItemCount);
}
return true;
}
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
{
constexpr size_t twoMiB = 2u * 1024u * 1024u;
constexpr const size_t total = (VirtualMemory::align(size(), twoMiB) + VirtualMemory::align(RxCache::size(), twoMiB)) / twoMiB;
size_t count = 0;
if (isHugePages()) {
count += VirtualMemory::align(size(), twoMiB) / twoMiB;
}
if (m_cache->isHugePages()) {
count += VirtualMemory::align(RxCache::size(), twoMiB) / twoMiB;
}
return std::pair<size_t, size_t>(count, total);
}

72
src/crypto/rx/RxDataset.h Normal file
View file

@ -0,0 +1,72 @@
/* 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_DATASET_H
#define XMRIG_RX_DATASET_H
#include "crypto/common/Algorithm.h"
#include "crypto/randomx/configuration.h"
struct randomx_dataset;
namespace xmrig
{
class RxCache;
class RxDataset
{
public:
RxDataset(bool hugePages = true);
~RxDataset();
inline bool isHugePages() const { return m_flags & 1; }
inline randomx_dataset *get() const { return m_dataset; }
inline RxCache *cache() const { return m_cache; }
bool init(const void *seed, const Algorithm &algorithm, uint32_t numThreads);
bool isReady(const void *seed, const Algorithm &algorithm) const;
std::pair<size_t, size_t> hugePages() const;
static inline constexpr size_t size() { return RANDOMX_DATASET_MAX_SIZE; }
private:
Algorithm m_algorithm;
int m_flags = 0;
randomx_dataset *m_dataset = nullptr;
RxCache *m_cache = nullptr;
};
} /* namespace xmrig */
#endif /* XMRIG_RX_DATASET_H */