proxygen
folly::JemallocHugePageAllocator Class Reference

#include <JemallocHugePageAllocator.h>

Static Public Member Functions

static bool init (int nr_pages)
 
static void * allocate (size_t size)
 
static void * reallocate (void *p, size_t size)
 
static void deallocate (void *p, size_t=0)
 
static bool initialized ()
 
static size_t freeSpace ()
 
static bool addressInArena (void *address)
 

Static Private Attributes

static int flags_ {0}
 
static bool hugePagesSupported {false}
 

Detailed Description

An allocator which uses Jemalloc to create a dedicated huge page arena, backed by 2MB huge pages (on linux x86-64).

This allocator is specifically intended for linux with the transparent huge page support set to 'madvise' and defrag policy set to 'madvise' or 'defer+madvise'. These can be controller via /sys/kernel/mm/transparent_hugepage/enabled and /sys/kernel/mm/transparent_hugepage/defrag.

The allocator reserves a fixed-size area using mmap, and sets the MADV_HUGEPAGE page attribute using the madvise system call. A custom jemalloc hook is installed which is called when creating a new extent of memory. This will allocate from the reserved area if possible, and otherwise fall back to the default method. Jemalloc does not use allocated extents across different arenas without first unmapping them, and the advice flags are cleared on munmap. A regular malloc will never end up allocating memory from this arena.

If binary isn't linked with jemalloc, the logic falls back to malloc / free.

Note that the madvise call does not guarantee huge pages, it is best effort.

1GB Huge Pages are not supported at this point.

Definition at line 57 of file JemallocHugePageAllocator.h.

Member Function Documentation

bool folly::JemallocHugePageAllocator::addressInArena ( void *  address)
static

Definition at line 279 of file JemallocHugePageAllocator.cpp.

Referenced by initialized(), and TEST().

279  {
280  return arena.addressInArena(address);
281 }
static void* folly::JemallocHugePageAllocator::allocate ( size_t  size)
inlinestatic

Definition at line 61 of file JemallocHugePageAllocator.h.

References flags_, hugePagesSupported, and mallocx.

Referenced by folly::CxxHugePageAllocator< T >::allocate(), and TEST().

61  {
62  // If uninitialized, flags_ will be 0 and the mallocx behavior
63  // will match that of a regular malloc
64  return hugePagesSupported ? mallocx(size, flags_) : malloc(size);
65  }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void *(* mallocx)(size_t, int)
Definition: MallocImpl.cpp:35
static void folly::JemallocHugePageAllocator::deallocate ( void *  p,
size_t  = 0 
)
inlinestatic

Definition at line 71 of file JemallocHugePageAllocator.h.

References dallocx, flags_, bm::free(), and hugePagesSupported.

Referenced by folly::CxxHugePageAllocator< T >::deallocate(), and TEST().

71  {
73  }
void(* dallocx)(void *, int)
Definition: MallocImpl.cpp:39
void free()
size_t folly::JemallocHugePageAllocator::freeSpace ( )
static

Definition at line 275 of file JemallocHugePageAllocator.cpp.

Referenced by initialized(), and TEST().

275  {
276  return arena.freeSpace();
277 }
bool folly::JemallocHugePageAllocator::init ( int  nr_pages)
static

Definition at line 258 of file JemallocHugePageAllocator.cpp.

References folly::usingJEMalloc(), and folly::WARNING.

Referenced by TEST().

258  {
259  if (!usingJEMalloc()) {
260  LOG(ERROR) << "Not linked with jemalloc?";
261  hugePagesSupported = false;
262  }
263  if (hugePagesSupported) {
264  if (flags_ == 0) {
265  flags_ = arena.init(nr_pages);
266  } else {
267  LOG(WARNING) << "Already initialized";
268  }
269  } else {
270  LOG(WARNING) << "Huge Page Allocator not supported";
271  }
272  return flags_ != 0;
273 }
bool usingJEMalloc() noexcept
Definition: Malloc.h:147
static bool folly::JemallocHugePageAllocator::initialized ( )
inlinestatic

Definition at line 75 of file JemallocHugePageAllocator.h.

References addressInArena(), flags_, and freeSpace().

Referenced by TEST().

75  {
76  return flags_ != 0;
77  }
static void* folly::JemallocHugePageAllocator::reallocate ( void *  p,
size_t  size 
)
inlinestatic

Definition at line 67 of file JemallocHugePageAllocator.h.

References flags_, hugePagesSupported, and rallocx.

67  {
68  return hugePagesSupported ? rallocx(p, size, flags_) : realloc(p, size);
69  }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void *(* rallocx)(void *, size_t, int)
Definition: MallocImpl.cpp:36

Member Data Documentation

int folly::JemallocHugePageAllocator::flags_ {0}
staticprivate

Definition at line 83 of file JemallocHugePageAllocator.h.

Referenced by allocate(), deallocate(), initialized(), and reallocate().

bool folly::JemallocHugePageAllocator::hugePagesSupported {false}
staticprivate

Definition at line 84 of file JemallocHugePageAllocator.h.

Referenced by allocate(), deallocate(), and reallocate().


The documentation for this class was generated from the following files: