From a84b45b1bb46b829e2caeeb14ae2897e5cd85fec Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 4 Sep 2020 16:16:07 +0200 Subject: [PATCH] RandomX: added parameter for scratchpad prefetch mode `scratchpad_prefetch_mode` can have 4 values: 0: off 1: use `prefetcht0` instruction (default, same as previous XMRig versions) 2: use `prefetchnta` instruction (faster on Coffee Lake and a few other CPUs) 3: use `mov` instruction --- src/config.json | 3 ++- src/core/config/Config_default.h | 7 +++++- src/crypto/randomx/randomx.cpp | 37 ++++++++++++++++++++++++++++++++ src/crypto/randomx/randomx.h | 2 ++ src/crypto/rx/Rx.cpp | 3 +++ src/crypto/rx/RxConfig.cpp | 9 ++++++++ src/crypto/rx/RxConfig.h | 12 +++++++++++ 7 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/config.json b/src/config.json index f3f282679..c2bac8614 100644 --- a/src/config.json +++ b/src/config.json @@ -21,7 +21,8 @@ "rdmsr": true, "wrmsr": true, "cache_qos": false, - "numa": true + "numa": true, + "scratchpad_prefetch_mode": 1 }, "cpu": { "enabled": true, diff --git a/src/core/config/Config_default.h b/src/core/config/Config_default.h index dd01fa12b..7e4ed2707 100644 --- a/src/core/config/Config_default.h +++ b/src/core/config/Config_default.h @@ -51,7 +51,12 @@ R"===( "randomx": { "init": -1, "mode": "auto", - "numa": true + "1gb-pages": false, + "rdmsr": true, + "wrmsr": true, + "cache_qos": false, + "numa": true, + "scratchpad_prefetch_mode": 1 }, "cpu": { "enabled": true, diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp index fdd7bd067..8a6053638 100644 --- a/src/crypto/randomx/randomx.cpp +++ b/src/crypto/randomx/randomx.cpp @@ -211,6 +211,13 @@ RandomX_ConfigurationBase::RandomX_ConfigurationBase() static uint32_t Log2(size_t value) { return (value > 1) ? (Log2(value / 2) + 1) : 0; } #endif +static int scratchpadPrefetchMode = 1; + +void randomx_set_scratchpad_prefetch_mode(int mode) +{ + scratchpadPrefetchMode = mode; +} + void RandomX_ConfigurationBase::Apply() { const uint32_t ScratchpadL1Mask_Calculated = (ScratchpadL1_Size / sizeof(uint64_t) - 1) * 8; @@ -240,6 +247,36 @@ void RandomX_ConfigurationBase::Apply() *(uint32_t*)(codePrefetchScratchpadTweaked + 4) = ScratchpadL3Mask64_Calculated; *(uint32_t*)(codePrefetchScratchpadTweaked + 18) = ScratchpadL3Mask64_Calculated; + // Apply scratchpad prefetch mode + { + uint32_t* a = (uint32_t*)(codePrefetchScratchpadTweaked + 8); + uint32_t* b = (uint32_t*)(codePrefetchScratchpadTweaked + 22); + + switch (scratchpadPrefetchMode) + { + case 0: + *a = 0x00401F0FUL; // 4-byte nop + *b = 0x00401F0FUL; // 4-byte nop + break; + + case 1: + default: + *a = 0x060C180FUL; // prefetcht0 [rsi+rax] + *b = 0x160C180FUL; // prefetcht0 [rsi+rdx] + break; + + case 2: + *a = 0x0604180FUL; // prefetchnta [rsi+rax] + *b = 0x1604180FUL; // prefetchnta [rsi+rdx] + break; + + case 3: + *a = 0x060C8B48UL; // mov rcx, [rsi+rax] + *b = 0x160C8B48UL; // mov rcx, [rsi+rdx] + break; + } + } + #define JIT_HANDLE(x, prev) randomx::JitCompilerX86::engine[k] = &randomx::JitCompilerX86::h_##x #elif defined(XMRIG_ARMv8) diff --git a/src/crypto/randomx/randomx.h b/src/crypto/randomx/randomx.h index ce3f6ebb3..3379e2242 100644 --- a/src/crypto/randomx/randomx.h +++ b/src/crypto/randomx/randomx.h @@ -200,6 +200,8 @@ void randomx_apply_config(const T& config) RandomX_CurrentConfig.Apply(); } +void randomx_set_scratchpad_prefetch_mode(int mode); + #if defined(__cplusplus) extern "C" { #endif diff --git a/src/crypto/rx/Rx.cpp b/src/crypto/rx/Rx.cpp index 4c98d85ad..a6a1f5c95 100644 --- a/src/crypto/rx/Rx.cpp +++ b/src/crypto/rx/Rx.cpp @@ -32,6 +32,7 @@ #include "base/io/log/Log.h" #include "crypto/rx/RxConfig.h" #include "crypto/rx/RxQueue.h" +#include "crypto/randomx/randomx.h" namespace xmrig { @@ -99,6 +100,8 @@ bool xmrig::Rx::init(const T &seed, const RxConfig &config, const CpuConfig &cpu return true; } + randomx_set_scratchpad_prefetch_mode(config.scratchpadPrefetchMode()); + if (isReady(seed)) { return true; } diff --git a/src/crypto/rx/RxConfig.cpp b/src/crypto/rx/RxConfig.cpp index f1cd09e97..55dae35d6 100644 --- a/src/crypto/rx/RxConfig.cpp +++ b/src/crypto/rx/RxConfig.cpp @@ -57,6 +57,8 @@ static const char *kCacheQoS = "cache_qos"; static const char *kNUMA = "numa"; #endif +static const char *kScratchpadPrefetchMode = "scratchpad_prefetch_mode"; + static const std::array modeNames = { "auto", "fast", "light" }; @@ -118,6 +120,11 @@ bool xmrig::RxConfig::read(const rapidjson::Value &value) } # endif + const int mode = Json::getInt(value, kScratchpadPrefetchMode, static_cast(m_scratchpadPrefetchMode)); + if ((mode >= ScratchpadPrefetchOff) && (mode < ScratchpadPrefetchMax)) { + m_scratchpadPrefetchMode = static_cast(mode); + } + return true; } @@ -171,6 +178,8 @@ rapidjson::Value xmrig::RxConfig::toJSON(rapidjson::Document &doc) const } # endif + obj.AddMember(StringRef(kScratchpadPrefetchMode), static_cast(m_scratchpadPrefetchMode), allocator); + return obj; } diff --git a/src/crypto/rx/RxConfig.h b/src/crypto/rx/RxConfig.h index e3e06326e..c8d715d07 100644 --- a/src/crypto/rx/RxConfig.h +++ b/src/crypto/rx/RxConfig.h @@ -50,6 +50,14 @@ public: ModeMax }; + enum ScratchpadPrefetchMode : uint32_t { + ScratchpadPrefetchOff, + ScratchpadPrefetchT0, + ScratchpadPrefetchNTA, + ScratchpadPrefetchMov, + ScratchpadPrefetchMax, + }; + bool read(const rapidjson::Value &value); rapidjson::Value toJSON(rapidjson::Document &doc) const; @@ -68,6 +76,8 @@ public: inline bool cacheQoS() const { return m_cacheQoS; } inline Mode mode() const { return m_mode; } + inline ScratchpadPrefetchMode scratchpadPrefetchMode() const { return m_scratchpadPrefetchMode; } + # ifdef XMRIG_FEATURE_MSR const char *msrPresetName() const; const MsrItems &msrPreset() const; @@ -94,6 +104,8 @@ private: int m_threads = -1; Mode m_mode = AutoMode; + ScratchpadPrefetchMode m_scratchpadPrefetchMode = ScratchpadPrefetchT0; + # ifdef XMRIG_FEATURE_HWLOC std::vector m_nodeset; # endif