mirror of
https://github.com/apple/foundationdb.git
synced 2025-05-15 18:32:18 +08:00
Put guard pages next to fast alloc memory (#6885)
* Put guard pages next to fast alloc memory I verified that we can now detect #6753 without creating tons of threads. * Use pageSize instead of 4096 * Don't include mmapInternal for windows
This commit is contained in:
parent
eedac50bb5
commit
297d831192
@ -519,7 +519,7 @@ void FastAllocator<Size>::getMagazine() {
|
|||||||
--g_allocation_tracing_disabled;
|
--g_allocation_tracing_disabled;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
block = (void**)::allocate(magazine_size * Size, false);
|
block = (void**)::allocate(magazine_size * Size, /*allowLargePages*/ false, /*includeGuardPages*/ true);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// void** block = new void*[ magazine_size * PSize ];
|
// void** block = new void*[ magazine_size * PSize ];
|
||||||
|
@ -2037,7 +2037,22 @@ static void enableLargePages() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void* allocateInternal(size_t length, bool largePages) {
|
#ifndef _WIN32
|
||||||
|
static void* mmapInternal(size_t length, int flags, bool guardPages) {
|
||||||
|
if (guardPages) {
|
||||||
|
constexpr size_t pageSize = 4096;
|
||||||
|
length += 2 * pageSize; // Map enough for the guard pages
|
||||||
|
void* resultWithGuardPages = mmap(nullptr, length, PROT_READ | PROT_WRITE, flags, -1, 0);
|
||||||
|
mprotect(resultWithGuardPages, pageSize, PROT_NONE); // left guard page
|
||||||
|
mprotect((void*)(uintptr_t(resultWithGuardPages) + length - pageSize), pageSize, PROT_NONE); // right guard page
|
||||||
|
return (void*)(uintptr_t(resultWithGuardPages) + pageSize);
|
||||||
|
} else {
|
||||||
|
return mmap(nullptr, length, PROT_READ | PROT_WRITE, flags, -1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void* allocateInternal(size_t length, bool largePages, bool guardPages) {
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
DWORD allocType = MEM_COMMIT | MEM_RESERVE;
|
DWORD allocType = MEM_COMMIT | MEM_RESERVE;
|
||||||
@ -2052,31 +2067,31 @@ static void* allocateInternal(size_t length, bool largePages) {
|
|||||||
if (largePages)
|
if (largePages)
|
||||||
flags |= MAP_HUGETLB;
|
flags |= MAP_HUGETLB;
|
||||||
|
|
||||||
return mmap(nullptr, length, PROT_READ | PROT_WRITE, flags, -1, 0);
|
return mmapInternal(length, flags, guardPages);
|
||||||
#elif defined(__APPLE__) || defined(__FreeBSD__)
|
#elif defined(__APPLE__) || defined(__FreeBSD__)
|
||||||
int flags = MAP_PRIVATE | MAP_ANON;
|
int flags = MAP_PRIVATE | MAP_ANON;
|
||||||
|
|
||||||
return mmap(nullptr, length, PROT_READ | PROT_WRITE, flags, -1, 0);
|
return mmapInternal(length, flags, guardPages);
|
||||||
#else
|
#else
|
||||||
#error Port me!
|
#error Port me!
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool largeBlockFail = false;
|
static bool largeBlockFail = false;
|
||||||
void* allocate(size_t length, bool allowLargePages) {
|
void* allocate(size_t length, bool allowLargePages, bool includeGuardPages) {
|
||||||
if (allowLargePages)
|
if (allowLargePages)
|
||||||
enableLargePages();
|
enableLargePages();
|
||||||
|
|
||||||
void* block = ALLOC_FAIL;
|
void* block = ALLOC_FAIL;
|
||||||
|
|
||||||
if (allowLargePages && !largeBlockFail) {
|
if (allowLargePages && !largeBlockFail) {
|
||||||
block = allocateInternal(length, true);
|
block = allocateInternal(length, true, includeGuardPages);
|
||||||
if (block == ALLOC_FAIL)
|
if (block == ALLOC_FAIL)
|
||||||
largeBlockFail = true;
|
largeBlockFail = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (block == ALLOC_FAIL)
|
if (block == ALLOC_FAIL)
|
||||||
block = allocateInternal(length, false);
|
block = allocateInternal(length, false, includeGuardPages);
|
||||||
|
|
||||||
// FIXME: SevWarnAlways trace if "close" to out of memory
|
// FIXME: SevWarnAlways trace if "close" to out of memory
|
||||||
|
|
||||||
|
@ -284,7 +284,7 @@ std::string epochsToGMTString(double epochs);
|
|||||||
|
|
||||||
void setMemoryQuota(size_t limit);
|
void setMemoryQuota(size_t limit);
|
||||||
|
|
||||||
void* allocate(size_t length, bool allowLargePages);
|
void* allocate(size_t length, bool allowLargePages, bool includeGuardPages);
|
||||||
|
|
||||||
void setAffinity(int proc);
|
void setAffinity(int proc);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user