From 30642881bf8c6698a4e0773b12627283f4d1197f Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 9 Jun 2017 15:09:21 +0300 Subject: [PATCH] Add Mem class. --- CMakeLists.txt | 9 +++-- src/App.cpp | 2 ++ src/Mem.cpp | 28 ++++++++++++++++ src/Mem.h | 51 +++++++++++++++++++++++++++++ win/memory_win.c => src/Mem_win.cpp | 44 +++++++++++-------------- 5 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 src/Mem.cpp create mode 100644 src/Mem.h rename win/memory_win.c => src/Mem_win.cpp (79%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f34e234..9d43b639 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,14 +6,15 @@ option(WITH_AEON "CryptoNight-Lite support" ON) set(HEADERS src/App.h + src/Console.h + src/Cpu.h src/interfaces/IClientListener.h + src/Mem.h src/net/Client.h src/net/Job.h src/net/Network.h src/net/Url.h src/Options.h - src/Console.h - src/Cpu.h src/Summary.h src/version.h ) @@ -34,12 +35,13 @@ set(HEADERS_CRYPTO set(SOURCES src/App.cpp + src/Console.cpp + src/Mem.cpp src/net/Client.cpp src/net/Job.cpp src/net/Network.cpp src/net/Url.cpp src/Options.cpp - src/Console.cpp src/Summary.cpp src/xmrig.cpp ) @@ -61,6 +63,7 @@ if (WIN32) src/3rdparty/winansi.cpp src/3rdparty/winansi.h src/Cpu_win.cpp + src/Mem_win.cpp src/net/Network_win.cpp ) diff --git a/src/App.cpp b/src/App.cpp index 6608554c..b6a0720a 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -29,6 +29,7 @@ #include "Console.h" #include "Cpu.h" #include "crypto/CryptoNight.h" +#include "Mem.h" #include "net/Client.h" #include "net/Network.h" #include "Options.h" @@ -67,6 +68,7 @@ App::exec() return 1; } + Mem::allocate(m_options->algo(), m_options->threads(), m_options->doubleHash()); Summary::print(); m_network->connect(); diff --git a/src/Mem.cpp b/src/Mem.cpp new file mode 100644 index 00000000..b7bf2511 --- /dev/null +++ b/src/Mem.cpp @@ -0,0 +1,28 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Mem.h" + + +uint8_t *Mem::m_memory = nullptr; +int Mem::m_flags = 0; diff --git a/src/Mem.h b/src/Mem.h new file mode 100644 index 00000000..d3841735 --- /dev/null +++ b/src/Mem.h @@ -0,0 +1,51 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __MEM_H__ +#define __MEM_H__ + + +#include "crypto/CryptoNight.h" + + +class Mem +{ +public: + enum Flags { + HUGEPAGES_AVAILABLE = 1, + HUGEPAGES_ENABLED = 2, + LOCK = 4 + }; + + static bool allocate(int algo, int threads, bool doubleHash); + static void release(); + + static inline int flags() { return m_flags; } + +private: + static uint8_t *m_memory __attribute__((aligned(16))); + static int m_flags; +}; + + +#endif /* __MEM_H__ */ diff --git a/win/memory_win.c b/src/Mem_win.cpp similarity index 79% rename from win/memory_win.c rename to src/Mem_win.cpp index 9ff63dad..bab483bb 100644 --- a/win/memory_win.c +++ b/src/Mem_win.cpp @@ -21,20 +21,16 @@ * along with this program. If not, see . */ -#ifndef __MEMORY_H__ -#define __MEMORY_H__ +#include #include #include #include -#include "options.h" -#include "persistent_memory.h" -#include "utils/applog.h" - -char *persistent_memory; -int persistent_memory_flags = 0; +#include "Mem.h" +#include "Console.h" +#include "Options.h" /***************************************************************** @@ -122,7 +118,7 @@ static BOOL ObtainLockPagesPrivilege() { LSA_UNICODE_STRING str = StringToLsaUnicodeString(_T(SE_LOCK_MEMORY_NAME)); if (LsaAddAccountRights(handle, user->User.Sid, &str, 1) == 0) { - applog_notime(LOG_WARNING, "Huge pages support was successfully enabled, but reboot required to use it"); + LOG_DEBUG("Huge pages support was successfully enabled, but reboot required to use it"); result = TRUE; } @@ -143,33 +139,33 @@ static BOOL TrySetLockPagesPrivilege() { } -const char * persistent_memory_allocate() { - const int ratio = (opt_double_hash && opt_algo != ALGO_CRYPTONIGHT_LITE) ? 2 : 1; - const int size = MEMORY * (opt_n_threads * ratio + 1); +bool Mem::allocate(int algo, int threads, bool doubleHash) +{ + const int ratio = (doubleHash && algo != Options::ALGO_CRYPTONIGHT_LITE) ? 2 : 1; + const size_t size = MEMORY * (threads * ratio + 1); if (TrySetLockPagesPrivilege()) { - persistent_memory_flags |= MEMORY_HUGEPAGES_AVAILABLE; + m_flags |= HUGEPAGES_AVAILABLE; } - persistent_memory = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE); - if (!persistent_memory) { - persistent_memory = _mm_malloc(size, 16); + m_memory = static_cast(VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE)); + if (!m_memory) { + m_memory = static_cast(_mm_malloc(size, 16)); } else { - persistent_memory_flags |= MEMORY_HUGEPAGES_ENABLED; + m_flags |= HUGEPAGES_ENABLED; } - return persistent_memory; + return true; } -void persistent_memory_free() { - if (persistent_memory_flags & MEMORY_HUGEPAGES_ENABLED) { - VirtualFree(persistent_memory, 0, MEM_RELEASE); +void Mem::release() +{ + if (m_flags & HUGEPAGES_ENABLED) { + VirtualFree(m_memory, 0, MEM_RELEASE); } else { - _mm_free(persistent_memory); + _mm_free(m_memory); } } - -#endif /* __MEMORY_H__ */