VirtualMemory class refactoring.

This commit is contained in:
XMRig 2019-10-06 17:49:15 +07:00
parent 72c9d94390
commit c13c83b902
4 changed files with 39 additions and 37 deletions

View file

@ -80,7 +80,7 @@ int xmrig::App::exec()
m_console = new Console(this); m_console = new Console(this);
} }
VirtualMemory::init(m_controller->config()->cpu().isHugePages()); VirtualMemory::init();
Summary::print(m_controller); Summary::print(m_controller);

View file

@ -31,6 +31,7 @@
#include "base/tools/Object.h" #include "base/tools/Object.h"
#include <bitset>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <utility> #include <utility>
@ -42,13 +43,12 @@ namespace xmrig {
class VirtualMemory class VirtualMemory
{ {
public: public:
XMRIG_DISABLE_COPY_MOVE(VirtualMemory) XMRIG_DISABLE_COPY_MOVE_DEFAULT(VirtualMemory)
VirtualMemory() = default;
VirtualMemory(size_t size, bool hugePages = true, size_t align = 64); VirtualMemory(size_t size, bool hugePages = true, size_t align = 64);
~VirtualMemory(); ~VirtualMemory();
inline bool isHugePages() const { return m_flags & HUGEPAGES; } inline bool isHugePages() const { return m_flags.test(FLAG_HUGEPAGES); }
inline size_t size() const { return m_size; } inline size_t size() const { return m_size; }
inline uint8_t *scratchpad() const { return m_scratchpad; } inline uint8_t *scratchpad() const { return m_scratchpad; }
@ -57,28 +57,27 @@ public:
return { isHugePages() ? (align(size()) / 2097152) : 0, align(size()) / 2097152 }; return { isHugePages() ? (align(size()) / 2097152) : 0, align(size()) / 2097152 };
} }
static bool isHugepagesAvailable();
static uint32_t bindToNUMANode(int64_t affinity); static uint32_t bindToNUMANode(int64_t affinity);
static void *allocateExecutableMemory(size_t size); static void *allocateExecutableMemory(size_t size);
static void *allocateLargePagesMemory(size_t size); static void *allocateLargePagesMemory(size_t size);
static void flushInstructionCache(void *p, size_t size); static void flushInstructionCache(void *p, size_t size);
static void freeLargePagesMemory(void *p, size_t size); static void freeLargePagesMemory(void *p, size_t size);
static void init(bool hugePages); static void init();
static void protectExecutableMemory(void *p, size_t size); static void protectExecutableMemory(void *p, size_t size);
static void unprotectExecutableMemory(void *p, size_t size); static void unprotectExecutableMemory(void *p, size_t size);
static inline bool isHugepagesAvailable() { return (m_globalFlags & HUGEPAGES_AVAILABLE) != 0; }
static inline constexpr size_t align(size_t pos, size_t align = 2097152) { return ((pos - 1) / align + 1) * align; } static inline constexpr size_t align(size_t pos, size_t align = 2097152) { return ((pos - 1) / align + 1) * align; }
private: private:
enum Flags { enum Flags {
HUGEPAGES_AVAILABLE = 1, FLAG_HUGEPAGES,
HUGEPAGES = 2, FLAG_LOCK,
LOCK = 4 FLAG_EXTERNAL,
FLAG_MAX
}; };
static int m_globalFlags; std::bitset<FLAG_MAX> m_flags;
int m_flags = 0;
size_t m_size = 0; size_t m_size = 0;
uint8_t *m_scratchpad = nullptr; uint8_t *m_scratchpad = nullptr;
}; };

View file

@ -38,21 +38,18 @@
#endif #endif
int xmrig::VirtualMemory::m_globalFlags = 0;
xmrig::VirtualMemory::VirtualMemory(size_t size, bool hugePages, size_t align) : xmrig::VirtualMemory::VirtualMemory(size_t size, bool hugePages, size_t align) :
m_size(VirtualMemory::align(size)) m_size(VirtualMemory::align(size))
{ {
if (hugePages) { if (hugePages) {
m_scratchpad = static_cast<uint8_t*>(allocateLargePagesMemory(m_size)); m_scratchpad = static_cast<uint8_t*>(allocateLargePagesMemory(m_size));
if (m_scratchpad) { if (m_scratchpad) {
m_flags |= HUGEPAGES; m_flags.set(FLAG_HUGEPAGES, true);
madvise(m_scratchpad, size, MADV_RANDOM | MADV_WILLNEED); madvise(m_scratchpad, size, MADV_RANDOM | MADV_WILLNEED);
if (mlock(m_scratchpad, m_size) == 0) { if (mlock(m_scratchpad, m_size) == 0) {
m_flags |= LOCK; m_flags.set(FLAG_LOCK, true);
} }
return; return;
@ -70,7 +67,7 @@ xmrig::VirtualMemory::~VirtualMemory()
} }
if (isHugePages()) { if (isHugePages()) {
if (m_flags & LOCK) { if (m_flags.test(FLAG_LOCK)) {
munlock(m_scratchpad, m_size); munlock(m_scratchpad, m_size);
} }
@ -82,6 +79,11 @@ xmrig::VirtualMemory::~VirtualMemory()
} }
bool xmrig::VirtualMemory::isHugepagesAvailable()
{
return true;
}
void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size) void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size)
{ {
@ -123,11 +125,8 @@ void xmrig::VirtualMemory::freeLargePagesMemory(void *p, size_t size)
} }
void xmrig::VirtualMemory::init(bool hugePages) void xmrig::VirtualMemory::init()
{ {
if (hugePages) {
m_globalFlags = HUGEPAGES | HUGEPAGES_AVAILABLE;
}
} }

View file

@ -36,6 +36,12 @@
#include "crypto/common/VirtualMemory.h" #include "crypto/common/VirtualMemory.h"
namespace xmrig {
static bool hugepagesAvailable = false;
/***************************************************************** /*****************************************************************
SetLockPagesPrivilege: a function to obtain or SetLockPagesPrivilege: a function to obtain or
release the privilege of locking physical pages. release the privilege of locking physical pages.
@ -83,7 +89,7 @@ static BOOL SetLockPagesPrivilege() {
static LSA_UNICODE_STRING StringToLsaUnicodeString(LPCTSTR string) { static LSA_UNICODE_STRING StringToLsaUnicodeString(LPCTSTR string) {
LSA_UNICODE_STRING lsaString; LSA_UNICODE_STRING lsaString;
DWORD dwLen = (DWORD) wcslen(string); const auto dwLen = (DWORD) wcslen(string);
lsaString.Buffer = (LPWSTR) string; lsaString.Buffer = (LPWSTR) string;
lsaString.Length = (USHORT)((dwLen) * sizeof(WCHAR)); lsaString.Length = (USHORT)((dwLen) * sizeof(WCHAR));
lsaString.MaximumLength = (USHORT)((dwLen + 1) * sizeof(WCHAR)); lsaString.MaximumLength = (USHORT)((dwLen + 1) * sizeof(WCHAR));
@ -141,7 +147,7 @@ static BOOL TrySetLockPagesPrivilege() {
} }
int xmrig::VirtualMemory::m_globalFlags = 0; } // namespace xmrig
xmrig::VirtualMemory::VirtualMemory(size_t size, bool hugePages, size_t align) : xmrig::VirtualMemory::VirtualMemory(size_t size, bool hugePages, size_t align) :
@ -150,7 +156,7 @@ xmrig::VirtualMemory::VirtualMemory(size_t size, bool hugePages, size_t align) :
if (hugePages) { if (hugePages) {
m_scratchpad = static_cast<uint8_t*>(allocateLargePagesMemory(m_size)); m_scratchpad = static_cast<uint8_t*>(allocateLargePagesMemory(m_size));
if (m_scratchpad) { if (m_scratchpad) {
m_flags |= HUGEPAGES; m_flags.set(FLAG_HUGEPAGES, true);
return; return;
} }
@ -175,6 +181,12 @@ xmrig::VirtualMemory::~VirtualMemory()
} }
bool xmrig::VirtualMemory::isHugepagesAvailable()
{
return hugepagesAvailable;
}
void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size) void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size)
{ {
return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
@ -206,17 +218,9 @@ void xmrig::VirtualMemory::freeLargePagesMemory(void *p, size_t)
} }
void xmrig::VirtualMemory::init(bool hugePages) void xmrig::VirtualMemory::init()
{ {
if (!hugePages) { hugepagesAvailable = TrySetLockPagesPrivilege();
return;
}
m_globalFlags = HUGEPAGES;
if (TrySetLockPagesPrivilege()) {
m_globalFlags |= HUGEPAGES_AVAILABLE;
}
} }