move JemallocNodumpAllocator to folly/experimental
[folly.git] / folly / experimental / JemallocNodumpAllocator.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
18
19 #pragma once
20
21 #include <folly/folly-config.h>
22
23 #ifdef FOLLY_HAVE_LIBJEMALLOC
24
25 #include <folly/portability/SysMman.h>
26 #include <jemalloc/jemalloc.h>
27
28 #if (JEMALLOC_VERSION_MAJOR > 3) && defined(MADV_DONTDUMP)
29 #define FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED 1
30 #endif
31
32 #endif // FOLLY_HAVE_LIBJEMALLOC
33
34 #include <cstddef>
35
36 namespace folly {
37
38 /**
39  * An allocator which uses Jemalloc to create an dedicated arena to allocate
40  * memory from. The only special property set on the allocated memory is that
41  * the memory is not dump-able.
42  *
43  * This is done by setting MADV_DONTDUMP using the `madvise` system call. A
44  * custom hook installed which is called when allocating a new chunk of memory.
45  * All it does is call the original jemalloc hook to allocate the memory and
46  * then set the advise on it before returning the pointer to the allocated
47  * memory. Jemalloc does not use allocated chunks across different arenas,
48  * without `munmap`-ing them first, and the advises are not sticky i.e. they are
49  * unset if `munmap` is done. Also this arena can't be used by any other part of
50  * the code by just calling `malloc`.
51  *
52  * If target system doesn't support MADV_DONTDUMP or jemalloc doesn't support
53  * custom arena hook, JemallocNodumpAllocator would fall back to using malloc /
54  * free. Such behavior can be identified by using
55  * !defined(FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED).
56  *
57  * Similarly, if binary isn't linked with jemalloc, the logic would fall back to
58  * malloc / free.
59  */
60 class JemallocNodumpAllocator {
61  public:
62   enum class State {
63     ENABLED,
64     DISABLED,
65   };
66
67   // To be used as IOBuf::FreeFunction, userData should be set to
68   // reinterpret_cast<void*>(getFlags()).
69   static void deallocate(void* p, void* userData);
70
71   explicit JemallocNodumpAllocator(State state = State::ENABLED);
72
73   void* allocate(size_t size);
74   void* reallocate(void* p, size_t size);
75   void deallocate(void* p);
76
77   unsigned getArenaIndex() const { return arena_index_; }
78   int getFlags() const { return flags_; }
79
80  private:
81 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
82   static chunk_alloc_t* original_chunk_alloc_;
83   static void* chunk_alloc(void* chunk,
84                            size_t size,
85                            size_t alignment,
86                            bool* zero,
87                            bool* commit,
88                            unsigned arena_ind);
89 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
90
91   bool extend_and_setup_arena();
92
93   unsigned arena_index_{0};
94   int flags_{0};
95 };
96
97 /**
98  * JemallocNodumpAllocator singleton.
99  */
100 JemallocNodumpAllocator& globalJemallocNodumpAllocator();
101
102 } // folly