Merge branch 'evo' into beta

This commit is contained in:
XMRig 2019-07-29 08:50:47 +07:00
commit cb814921d4
100 changed files with 33836 additions and 229 deletions

View file

@ -0,0 +1,78 @@
/* 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 Lee Clagett <https://github.com/vtnerd>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2018-2019 tevador <tevador@gmail.com>
* 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/>.
*/
#ifdef XMRIG_FEATURE_HWLOC
# include <hwloc.h>
# include "backend/cpu/platform/HwlocCpuInfo.h"
#endif
#include "base/io/log/Log.h"
#include "crypto/common/VirtualMemory.h"
uint32_t xmrig::VirtualMemory::bindToNUMANode(int64_t affinity)
{
# ifdef XMRIG_FEATURE_HWLOC
if (affinity < 0 || !HwlocCpuInfo::has(HwlocCpuInfo::SET_THISTHREAD_MEMBIND)) {
return 0;
}
hwloc_topology_t topology;
hwloc_topology_init(&topology);
hwloc_topology_load(topology);
const unsigned puId = static_cast<unsigned>(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);
}
hwloc_obj_t node = nullptr;
uint32_t nodeId = 0;
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;
# else
return 0;
# endif
}

View file

@ -52,6 +52,7 @@ public:
return std::pair<size_t, size_t>(isHugePages() ? (align(size()) / 2097152) : 0, align(size()) / 2097152);
}
static uint32_t bindToNUMANode(int64_t affinity);
static void *allocateExecutableMemory(size_t size);
static void *allocateLargePagesMemory(size_t size);
static void flushInstructionCache(void *p, size_t size);

View file

@ -82,12 +82,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define HAVE_SETROUNDMODE_IMPL
#endif
#ifndef HAVE_SETROUNDMODE_IMPL
static void setRoundMode_(uint32_t mode) {
fesetround(mode);
}
#endif
#ifndef HAVE_ROTR64
uint64_t rotr64(uint64_t a, unsigned int b) {
return (a >> b) | (a << (-b & 63));
@ -133,6 +127,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef RANDOMX_DEFAULT_FENV
# ifndef HAVE_SETROUNDMODE_IMPL
static void setRoundMode_(uint32_t mode) {
fesetround(mode);
}
# endif
void rx_reset_float_state() {
setRoundMode_(FE_TONEAREST);
rx_set_double_precision(); //set precision to 53 bits if needed by the platform

View file

@ -25,12 +25,21 @@
*/
#include <map>
#include <thread>
#include <uv.h>
#ifdef XMRIG_FEATURE_HWLOC
# include <hwloc.h>
# include "backend/cpu/platform/HwlocCpuInfo.h"
#endif
#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 "crypto/rx/Rx.h"
@ -41,6 +50,9 @@
namespace xmrig {
static const char *tag = BLUE_BG(WHITE_BOLD_S " rx ") " ";
class RxPrivate
{
public:
@ -52,7 +64,12 @@ public:
inline ~RxPrivate()
{
delete dataset;
for (auto const &item : datasets) {
delete item.second;
}
datasets.clear();
uv_mutex_destroy(&mutex);
}
@ -61,93 +78,161 @@ public:
inline void unlock() { uv_mutex_unlock(&mutex); }
RxDataset *dataset = nullptr;
uint32_t initThreads = std::thread::hardware_concurrency();
static void allocate(RxPrivate *self, uint32_t nodeId)
{
const uint64_t ts = Chrono::steadyMSecs();
# ifdef XMRIG_FEATURE_HWLOC
if (self->numa) {
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"),
tag,
nodeId,
(RxDataset::size() + RxCache::size()) / 1024 / 1024,
RxDataset::size() / 1024 / 1024,
RxCache::size() / 1024 / 1024
);
RxDataset *dataset = new RxDataset(self->hugePages);
self->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);
}
}
bool hugePages = true;
bool numa = true;
std::map<uint32_t, RxDataset *> datasets;
uv_mutex_t mutex;
};
static RxPrivate *d_ptr = new RxPrivate();
static const char *tag = BLUE_BG(" rx ");
} // namespace xmrig
xmrig::RxDataset *xmrig::Rx::dataset()
bool xmrig::Rx::isReady(const Job &job, uint32_t nodeId)
{
d_ptr->lock();
RxDataset *dataset = d_ptr->dataset;
const bool rc = isReady(job.seedHash(), job.algorithm(), d_ptr->numa ? nodeId : 0);
d_ptr->unlock();
return rc;
}
xmrig::RxDataset *xmrig::Rx::dataset(uint32_t nodeId)
{
d_ptr->lock();
RxDataset *dataset = d_ptr->datasets[d_ptr->numa ? nodeId : 0];
d_ptr->unlock();
return dataset;
}
xmrig::RxDataset *xmrig::Rx::dataset(const uint8_t *seed, const Algorithm &algorithm, bool hugePages)
std::pair<size_t, size_t> xmrig::Rx::hugePages()
{
std::pair<size_t, size_t> pages(0, 0);
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);
if (d_ptr->dataset->get() != nullptr) {
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
);
}
else {
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S " failed to allocate RandomX dataset, switching to slow mode", tag);
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;
}
if (!d_ptr->dataset->isReady(seed, algorithm)) {
const uint64_t ts = Chrono::steadyMSecs();
if (d_ptr->dataset->get() != nullptr) {
LOG_INFO("%s" MAGENTA_BOLD(" init dataset") " algo " WHITE_BOLD("%s (") CYAN_BOLD("%u") WHITE_BOLD(" threads)") BLACK_BOLD(" seed %s..."),
tag,
algorithm.shortName(),
d_ptr->initThreads,
Buffer::toHex(seed, 8).data()
);
}
else {
LOG_INFO("%s" MAGENTA_BOLD(" init cache") " algo " WHITE_BOLD("%s") BLACK_BOLD(" seed %s..."),
tag,
algorithm.shortName(),
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;
return pages;
}
void xmrig::Rx::init(const Job &job, int initThreads, bool hugePages, bool numa)
{
if (job.algorithm().family() != Algorithm::RANDOM_X) {
return;
}
d_ptr->lock();
size_t ready = 0;
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()) {
d_ptr->unlock();
return;
}
d_ptr->hugePages = hugePages;
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);
# ifdef XMRIG_FEATURE_HWLOC
if (d_ptr->numa) {
for (uint32_t nodeId : HwlocCpuInfo::nodeIndexes()) {
std::thread thread(initDataset, nodeId, job.seedHash(), job.algorithm(), threads);
thread.detach();
}
}
else
# endif
{
std::thread thread(initDataset, 0, job.seedHash(), job.algorithm(), threads);
thread.detach();
}
d_ptr->unlock();
}
@ -157,3 +242,59 @@ void xmrig::Rx::stop()
d_ptr = nullptr;
}
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)
{
d_ptr->lock();
RxDataset *dataset = d_ptr->datasets[nodeId];
if (!dataset) {
# ifdef XMRIG_FEATURE_HWLOC
if (d_ptr->numa) {
std::thread thread(RxPrivate::allocate, d_ptr, nodeId);
thread.join();
} else
# endif
{
RxPrivate::allocate(d_ptr, 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);
}
d_ptr->unlock();
}

View file

@ -29,6 +29,7 @@
#include <stdint.h>
#include <utility>
namespace xmrig
@ -37,14 +38,21 @@ namespace xmrig
class Algorithm;
class RxDataset;
class Job;
class Rx
{
public:
static RxDataset *dataset();
static RxDataset *dataset(const uint8_t *seed, const Algorithm &algorithm, bool hugePages = true);
static bool isReady(const Job &job, uint32_t nodeId);
static RxDataset *dataset(uint32_t nodeId);
static std::pair<size_t, size_t> hugePages();
static void init(const Job &job, int initThreads, bool hugePages, bool numa);
static void stop();
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

@ -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-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 "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);
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;
}

53
src/crypto/rx/RxConfig.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-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/>.
*/
#ifndef XMRIG_RXCONFIG_H
#define XMRIG_RXCONFIG_H
#include "rapidjson/fwd.h"
namespace xmrig {
class RxConfig
{
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; }
private:
bool m_numa = true;
int m_threads = -1;
};
} /* namespace xmrig */
#endif /* XMRIG_RXCONFIG_H */

View file

@ -112,7 +112,7 @@ bool xmrig::RxDataset::isReady(const void *seed, const Algorithm &algorithm) con
std::pair<size_t, size_t> xmrig::RxDataset::hugePages() const
{
constexpr size_t twoMiB = 2u * 1024u * 1024u;
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;