From 4914fefb1f531faf4fd3374d49885cbf4e5b86dd Mon Sep 17 00:00:00 2001
From: XMRig <support@xmrig.com>
Date: Sun, 25 Oct 2020 16:36:37 +0700
Subject: [PATCH] Added "msr" field for CPU backend.

---
 src/backend/cpu/CpuBackend.cpp |  1 +
 src/crypto/rx/Rx.cpp           | 18 +++++++++++++-----
 src/crypto/rx/Rx.h             |  8 +++++++-
 src/crypto/rx/Rx_linux.cpp     | 14 +++++++++-----
 src/crypto/rx/Rx_win.cpp       | 13 ++++++++-----
 5 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/src/backend/cpu/CpuBackend.cpp b/src/backend/cpu/CpuBackend.cpp
index 8e13a9a43..ecce64fe1 100644
--- a/src/backend/cpu/CpuBackend.cpp
+++ b/src/backend/cpu/CpuBackend.cpp
@@ -430,6 +430,7 @@ rapidjson::Value xmrig::CpuBackend::toJSON(rapidjson::Document &doc) const
     out.AddMember("profile",    profileName().toJSON(), allocator);
     out.AddMember("hw-aes",     cpu.isHwAES(), allocator);
     out.AddMember("priority",   cpu.priority(), allocator);
+    out.AddMember("msr",        Rx::isMSR(), allocator);
 
 #   ifdef XMRIG_FEATURE_ASM
     const Assembly assembly = Cpu::assembly(cpu.assembly());
diff --git a/src/crypto/rx/Rx.cpp b/src/crypto/rx/Rx.cpp
index 66725f60c..01a002147 100644
--- a/src/crypto/rx/Rx.cpp
+++ b/src/crypto/rx/Rx.cpp
@@ -42,6 +42,7 @@ class RxPrivate;
 
 static bool osInitialized   = false;
 static bool msrInitialized  = false;
+static bool msrEnabled      = false;
 static RxPrivate *d_ptr     = nullptr;
 
 
@@ -93,7 +94,8 @@ bool xmrig::Rx::init(const T &seed, const RxConfig &config, const CpuConfig &cpu
     if (seed.algorithm().family() != Algorithm::RANDOM_X) {
         if (msrInitialized) {
             msrDestroy();
-            msrInitialized = false;
+            msrInitialized  = false;
+            msrEnabled      = false;
         }
 
         return true;
@@ -107,8 +109,8 @@ bool xmrig::Rx::init(const T &seed, const RxConfig &config, const CpuConfig &cpu
     }
 
     if (!msrInitialized) {
-        msrInit(config, cpu.threads().get(seed.algorithm()).data());
-        msrInitialized = true;
+        msrEnabled      = msrInit(config, cpu.threads().get(seed.algorithm()).data());
+        msrInitialized  = true;
     }
 
     if (!osInitialized) {
@@ -132,9 +134,15 @@ bool xmrig::Rx::isReady(const T &seed)
 }
 
 
-#ifndef XMRIG_FEATURE_MSR
-void xmrig::Rx::msrInit(const RxConfig &, const std::vector<CpuThread> &)
+#ifdef XMRIG_FEATURE_MSR
+bool xmrig::Rx::isMSR()
 {
+    return msrEnabled;
+}
+#else
+bool xmrig::Rx::msrInit(const RxConfig &, const std::vector<CpuThread> &)
+{
+    return false;
 }
 
 
diff --git a/src/crypto/rx/Rx.h b/src/crypto/rx/Rx.h
index 71a9fb13e..1c452e8bc 100644
--- a/src/crypto/rx/Rx.h
+++ b/src/crypto/rx/Rx.h
@@ -63,8 +63,14 @@ public:
     static void setMainLoopBounds(const std::pair<const void*, const void*>& bounds);
 #   endif
 
+#   ifdef XMRIG_FEATURE_MSR
+    static bool isMSR();
+#   else
+    static constexpr bool isMSR()   { return false; }
+#   endif
+
 private:
-    static void msrInit(const RxConfig &config, const std::vector<CpuThread>& threads);
+    static bool msrInit(const RxConfig &config, const std::vector<CpuThread>& threads);
     static void msrDestroy();
     static void setupMainLoopExceptionFrame();
 };
diff --git a/src/crypto/rx/Rx_linux.cpp b/src/crypto/rx/Rx_linux.cpp
index 940360467..94a0ad326 100644
--- a/src/crypto/rx/Rx_linux.cpp
+++ b/src/crypto/rx/Rx_linux.cpp
@@ -272,21 +272,25 @@ void Rx::setMainLoopBounds(const std::pair<const void*, const void*>& bounds)
 } // namespace xmrig
 
 
-void xmrig::Rx::msrInit(const RxConfig &config, const std::vector<CpuThread>& threads)
+bool xmrig::Rx::msrInit(const RxConfig &config, const std::vector<CpuThread> &threads)
 {
     const auto &preset = config.msrPreset();
     if (preset.empty()) {
-        return;
+        return false;
     }
 
     const uint64_t ts = Chrono::steadyMSecs();
 
     if (wrmsr(preset, threads, config.cacheQoS(), config.rdmsr())) {
         LOG_NOTICE(CLEAR "%s" GREEN_BOLD_S "register values for \"%s\" preset has been set successfully" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, config.msrPresetName(), Chrono::steadyMSecs() - ts);
+
+        return true;
     }
-    else {
-        LOG_ERR(CLEAR "%s" RED_BOLD_S "FAILED TO APPLY MSR MOD, HASHRATE WILL BE LOW", tag);
-    }
+
+
+    LOG_ERR(CLEAR "%s" RED_BOLD_S "FAILED TO APPLY MSR MOD, HASHRATE WILL BE LOW", tag);
+
+    return false;
 }
 
 
diff --git a/src/crypto/rx/Rx_win.cpp b/src/crypto/rx/Rx_win.cpp
index 5131c1b5f..560c00519 100644
--- a/src/crypto/rx/Rx_win.cpp
+++ b/src/crypto/rx/Rx_win.cpp
@@ -395,21 +395,24 @@ void Rx::setMainLoopBounds(const std::pair<const void*, const void*>& bounds)
 } // namespace xmrig
 
 
-void xmrig::Rx::msrInit(const RxConfig &config, const std::vector<CpuThread>& threads)
+bool xmrig::Rx::msrInit(const RxConfig &config, const std::vector<CpuThread>& threads)
 {
     const auto &preset = config.msrPreset();
     if (preset.empty()) {
-        return;
+        return false;
     }
 
     const uint64_t ts = Chrono::steadyMSecs();
 
     if (wrmsr(preset, threads, config.cacheQoS(), config.rdmsr())) {
         LOG_NOTICE(CLEAR "%s" GREEN_BOLD_S "register values for \"%s\" preset has been set successfully" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, config.msrPresetName(), Chrono::steadyMSecs() - ts);
+
+        return true;
     }
-    else {
-        LOG_ERR(CLEAR "%s" RED_BOLD_S "FAILED TO APPLY MSR MOD, HASHRATE WILL BE LOW", tag);
-    }
+
+    LOG_ERR(CLEAR "%s" RED_BOLD_S "FAILED TO APPLY MSR MOD, HASHRATE WILL BE LOW", tag);
+
+    return false;
 }