From 7741c341c71a50ab1c19fb33fe529a8cdc936e5e Mon Sep 17 00:00:00 2001 From: Admin Date: Sat, 27 May 2017 10:34:42 +0300 Subject: [PATCH] Huge pages support on OS X. --- CMakeLists.txt | 8 +++++++- mac/memory_mac.c | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 39465124..9d30acd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,7 +87,13 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "") endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wno-pointer-to-int-cast") -set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") + +if (CMAKE_C_COMPILER_ID MATCHES "Clang") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -fmerge-all-constants") +else() + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") +endif() + #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -gdwarf-2") #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-generate") #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-use -fprofile-correction") diff --git a/mac/memory_mac.c b/mac/memory_mac.c index 868ffb14..3f5f714c 100644 --- a/mac/memory_mac.c +++ b/mac/memory_mac.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "persistent_memory.h" #include "options.h" @@ -36,12 +37,40 @@ int persistent_memory_flags = 0; 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); + persistent_memory_flags |= MEMORY_HUGEPAGES_AVAILABLE; + + persistent_memory = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0); + + if (persistent_memory == MAP_FAILED) { + persistent_memory = _mm_malloc(size, 16); + return persistent_memory; + } + + persistent_memory_flags |= MEMORY_HUGEPAGES_ENABLED; + + if (madvise(persistent_memory, size, MADV_RANDOM | MADV_WILLNEED) != 0) { + applog(LOG_ERR, "madvise failed"); + } + + if (mlock(persistent_memory, size) == 0) { + persistent_memory_flags |= MEMORY_LOCK; + } - persistent_memory = _mm_malloc(size, 16); return persistent_memory; } void persistent_memory_free() { - _mm_free(persistent_memory); + const int size = MEMORY * (opt_n_threads + 1); + + if (persistent_memory_flags & MEMORY_HUGEPAGES_ENABLED) { + if (persistent_memory_flags & MEMORY_LOCK) { + munlock(persistent_memory, size); + } + + munmap(persistent_memory, size); + } + else { + _mm_free(persistent_memory); + } }