MCPcopy Create free account
hub / github.com/apache/mesos / detectJemalloc

Function detectJemalloc

3rdparty/libprocess/src/memory_profiler.cpp:145–187  ·  view source on GitHub ↗

The `detectJemalloc()` function below was taken from the folly library (called `usingJEMalloc()` there), originally distributed by Facebook, Inc under the Apache License. It checks whether jemalloc is used as the current malloc implementation by allocating one byte and checking if the threads allocation counter increases. This requires jemalloc to have been compiled with `--enable-stats`.

Source from the content-addressed store, hash-verified

143// allocating one byte and checking if the threads allocation counter increases.
144// This requires jemalloc to have been compiled with `--enable-stats`.
145bool detectJemalloc() noexcept {
146#ifndef LIBPROCESS_ALLOW_JEMALLOC
147 return false;
148#else
149 static const bool result = [] () noexcept {
150 // Some platforms (*cough* OSX *cough*) require weak symbol checks to be
151 // in the form if (mallctl != nullptr). Not if (mallctl) or if (!mallctl)
152 // (!!). http://goo.gl/xpmctm
153 if (mallctl == nullptr || malloc_stats_print == nullptr) {
154 return false;
155 }
156
157 // "volatile" because gcc optimizes out the reads from *counter, because
158 // it "knows" malloc doesn't modify global state...
159 volatile uint64_t* counter;
160 size_t counterLen = sizeof(uint64_t*);
161
162 if (mallctl("thread.allocatedp", static_cast<void*>(&counter), &counterLen,
163 nullptr, 0) != 0) {
164 return false;
165 }
166
167 if (counterLen != sizeof(uint64_t*)) {
168 return false;
169 }
170
171 uint64_t origAllocated = *counter;
172
173 // Static because otherwise clever compilers will find out that
174 // the ptr is not used and does not escape the scope, so they will
175 // just optimize away the malloc.
176 static const void* ptr = malloc(1);
177 if (!ptr) {
178 // wtf, failing to allocate 1 byte
179 return false;
180 }
181
182 return (origAllocated != *counter);
183 }();
184
185 return result;
186#endif
187}
188
189
190template<typename T>

Callers 8

readJemallocSettingFunction · 0.85
updateJemallocSettingFunction · 0.85
writeJemallocSettingFunction · 0.85
startMethod · 0.85
stopMethod · 0.85
statisticsMethod · 0.85
stateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected