proxygen
folly::JemallocNodumpAllocator Class Reference

#include <JemallocNodumpAllocator.h>

Public Types

enum  State { State::ENABLED, State::DISABLED }
 

Public Member Functions

 JemallocNodumpAllocator (State state=State::ENABLED)
 
void * allocate (size_t size)
 
void * reallocate (void *p, size_t size)
 
void deallocate (void *p, size_t=0)
 
unsigned getArenaIndex () const
 
int getFlags () const
 

Static Public Member Functions

static void deallocate (void *p, void *userData)
 

Private Member Functions

bool extend_and_setup_arena ()
 

Private Attributes

unsigned arena_index_ {0}
 
int flags_ {0}
 

Detailed Description

An allocator which uses Jemalloc to create an dedicated arena to allocate memory from. The only special property set on the allocated memory is that the memory is not dump-able.

This is done by setting MADV_DONTDUMP using the madvise system call. A custom hook installed which is called when allocating a new chunk / extent of memory. All it does is call the original jemalloc hook to allocate the memory and then set the advise on it before returning the pointer to the allocated memory. Jemalloc does not use allocated chunks / extents across different arenas, without munmap-ing them first, and the advises are not sticky i.e. they are unset if munmap is done. Also this arena can't be used by any other part of the code by just calling malloc.

If target system doesn't support MADV_DONTDUMP or jemalloc doesn't support custom arena hook, JemallocNodumpAllocator would fall back to using malloc / free. Such behavior can be identified by using !defined(FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED).

Similarly, if binary isn't linked with jemalloc, the logic would fall back to malloc / free.

Definition at line 68 of file JemallocNodumpAllocator.h.

Member Enumeration Documentation

Enumerator
ENABLED 
DISABLED 

Definition at line 70 of file JemallocNodumpAllocator.h.

Constructor & Destructor Documentation

folly::JemallocNodumpAllocator::JemallocNodumpAllocator ( State  state = State::ENABLED)
explicit

Member Function Documentation

void * folly::JemallocNodumpAllocator::allocate ( size_t  size)

Definition at line 107 of file JemallocNodumpAllocator.cpp.

References flags_, and mallocx.

Referenced by TEST().

107  {
108  return mallocx != nullptr ? mallocx(size, flags_) : malloc(size);
109 }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void *(* mallocx)(size_t, int)
Definition: MallocImpl.cpp:35
void folly::JemallocNodumpAllocator::deallocate ( void *  p,
void *  userData 
)
static

Definition at line 158 of file JemallocNodumpAllocator.cpp.

References dallocx, bm::free(), and uint64_t.

Referenced by TEST().

158  {
159  const uint64_t flags = reinterpret_cast<uint64_t>(userData);
160  dallocx != nullptr ? dallocx(p, static_cast<int>(flags)) : free(p);
161 }
void(* dallocx)(void *, int)
Definition: MallocImpl.cpp:39
flags
Definition: http_parser.h:127
void free()
void folly::JemallocNodumpAllocator::deallocate ( void *  p,
size_t  = 0 
)

Definition at line 154 of file JemallocNodumpAllocator.cpp.

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

154  {
155  dallocx != nullptr ? dallocx(p, flags_) : free(p);
156 }
void(* dallocx)(void *, int)
Definition: MallocImpl.cpp:39
void free()
bool folly::JemallocNodumpAllocator::extend_and_setup_arena ( )
private

Definition at line 32 of file JemallocNodumpAllocator.cpp.

References arena_index_, folly::errnoStr(), folly::FATAL, flags_, mallctl, MALLOCX_ARENA, and MALLOCX_TCACHE_NONE.

Referenced by getFlags(), and JemallocNodumpAllocator().

32  {
33 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
34  if (mallctl == nullptr) {
35  // Not linked with jemalloc.
36  return false;
37  }
38 
39  size_t len = sizeof(arena_index_);
40  if (auto ret = mallctl(
41 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_CHUNK
42  "arenas.extend"
43 #else
44  "arenas.create"
45 #endif
46  ,
47  &arena_index_,
48  &len,
49  nullptr,
50  0)) {
51  LOG(FATAL) << "Unable to extend arena: " << errnoStr(ret);
52  }
54 
55 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_CHUNK
56  const auto key =
57  folly::to<std::string>("arena.", arena_index_, ".chunk_hooks");
58  chunk_hooks_t hooks;
59  len = sizeof(hooks);
60  // Read the existing hooks
61  if (auto ret = mallctl(key.c_str(), &hooks, &len, nullptr, 0)) {
62  LOG(FATAL) << "Unable to get the hooks: " << errnoStr(ret);
63  }
64  if (original_alloc_ == nullptr) {
65  original_alloc_ = hooks.alloc;
66  } else {
67  DCHECK_EQ(original_alloc_, hooks.alloc);
68  }
69 
70  // Set the custom hook
71  hooks.alloc = &JemallocNodumpAllocator::alloc;
72  if (auto ret =
73  mallctl(key.c_str(), nullptr, nullptr, &hooks, sizeof(hooks))) {
74  LOG(FATAL) << "Unable to set the hooks: " << errnoStr(ret);
75  }
76 #else
77  const auto key =
78  folly::to<std::string>("arena.", arena_index_, ".extent_hooks");
79  extent_hooks_t* hooks;
80  len = sizeof(hooks);
81  // Read the existing hooks
82  if (auto ret = mallctl(key.c_str(), &hooks, &len, nullptr, 0)) {
83  LOG(FATAL) << "Unable to get the hooks: " << errnoStr(ret);
84  }
85  if (original_alloc_ == nullptr) {
86  original_alloc_ = hooks->alloc;
87  } else {
88  DCHECK_EQ(original_alloc_, hooks->alloc);
89  }
90 
91  // Set the custom hook
92  extent_hooks_ = *hooks;
93  extent_hooks_.alloc = &JemallocNodumpAllocator::alloc;
94  extent_hooks_t* new_hooks = &extent_hooks_;
95  if (auto ret = mallctl(
96  key.c_str(), nullptr, nullptr, &new_hooks, sizeof(new_hooks))) {
97  LOG(FATAL) << "Unable to set the hooks: " << errnoStr(ret);
98  }
99 #endif
100 
101  return true;
102 #else // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
103  return false;
104 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
105 }
#define MALLOCX_ARENA(x)
int(* mallctl)(const char *, void *, size_t *, void *, size_t)
Definition: MallocImpl.cpp:42
fbstring errnoStr(int err)
Definition: String.cpp:463
#define MALLOCX_TCACHE_NONE
unsigned folly::JemallocNodumpAllocator::getArenaIndex ( ) const
inline

Definition at line 85 of file JemallocNodumpAllocator.h.

References arena_index_.

Referenced by TEST().

85  {
86  return arena_index_;
87  }
int folly::JemallocNodumpAllocator::getFlags ( ) const
inline

Definition at line 88 of file JemallocNodumpAllocator.h.

References extend_and_setup_arena(), and flags_.

88  {
89  return flags_;
90  }
void * folly::JemallocNodumpAllocator::reallocate ( void *  p,
size_t  size 
)

Definition at line 111 of file JemallocNodumpAllocator.cpp.

References folly::errnoStr(), flags_, rallocx, and folly::size().

111  {
112  return rallocx != nullptr ? rallocx(p, size, flags_) : realloc(p, size);
113 }
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

unsigned folly::JemallocNodumpAllocator::arena_index_ {0}
private
int folly::JemallocNodumpAllocator::flags_ {0}
private

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