Implemented unified cryptonight and RandomX scratchpad memory.
This commit is contained in:
parent
5699147aab
commit
20313cbc56
30 changed files with 434 additions and 297 deletions
|
@ -38,6 +38,15 @@ namespace xmrig {
|
|||
class VirtualMemory
|
||||
{
|
||||
public:
|
||||
inline VirtualMemory() {}
|
||||
VirtualMemory(size_t size, bool hugePages = true, size_t align = 64);
|
||||
~VirtualMemory();
|
||||
|
||||
inline bool isHugePages() const { return m_flags & HUGEPAGES; }
|
||||
inline size_t hugePages() const { return isHugePages() ? (align(size()) / 2097152) : 0; }
|
||||
inline size_t size() const { return m_size; }
|
||||
inline uint8_t *scratchpad() const { return m_scratchpad; }
|
||||
|
||||
static void *allocateExecutableMemory(size_t size);
|
||||
static void *allocateLargePagesMemory(size_t size);
|
||||
static void flushInstructionCache(void *p, size_t size);
|
||||
|
@ -46,6 +55,17 @@ public:
|
|||
static void unprotectExecutableMemory(void *p, size_t size);
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
int m_flags = 0;
|
||||
size_t m_size = 0;
|
||||
uint8_t *m_scratchpad = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include <sys/mman.h>
|
||||
|
||||
|
||||
#include "crypto/common/portable/mm_malloc.h"
|
||||
#include "crypto/common/VirtualMemory.h"
|
||||
|
||||
|
||||
|
@ -37,6 +38,47 @@
|
|||
#endif
|
||||
|
||||
|
||||
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;
|
||||
|
||||
madvise(m_scratchpad, size, MADV_RANDOM | MADV_WILLNEED);
|
||||
|
||||
if (mlock(m_scratchpad, m_size) == 0) {
|
||||
m_flags |= LOCK;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_scratchpad = static_cast<uint8_t*>(_mm_malloc(m_size, align));
|
||||
}
|
||||
|
||||
|
||||
xmrig::VirtualMemory::~VirtualMemory()
|
||||
{
|
||||
if (!m_scratchpad) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isHugePages()) {
|
||||
if (m_flags & LOCK) {
|
||||
munlock(m_scratchpad, m_size);
|
||||
}
|
||||
|
||||
freeLargePagesMemory(m_scratchpad, m_size);
|
||||
}
|
||||
else {
|
||||
_mm_free(m_scratchpad);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size)
|
||||
{
|
||||
|
|
|
@ -32,6 +32,37 @@
|
|||
#include "crypto/common/VirtualMemory.h"
|
||||
|
||||
|
||||
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;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_scratchpad = static_cast<uint8_t*>(_mm_malloc(m_size, align));
|
||||
}
|
||||
|
||||
|
||||
xmrig::VirtualMemory::~VirtualMemory()
|
||||
{
|
||||
if (!m_scratchpad) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isHugePages()) {
|
||||
freeLargePagesMemory(m_scratchpad, m_size);
|
||||
}
|
||||
else {
|
||||
_mm_free(m_scratchpad);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void *xmrig::VirtualMemory::allocateExecutableMemory(size_t size)
|
||||
{
|
||||
return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue