1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Author: Sanjay Ghemawat <opensource@google.com>
37 #if defined HAVE_STDINT_H
39 #elif defined HAVE_INTTYPES_H
42 #include <sys/types.h>
45 #include "base/dynamic_annotations.h"
46 #include "base/sysinfo.h" // for FillProcSelfMaps
48 #include "gperftools/heap-checker.h"
50 #include "gperftools/malloc_extension.h"
51 #include "gperftools/malloc_extension_c.h"
52 #include "maybe_threads.h"
54 using STL_NAMESPACE::string
;
55 using STL_NAMESPACE::vector
;
57 static void DumpAddressMap(string
* result
) {
58 *result
+= "\nMAPPED_LIBRARIES:\n";
59 // We keep doubling until we get a fit
60 const size_t old_resultlen
= result
->size();
61 for (int amap_size
= 10240; amap_size
< 10000000; amap_size
*= 2) {
62 result
->resize(old_resultlen
+ amap_size
);
63 bool wrote_all
= false;
64 const int bytes_written
=
65 tcmalloc::FillProcSelfMaps(&((*result
)[old_resultlen
]), amap_size
,
67 if (wrote_all
) { // we fit!
68 (*result
)[old_resultlen
+ bytes_written
] = '\0';
69 result
->resize(old_resultlen
+ bytes_written
);
73 result
->reserve(old_resultlen
); // just don't print anything
76 // Note: this routine is meant to be called before threads are spawned.
77 void MallocExtension::Initialize() {
78 static bool initialize_called
= false;
80 if (initialize_called
) return;
81 initialize_called
= true;
84 // GNU libc++ versions 3.3 and 3.4 obey the environment variables
85 // GLIBCPP_FORCE_NEW and GLIBCXX_FORCE_NEW respectively. Setting
86 // one of these variables forces the STL default allocator to call
87 // new() or delete() for each allocation or deletion. Otherwise
88 // the STL allocator tries to avoid the high cost of doing
89 // allocations by pooling memory internally. However, tcmalloc
90 // does allocations really fast, especially for the types of small
91 // items one sees in STL, so it's better off just using us.
92 // TODO: control whether we do this via an environment variable?
93 setenv("GLIBCPP_FORCE_NEW", "1", false /* no overwrite*/);
94 setenv("GLIBCXX_FORCE_NEW", "1", false /* no overwrite*/);
96 // Now we need to make the setenv 'stick', which it may not do since
97 // the env is flakey before main() is called. But luckily stl only
98 // looks at this env var the first time it tries to do an alloc, and
99 // caches what it finds. So we just cause an stl alloc here.
100 string
dummy("I need to be allocated");
101 dummy
+= "!"; // so the definition of dummy isn't optimized out
102 #endif /* __GLIBC__ */
105 // SysAllocator implementation
106 SysAllocator::~SysAllocator() {}
108 // Default implementation -- does nothing
109 MallocExtension::~MallocExtension() { }
110 bool MallocExtension::VerifyAllMemory() { return true; }
111 bool MallocExtension::VerifyNewMemory(const void* p
) { return true; }
112 bool MallocExtension::VerifyArrayNewMemory(const void* p
) { return true; }
113 bool MallocExtension::VerifyMallocMemory(const void* p
) { return true; }
115 bool MallocExtension::GetNumericProperty(const char* property
, size_t* value
) {
119 bool MallocExtension::SetNumericProperty(const char* property
, size_t value
) {
123 void MallocExtension::GetStats(char* buffer
, int length
) {
128 bool MallocExtension::MallocMemoryStats(int* blocks
, size_t* total
,
129 int histogram
[kMallocHistogramSize
]) {
132 memset(histogram
, 0, sizeof(*histogram
) * kMallocHistogramSize
);
136 void** MallocExtension::ReadStackTraces(int* sample_period
) {
140 void** MallocExtension::ReadHeapGrowthStackTraces() {
144 void MallocExtension::MarkThreadIdle() {
145 // Default implementation does nothing
148 void MallocExtension::MarkThreadBusy() {
149 // Default implementation does nothing
152 SysAllocator
* MallocExtension::GetSystemAllocator() {
156 void MallocExtension::SetSystemAllocator(SysAllocator
*a
) {
157 // Default implementation does nothing
160 void MallocExtension::ReleaseToSystem(size_t num_bytes
) {
161 // Default implementation does nothing
164 void MallocExtension::ReleaseFreeMemory() {
165 ReleaseToSystem(static_cast<size_t>(-1)); // SIZE_T_MAX
168 void MallocExtension::SetMemoryReleaseRate(double rate
) {
169 // Default implementation does nothing
172 double MallocExtension::GetMemoryReleaseRate() {
176 size_t MallocExtension::GetEstimatedAllocatedSize(size_t size
) {
180 size_t MallocExtension::GetAllocatedSize(const void* p
) {
181 assert(GetOwnership(p
) != kNotOwned
);
185 MallocExtension::Ownership
MallocExtension::GetOwnership(const void* p
) {
186 return kUnknownOwnership
;
189 void MallocExtension::GetFreeListSizes(
190 vector
<MallocExtension::FreeListInfo
>* v
) {
194 // The current malloc extension object.
196 static pthread_once_t module_init
= PTHREAD_ONCE_INIT
;
197 static MallocExtension
* current_instance
= NULL
;
199 static void InitModule() {
200 current_instance
= new MallocExtension
;
201 #ifndef NO_HEAP_CHECK
202 HeapLeakChecker::IgnoreObject(current_instance
);
206 MallocExtension
* MallocExtension::instance() {
207 perftools_pthread_once(&module_init
, InitModule
);
208 return current_instance
;
211 void MallocExtension::Register(MallocExtension
* implementation
) {
212 perftools_pthread_once(&module_init
, InitModule
);
213 // When running under valgrind, our custom malloc is replaced with
214 // valgrind's one and malloc extensions will not work. (Note:
215 // callers should be responsible for checking that they are the
216 // malloc that is really being run, before calling Register. This
217 // is just here as an extra sanity check.)
218 if (!RunningOnValgrind()) {
219 current_instance
= implementation
;
223 // -----------------------------------------------------------------------
224 // Heap sampling support
225 // -----------------------------------------------------------------------
230 uintptr_t Count(void** entry
) {
231 return reinterpret_cast<uintptr_t>(entry
[0]);
233 uintptr_t Size(void** entry
) {
234 return reinterpret_cast<uintptr_t>(entry
[1]);
236 uintptr_t Depth(void** entry
) {
237 return reinterpret_cast<uintptr_t>(entry
[2]);
239 void* PC(void** entry
, int i
) {
243 void PrintCountAndSize(MallocExtensionWriter
* writer
,
244 uintptr_t count
, uintptr_t size
) {
246 snprintf(buf
, sizeof(buf
),
247 "%6"PRIu64
": %8"PRIu64
" [%6"PRIu64
": %8"PRIu64
"] @",
248 static_cast<uint64
>(count
),
249 static_cast<uint64
>(size
),
250 static_cast<uint64
>(count
),
251 static_cast<uint64
>(size
));
252 writer
->append(buf
, strlen(buf
));
255 void PrintHeader(MallocExtensionWriter
* writer
,
256 const char* label
, void** entries
) {
257 // Compute the total count and total size
258 uintptr_t total_count
= 0;
259 uintptr_t total_size
= 0;
260 for (void** entry
= entries
; Count(entry
) != 0; entry
+= 3 + Depth(entry
)) {
261 total_count
+= Count(entry
);
262 total_size
+= Size(entry
);
265 const char* const kTitle
= "heap profile: ";
266 writer
->append(kTitle
, strlen(kTitle
));
267 PrintCountAndSize(writer
, total_count
, total_size
);
268 writer
->append(" ", 1);
269 writer
->append(label
, strlen(label
));
270 writer
->append("\n", 1);
273 void PrintStackEntry(MallocExtensionWriter
* writer
, void** entry
) {
274 PrintCountAndSize(writer
, Count(entry
), Size(entry
));
276 for (int i
= 0; i
< Depth(entry
); i
++) {
278 snprintf(buf
, sizeof(buf
), " %p", PC(entry
, i
));
279 writer
->append(buf
, strlen(buf
));
281 writer
->append("\n", 1);
286 void MallocExtension::GetHeapSample(MallocExtensionWriter
* writer
) {
287 int sample_period
= 0;
288 void** entries
= ReadStackTraces(&sample_period
);
289 if (entries
== NULL
) {
290 const char* const kErrorMsg
=
291 "This malloc implementation does not support sampling.\n"
292 "As of 2005/01/26, only tcmalloc supports sampling, and\n"
293 "you are probably running a binary that does not use\n"
295 writer
->append(kErrorMsg
, strlen(kErrorMsg
));
300 sprintf(label
, "heap_v2/%d", sample_period
);
301 PrintHeader(writer
, label
, entries
);
302 for (void** entry
= entries
; Count(entry
) != 0; entry
+= 3 + Depth(entry
)) {
303 PrintStackEntry(writer
, entry
);
307 DumpAddressMap(writer
);
310 void MallocExtension::GetHeapGrowthStacks(MallocExtensionWriter
* writer
) {
311 void** entries
= ReadHeapGrowthStackTraces();
312 if (entries
== NULL
) {
313 const char* const kErrorMsg
=
314 "This malloc implementation does not support "
315 "ReadHeapGrowthStackTraces().\n"
316 "As of 2005/09/27, only tcmalloc supports this, and you\n"
317 "are probably running a binary that does not use tcmalloc.\n";
318 writer
->append(kErrorMsg
, strlen(kErrorMsg
));
322 // Do not canonicalize the stack entries, so that we get a
323 // time-ordered list of stack traces, which may be useful if the
324 // client wants to focus on the latest stack traces.
325 PrintHeader(writer
, "growth", entries
);
326 for (void** entry
= entries
; Count(entry
) != 0; entry
+= 3 + Depth(entry
)) {
327 PrintStackEntry(writer
, entry
);
331 DumpAddressMap(writer
);
334 void MallocExtension::Ranges(void* arg
, RangeFunction func
) {
335 // No callbacks by default
338 // These are C shims that work on the current instance.
340 #define C_SHIM(fn, retval, paramlist, arglist) \
341 extern "C" PERFTOOLS_DLL_DECL retval MallocExtension_##fn paramlist { \
342 return MallocExtension::instance()->fn arglist; \
345 C_SHIM(VerifyAllMemory
, int, (void), ());
346 C_SHIM(VerifyNewMemory
, int, (const void* p
), (p
));
347 C_SHIM(VerifyArrayNewMemory
, int, (const void* p
), (p
));
348 C_SHIM(VerifyMallocMemory
, int, (const void* p
), (p
));
349 C_SHIM(MallocMemoryStats
, int,
350 (int* blocks
, size_t* total
, int histogram
[kMallocHistogramSize
]),
351 (blocks
, total
, histogram
));
353 C_SHIM(GetStats
, void,
354 (char* buffer
, int buffer_length
), (buffer
, buffer_length
));
355 C_SHIM(GetNumericProperty
, int,
356 (const char* property
, size_t* value
), (property
, value
));
357 C_SHIM(SetNumericProperty
, int,
358 (const char* property
, size_t value
), (property
, value
));
360 C_SHIM(MarkThreadIdle
, void, (void), ());
361 C_SHIM(MarkThreadBusy
, void, (void), ());
362 C_SHIM(ReleaseFreeMemory
, void, (void), ());
363 C_SHIM(ReleaseToSystem
, void, (size_t num_bytes
), (num_bytes
));
364 C_SHIM(GetEstimatedAllocatedSize
, size_t, (size_t size
), (size
));
365 C_SHIM(GetAllocatedSize
, size_t, (const void* p
), (p
));
367 // Can't use the shim here because of the need to translate the enums.
369 MallocExtension_Ownership
MallocExtension_GetOwnership(const void* p
) {
370 return static_cast<MallocExtension_Ownership
>(
371 MallocExtension::instance()->GetOwnership(p
));