1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 #include <folly/experimental/JemallocNodumpAllocator.h>
18 
19 #include <folly/Conv.h>
20 #include <folly/String.h>
21 #include <folly/memory/Malloc.h>
22 
23 #include <glog/logging.h>
24 
25 namespace folly {
26 
JemallocNodumpAllocator(State state)27 JemallocNodumpAllocator::JemallocNodumpAllocator(State state) {
28   if (state == State::ENABLED && extend_and_setup_arena()) {
29     LOG(INFO) << "Set up arena: " << arena_index_;
30   }
31 }
32 
extend_and_setup_arena()33 bool JemallocNodumpAllocator::extend_and_setup_arena() {
34 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
35   if (mallctl == nullptr) {
36     // Not linked with jemalloc.
37     return false;
38   }
39 
40   size_t len = sizeof(arena_index_);
41   if (auto ret = mallctl(
42 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_CHUNK
43           "arenas.extend"
44 #else
45           "arenas.create"
46 #endif
47           ,
48           &arena_index_,
49           &len,
50           nullptr,
51           0)) {
52     LOG(FATAL) << "Unable to extend arena: " << errnoStr(ret);
53   }
54   flags_ = MALLOCX_ARENA(arena_index_) | MALLOCX_TCACHE_NONE;
55 
56 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_CHUNK
57   const auto key =
58       folly::to<std::string>("arena.", arena_index_, ".chunk_hooks");
59   chunk_hooks_t hooks;
60   len = sizeof(hooks);
61   // Read the existing hooks
62   if (auto ret = mallctl(key.c_str(), &hooks, &len, nullptr, 0)) {
63     LOG(FATAL) << "Unable to get the hooks: " << errnoStr(ret);
64   }
65   if (original_alloc_ == nullptr) {
66     original_alloc_ = hooks.alloc;
67   } else {
68     DCHECK_EQ(original_alloc_, hooks.alloc);
69   }
70 
71   // Set the custom hook
72   hooks.alloc = &JemallocNodumpAllocator::alloc;
73   if (auto ret =
74           mallctl(key.c_str(), nullptr, nullptr, &hooks, sizeof(hooks))) {
75     LOG(FATAL) << "Unable to set the hooks: " << errnoStr(ret);
76   }
77 #else
78   const auto key =
79       folly::to<std::string>("arena.", arena_index_, ".extent_hooks");
80   extent_hooks_t* hooks;
81   len = sizeof(hooks);
82   // Read the existing hooks
83   if (auto ret = mallctl(key.c_str(), &hooks, &len, nullptr, 0)) {
84     LOG(FATAL) << "Unable to get the hooks: " << errnoStr(ret);
85   }
86   if (original_alloc_ == nullptr) {
87     original_alloc_ = hooks->alloc;
88   } else {
89     DCHECK_EQ(original_alloc_, hooks->alloc);
90   }
91 
92   // Set the custom hook
93   extent_hooks_ = *hooks;
94   extent_hooks_.alloc = &JemallocNodumpAllocator::alloc;
95   extent_hooks_t* new_hooks = &extent_hooks_;
96   if (auto ret = mallctl(
97           key.c_str(), nullptr, nullptr, &new_hooks, sizeof(new_hooks))) {
98     LOG(FATAL) << "Unable to set the hooks: " << errnoStr(ret);
99   }
100 #endif
101 
102   return true;
103 #else // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
104   return false;
105 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
106 }
107 
allocate(size_t size)108 void* JemallocNodumpAllocator::allocate(size_t size) {
109   return mallocx != nullptr ? mallocx(size, flags_) : malloc(size);
110 }
111 
reallocate(void * p,size_t size)112 void* JemallocNodumpAllocator::reallocate(void* p, size_t size) {
113   return rallocx != nullptr ? rallocx(p, size, flags_) : realloc(p, size);
114 }
115 
116 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
117 
118 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_CHUNK
119 chunk_alloc_t* JemallocNodumpAllocator::original_alloc_ = nullptr;
alloc(void * chunk,size_t size,size_t alignment,bool * zero,bool * commit,unsigned arena_ind)120 void* JemallocNodumpAllocator::alloc(
121     void* chunk,
122 #else
123 extent_hooks_t JemallocNodumpAllocator::extent_hooks_;
124 extent_alloc_t* JemallocNodumpAllocator::original_alloc_ = nullptr;
125 void* JemallocNodumpAllocator::alloc(
126     extent_hooks_t* extent,
127     void* new_addr,
128 #endif
129     size_t size,
130     size_t alignment,
131     bool* zero,
132     bool* commit,
133     unsigned arena_ind) {
134   void* result = original_alloc_(
135       JEMALLOC_CHUNK_OR_EXTENT,
136 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_EXTENT
137       new_addr,
138 #endif
139       size,
140       alignment,
141       zero,
142       commit,
143       arena_ind);
144   if (result != nullptr) {
145     if (auto ret = madvise(result, size, MADV_DONTDUMP)) {
146       VLOG(1) << "Unable to madvise(MADV_DONTDUMP): " << errnoStr(ret);
147     }
148   }
149 
150   return result;
151 }
152 
153 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
154 
deallocate(void * p,size_t)155 void JemallocNodumpAllocator::deallocate(void* p, size_t) {
156   dallocx != nullptr ? dallocx(p, flags_) : free(p);
157 }
158 
deallocate(void * p,void * userData)159 void JemallocNodumpAllocator::deallocate(void* p, void* userData) {
160   const auto flags = reinterpret_cast<uint64_t>(userData);
161   dallocx != nullptr ? dallocx(p, static_cast<int>(flags)) : free(p);
162 }
163 
globalJemallocNodumpAllocator()164 JemallocNodumpAllocator& globalJemallocNodumpAllocator() {
165   static auto instance = new JemallocNodumpAllocator();
166   return *instance;
167 }
168 
169 } // namespace folly
170