VirtualMemory class refactoring.
This commit is contained in:
parent
72c9d94390
commit
c13c83b902
4 changed files with 39 additions and 37 deletions
|
@ -80,7 +80,7 @@ int xmrig::App::exec()
|
|||
m_console = new Console(this);
|
||||
}
|
||||
|
||||
VirtualMemory::init(m_controller->config()->cpu().isHugePages());
|
||||
VirtualMemory::init();
|
||||
|
||||
Summary::print(m_controller);
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
#include "base/tools/Object.h"
|
||||
|
||||
|
||||
#include <bitset>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
@ -42,13 +43,12 @@ namespace xmrig {
|
|||
class VirtualMemory
|
||||
{
|
||||
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();
|
||||
|
||||
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 uint8_t *scratchpad() const { return m_scratchpad; }
|
||||
|
||||
|
@ -57,30 +57,29 @@ public:
|
|||
return { isHugePages() ? (align(size()) / 2097152) : 0, align(size()) / 2097152 };
|
||||
}
|
||||
|
||||
static bool isHugepagesAvailable();
|
||||
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);
|
||||
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 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; }
|
||||
|
||||
private:
|
||||
enum Flags {
|
||||
HUGEPAGES_AVAILABLE = 1,
|
||||
HUGEPAGES = 2,
|
||||
LOCK = 4
|
||||
FLAG_HUGEPAGES,
|
||||
FLAG_LOCK,
|
||||
FLAG_EXTERNAL,
|
||||
FLAG_MAX
|
||||
};
|
||||
|
||||
static int m_globalFlags;
|
||||
|
||||
int m_flags = 0;
|
||||
size_t m_size = 0;
|
||||
uint8_t *m_scratchpad = nullptr;
|
||||
std::bitset<FLAG_MAX> m_flags;
|
||||
size_t m_size = 0;
|
||||
uint8_t *m_scratchpad = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -38,21 +38,18 @@
|
|||
#endif
|
||||
|
||||
|
||||
int xmrig::VirtualMemory::m_globalFlags = 0;
|
||||
|
||||
|
||||
xmrig::VirtualMemory::VirtualMemory(size_t size, bool hugePages, size_t align) :
|
||||
m_size(VirtualMemory::align(size))
|
||||
{
|
||||
if (hugePages) {
|
||||
m_scratchpad = static_cast<uint8_t*>(allocateLargePagesMemory(m_size));
|
||||
if (m_scratchpad) {
|
||||
m_flags |= HUGEPAGES;
|
||||
m_flags.set(FLAG_HUGEPAGES, true);
|
||||
|
||||
madvise(m_scratchpad, size, MADV_RANDOM | MADV_WILLNEED);
|
||||
|
||||
if (mlock(m_scratchpad, m_size) == 0) {
|
||||
m_flags |= LOCK;
|
||||
m_flags.set(FLAG_LOCK, true);
|
||||
}
|
||||
|
||||
return;
|
||||
|
@ -70,7 +67,7 @@ xmrig::VirtualMemory::~VirtualMemory()
|
|||
}
|
||||
|
||||
if (isHugePages()) {
|
||||
if (m_flags & LOCK) {
|
||||
if (m_flags.test(FLAG_LOCK)) {
|
||||
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)
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -36,6 +36,12 @@
|
|||
#include "crypto/common/VirtualMemory.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
static bool hugepagesAvailable = false;
|
||||
|
||||
|
||||
/*****************************************************************
|
||||
SetLockPagesPrivilege: a function to obtain or
|
||||
release the privilege of locking physical pages.
|
||||
|
@ -83,7 +89,7 @@ static BOOL SetLockPagesPrivilege() {
|
|||
static LSA_UNICODE_STRING StringToLsaUnicodeString(LPCTSTR string) {
|
||||
LSA_UNICODE_STRING lsaString;
|
||||
|
||||
DWORD dwLen = (DWORD) wcslen(string);
|
||||
const auto dwLen = (DWORD) wcslen(string);
|
||||
lsaString.Buffer = (LPWSTR) string;
|
||||
lsaString.Length = (USHORT)((dwLen) * 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) :
|
||||
|
@ -150,7 +156,7 @@ xmrig::VirtualMemory::VirtualMemory(size_t size, bool hugePages, size_t align) :
|
|||
if (hugePages) {
|
||||
m_scratchpad = static_cast<uint8_t*>(allocateLargePagesMemory(m_size));
|
||||
if (m_scratchpad) {
|
||||
m_flags |= HUGEPAGES;
|
||||
m_flags.set(FLAG_HUGEPAGES, true);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -175,6 +181,12 @@ xmrig::VirtualMemory::~VirtualMemory()
|
|||
}
|
||||
|
||||
|
||||
bool xmrig::VirtualMemory::isHugepagesAvailable()
|
||||
{
|
||||
return hugepagesAvailable;
|
||||
}
|
||||
|
||||
|
||||
void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size)
|
||||
{
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_globalFlags = HUGEPAGES;
|
||||
|
||||
if (TrySetLockPagesPrivilege()) {
|
||||
m_globalFlags |= HUGEPAGES_AVAILABLE;
|
||||
}
|
||||
hugepagesAvailable = TrySetLockPagesPrivilege();
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue