Added RandomX option "rdmsr" and save/restore MSR registers on Windows.

This commit is contained in:
XMRig 2019-12-17 14:45:01 +07:00
parent 8bef964f68
commit d2d501c821
No known key found for this signature in database
GPG key ID: 446A53638BE94409
4 changed files with 56 additions and 4 deletions

View file

@ -17,6 +17,7 @@
"init": -1,
"mode": "auto",
"1gb-pages": false,
"rdmsr": true,
"wrmsr": true,
"numa": true
},

View file

@ -49,6 +49,7 @@ namespace xmrig {
static const char *kInit = "init";
static const char *kMode = "mode";
static const char *kOneGbPages = "1gb-pages";
static const char *kRdmsr = "rdmsr";
static const char *kWrmsr = "wrmsr";
#ifdef XMRIG_FEATURE_HWLOC
@ -82,6 +83,7 @@ bool xmrig::RxConfig::read(const rapidjson::Value &value)
if (value.IsObject()) {
m_threads = Json::getInt(value, kInit, m_threads);
m_mode = readMode(Json::getValue(value, kMode));
m_rdmsr = Json::getBool(value, kRdmsr, m_rdmsr);
# ifdef XMRIG_FEATURE_MSR
readMSR(Json::getValue(value, kWrmsr));
@ -129,6 +131,7 @@ rapidjson::Value xmrig::RxConfig::toJSON(rapidjson::Document &doc) const
obj.AddMember(StringRef(kInit), m_threads, allocator);
obj.AddMember(StringRef(kMode), StringRef(modeName()), allocator);
obj.AddMember(StringRef(kOneGbPages), m_oneGbPages, allocator);
obj.AddMember(StringRef(kRdmsr), m_rdmsr, allocator);
# ifdef XMRIG_FEATURE_MSR
if (!m_msrPreset.empty()) {

View file

@ -63,6 +63,7 @@ public:
uint32_t threads(uint32_t limit = 100) const;
inline bool isOneGbPages() const { return m_oneGbPages; }
inline bool rdmsr() const { return m_rdmsr; }
inline bool wrmsr() const { return m_wrmsr; }
inline Mode mode() const { return m_mode; }
@ -86,6 +87,7 @@ private:
bool m_numa = true;
bool m_oneGbPages = false;
bool m_rdmsr = true;
int m_threads = -1;
Mode m_mode = AutoMode;

View file

@ -48,8 +48,9 @@
namespace xmrig {
static const char *tag = YELLOW_BG_BOLD(WHITE_BOLD_S " msr ") " ";
static bool reuseDriver = false;
static const char *tag = YELLOW_BG_BOLD(WHITE_BOLD_S " msr ") " ";
static MsrItems savedState;
static SC_HANDLE hManager;
@ -177,10 +178,12 @@ static HANDLE wrmsr_install_driver()
}
#define IOCTL_READ_MSR CTL_CODE(40000, 0x821, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_WRITE_MSR CTL_CODE(40000, 0x822, METHOD_BUFFERED, FILE_ANY_ACCESS)
static bool wrmsr(HANDLE driver, uint32_t reg, uint64_t value) {
static bool wrmsr(HANDLE driver, uint32_t reg, uint64_t value)
{
struct {
uint32_t reg = 0;
uint32_t value[2]{};
@ -204,7 +207,32 @@ static bool wrmsr(HANDLE driver, uint32_t reg, uint64_t value) {
}
static bool wrmsr(const MsrItems &preset)
static bool rdmsr(HANDLE driver, uint32_t reg, uint64_t &value)
{
DWORD size = 0;
if (!DeviceIoControl(driver, IOCTL_READ_MSR, &reg, sizeof(reg), &value, sizeof(value), &size, nullptr)) {
LOG_WARN(CLEAR "%s" YELLOW_BOLD_S "cannot read MSR 0x%08" PRIx32, tag, reg);
return false;
}
return true;
}
static MsrItem rdmsr(HANDLE driver, uint32_t reg)
{
uint64_t value = 0;
if (!rdmsr(driver, reg, value)) {
return {};
}
return { reg, value };
}
static bool wrmsr(const MsrItems &preset, bool save)
{
bool success = true;
@ -219,6 +247,15 @@ static bool wrmsr(const MsrItems &preset)
return false;
}
if (save) {
for (const auto &i : preset) {
auto item = rdmsr(driver, i.reg());
if (item.isValid()) {
savedState.emplace_back(item);
}
}
}
std::thread wrmsr_thread([driver, &preset, &success]() {
for (uint32_t i = 0, n = Cpu::info()->threads(); i < n; ++i) {
if (!Platform::setThreadAffinity(i)) {
@ -258,7 +295,7 @@ void xmrig::Rx::msrInit(const RxConfig &config)
const uint64_t ts = Chrono::steadyMSecs();
if (wrmsr(preset)) {
if (wrmsr(preset, 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);
}
}
@ -266,4 +303,13 @@ void xmrig::Rx::msrInit(const RxConfig &config)
void xmrig::Rx::msrDestroy()
{
if (savedState.empty()) {
return;
}
const uint64_t ts = Chrono::steadyMSecs();
if (!wrmsr(savedState, false)) {
LOG_ERR(CLEAR "%s" RED_BOLD_S "failed to restore initial state" BLACK_BOLD(" (%" PRIu64 " ms)"), tag, Chrono::steadyMSecs() - ts);
}
}