Commit 647ad236 authored by David Carlier's avatar David Carlier Committed by Facebook Github Bot

experimental enable jemalloc API for FreeBSD too. (#1294)

Summary:
Contrary to other systems, jemalloc is part of the FreeBSD's libc.
As such APIs are directly exposed. Enabling super pages
as well, supporting same page size.
Pull Request resolved: https://github.com/facebook/folly/pull/1294

Reviewed By: interwq

Differential Revision: D19249354

Pulled By: yfeldblum

fbshipit-source-id: 845f594b28f30b0360d4573d8a7354f9790bc8b7
parent d054e34e
...@@ -20,7 +20,11 @@ include(CheckSymbolExists) ...@@ -20,7 +20,11 @@ include(CheckSymbolExists)
include(CheckTypeSize) include(CheckTypeSize)
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
CHECK_INCLUDE_FILE_CXX(jemalloc/jemalloc.h FOLLY_USE_JEMALLOC) if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
CHECK_INCLUDE_FILE_CXX(malloc_np.h FOLLY_USE_JEMALLOC)
else()
CHECK_INCLUDE_FILE_CXX(jemalloc/jemalloc.h FOLLY_USE_JEMALLOC)
endif()
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Windows") if(NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
# clang only rejects unknown warning flags if -Werror=unknown-warning-option # clang only rejects unknown warning flags if -Werror=unknown-warning-option
......
...@@ -22,13 +22,15 @@ ...@@ -22,13 +22,15 @@
#include <sstream> #include <sstream>
#if defined(MADV_HUGEPAGE) && defined(FOLLY_USE_JEMALLOC) && !FOLLY_SANITIZE #if (defined(MADV_HUGEPAGE) || defined(MAP_ALIGNED_SUPER)) && \
#if (JEMALLOC_VERSION_MAJOR >= 5) defined(FOLLY_USE_JEMALLOC) && !FOLLY_SANITIZE
#if defined(__FreeBSD__) || (JEMALLOC_VERSION_MAJOR >= 5)
#define FOLLY_JEMALLOC_HUGE_PAGE_ALLOCATOR_SUPPORTED 1 #define FOLLY_JEMALLOC_HUGE_PAGE_ALLOCATOR_SUPPORTED 1
bool folly::JemallocHugePageAllocator::hugePagesSupported{true}; bool folly::JemallocHugePageAllocator::hugePagesSupported{true};
#endif #endif
#endif // MADV_HUGEPAGE && defined(FOLLY_USE_JEMALLOC) && !FOLLY_SANITIZE #endif // MADV_HUGEPAGE || MAP_ALIGNED_SUPER && defined(FOLLY_USE_JEMALLOC) &&
// !FOLLY_SANITIZE
#ifndef FOLLY_JEMALLOC_HUGE_PAGE_ALLOCATOR_SUPPORTED #ifndef FOLLY_JEMALLOC_HUGE_PAGE_ALLOCATOR_SUPPORTED
// Some mocks when jemalloc.h is not included or version too old // Some mocks when jemalloc.h is not included or version too old
...@@ -119,11 +121,15 @@ static inline T align_up(T val, U alignment) { ...@@ -119,11 +121,15 @@ static inline T align_up(T val, U alignment) {
static uintptr_t map_pages(size_t nr_pages) { static uintptr_t map_pages(size_t nr_pages) {
// Initial mmapped area is large enough to contain the aligned huge pages // Initial mmapped area is large enough to contain the aligned huge pages
size_t alloc_size = nr_pages * kHugePageSize; size_t alloc_size = nr_pages * kHugePageSize;
int mflags = MAP_PRIVATE | MAP_ANONYMOUS;
#if defined(__FreeBSD__)
mflags |= MAP_ALIGNED_SUPER;
#endif
void* p = mmap( void* p = mmap(
nullptr, nullptr,
alloc_size + kHugePageSize, alloc_size + kHugePageSize,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, mflags,
-1, -1,
0); 0);
...@@ -134,6 +140,7 @@ static uintptr_t map_pages(size_t nr_pages) { ...@@ -134,6 +140,7 @@ static uintptr_t map_pages(size_t nr_pages) {
// Aligned start address // Aligned start address
uintptr_t first_page = align_up((uintptr_t)p, kHugePageSize); uintptr_t first_page = align_up((uintptr_t)p, kHugePageSize);
#if !defined(__FreeBSD__)
// Unmap left-over 4k pages // Unmap left-over 4k pages
munmap(p, first_page - (uintptr_t)p); munmap(p, first_page - (uintptr_t)p);
munmap( munmap(
...@@ -158,6 +165,7 @@ static uintptr_t map_pages(size_t nr_pages) { ...@@ -158,6 +165,7 @@ static uintptr_t map_pages(size_t nr_pages) {
ptr += kHugePageSize) { ptr += kHugePageSize) {
memset((void*)ptr, 0, 1); memset((void*)ptr, 0, 1);
} }
#endif
return first_page; return first_page;
} }
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
extern "C" { extern "C" {
#if FOLLY_HAVE_WEAK_SYMBOLS #if FOLLY_HAVE_WEAK_SYMBOLS
#if !defined(__FreeBSD__)
void* mallocx(size_t, int) __attribute__((__nothrow__, __weak__)); void* mallocx(size_t, int) __attribute__((__nothrow__, __weak__));
void* rallocx(void*, size_t, int) __attribute__((__nothrow__, __weak__)); void* rallocx(void*, size_t, int) __attribute__((__nothrow__, __weak__));
size_t xallocx(void*, size_t, size_t, int) size_t xallocx(void*, size_t, size_t, int)
...@@ -37,6 +38,7 @@ int mallctlnametomib(const char*, size_t*, size_t*) ...@@ -37,6 +38,7 @@ int mallctlnametomib(const char*, size_t*, size_t*)
__attribute__((__nothrow__, __weak__)); __attribute__((__nothrow__, __weak__));
int mallctlbymib(const size_t*, size_t, void*, size_t*, void*, size_t) int mallctlbymib(const size_t*, size_t, void*, size_t*, void*, size_t)
__attribute__((__nothrow__, __weak__)); __attribute__((__nothrow__, __weak__));
#endif
#else #else
extern void* (*mallocx)(size_t, int); extern void* (*mallocx)(size_t, int);
extern void* (*rallocx)(void*, size_t, int); extern void* (*rallocx)(void*, size_t, int);
......
...@@ -28,7 +28,11 @@ ...@@ -28,7 +28,11 @@
#endif #endif
// JEMalloc provides it's own implementation of // JEMalloc provides it's own implementation of
// malloc_usable_size, and that's what we should be using. // malloc_usable_size, and that's what we should be using.
#if defined(__FreeBSD__)
#include <malloc_np.h>
#else
#include <jemalloc/jemalloc.h> // @manual #include <jemalloc/jemalloc.h> // @manual
#endif
#else #else
#if !defined(__FreeBSD__) #if !defined(__FreeBSD__)
#if __has_include(<malloc.h>) #if __has_include(<malloc.h>)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment