1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 // Copyright (c) 2005, Google Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // ---
32 // Author: Sanjay Ghemawat <opensource@google.com>
33 
34 #include <config.h>
35 #include <assert.h>
36 #include <string.h>
37 #include <stdio.h>
38 #if defined HAVE_STDINT_H
39 #include <stdint.h>
40 #elif defined HAVE_INTTYPES_H
41 #include <inttypes.h>
42 #else
43 #include <sys/types.h>
44 #endif
45 #include <string>
46 #include "base/dynamic_annotations.h"
47 #include "base/sysinfo.h"    // for FillProcSelfMaps
48 #ifndef NO_HEAP_CHECK
49 #include "gperftools/heap-checker.h"
50 #endif
51 #include "gperftools/malloc_extension.h"
52 #include "gperftools/malloc_extension_c.h"
53 #include "maybe_threads.h"
54 #include "base/googleinit.h"
55 
56 using std::string;
57 using std::vector;
58 
DumpAddressMap(string * result)59 static void DumpAddressMap(string* result) {
60   *result += "\nMAPPED_LIBRARIES:\n";
61   // We keep doubling until we get a fit
62   const size_t old_resultlen = result->size();
63   for (int amap_size = 10240; amap_size < 10000000; amap_size *= 2) {
64     result->resize(old_resultlen + amap_size);
65     bool wrote_all = false;
66     const int bytes_written =
67         tcmalloc::FillProcSelfMaps(&((*result)[old_resultlen]), amap_size,
68                                    &wrote_all);
69     if (wrote_all) {   // we fit!
70       (*result)[old_resultlen + bytes_written] = '\0';
71       result->resize(old_resultlen + bytes_written);
72       return;
73     }
74   }
75   result->reserve(old_resultlen);   // just don't print anything
76 }
77 
78 // Note: this routine is meant to be called before threads are spawned.
Initialize()79 void MallocExtension::Initialize() {
80   static bool initialize_called = false;
81 
82   if (initialize_called) return;
83   initialize_called = true;
84 
85 #ifdef __GLIBC__
86   // GNU libc++ versions 3.3 and 3.4 obey the environment variables
87   // GLIBCPP_FORCE_NEW and GLIBCXX_FORCE_NEW respectively.  Setting
88   // one of these variables forces the STL default allocator to call
89   // new() or delete() for each allocation or deletion.  Otherwise
90   // the STL allocator tries to avoid the high cost of doing
91   // allocations by pooling memory internally.  However, tcmalloc
92   // does allocations really fast, especially for the types of small
93   // items one sees in STL, so it's better off just using us.
94   // TODO: control whether we do this via an environment variable?
95   setenv("GLIBCPP_FORCE_NEW", "1", false /* no overwrite*/);
96   setenv("GLIBCXX_FORCE_NEW", "1", false /* no overwrite*/);
97 
98   // Now we need to make the setenv 'stick', which it may not do since
99   // the env is flakey before main() is called.  But luckily stl only
100   // looks at this env var the first time it tries to do an alloc, and
101   // caches what it finds.  So we just cause an stl alloc here.
102   string dummy("I need to be allocated");
103   dummy += "!";         // so the definition of dummy isn't optimized out
104 #endif  /* __GLIBC__ */
105 }
106 
107 // SysAllocator implementation
~SysAllocator()108 SysAllocator::~SysAllocator() {}
109 
110 // Default implementation -- does nothing
~MallocExtension()111 MallocExtension::~MallocExtension() { }
VerifyAllMemory()112 bool MallocExtension::VerifyAllMemory() { return true; }
VerifyNewMemory(const void * p)113 bool MallocExtension::VerifyNewMemory(const void* p) { return true; }
VerifyArrayNewMemory(const void * p)114 bool MallocExtension::VerifyArrayNewMemory(const void* p) { return true; }
VerifyMallocMemory(const void * p)115 bool MallocExtension::VerifyMallocMemory(const void* p) { return true; }
116 
GetNumericProperty(const char * property,size_t * value)117 bool MallocExtension::GetNumericProperty(const char* property, size_t* value) {
118   return false;
119 }
120 
SetNumericProperty(const char * property,size_t value)121 bool MallocExtension::SetNumericProperty(const char* property, size_t value) {
122   return false;
123 }
124 
GetStats(char * buffer,int length)125 void MallocExtension::GetStats(char* buffer, int length) {
126   assert(length > 0);
127   buffer[0] = '\0';
128 }
129 
MallocMemoryStats(int * blocks,size_t * total,int histogram[kMallocHistogramSize])130 bool MallocExtension::MallocMemoryStats(int* blocks, size_t* total,
131                                        int histogram[kMallocHistogramSize]) {
132   *blocks = 0;
133   *total = 0;
134   memset(histogram, 0, sizeof(*histogram) * kMallocHistogramSize);
135   return true;
136 }
137 
ReadStackTraces(int * sample_period)138 void** MallocExtension::ReadStackTraces(int* sample_period) {
139   return NULL;
140 }
141 
ReadHeapGrowthStackTraces()142 void** MallocExtension::ReadHeapGrowthStackTraces() {
143   return NULL;
144 }
145 
MarkThreadIdle()146 void MallocExtension::MarkThreadIdle() {
147   // Default implementation does nothing
148 }
149 
MarkThreadBusy()150 void MallocExtension::MarkThreadBusy() {
151   // Default implementation does nothing
152 }
153 
GetSystemAllocator()154 SysAllocator* MallocExtension::GetSystemAllocator() {
155   return NULL;
156 }
157 
SetSystemAllocator(SysAllocator * a)158 void MallocExtension::SetSystemAllocator(SysAllocator *a) {
159   // Default implementation does nothing
160 }
161 
ReleaseToSystem(size_t num_bytes)162 void MallocExtension::ReleaseToSystem(size_t num_bytes) {
163   // Default implementation does nothing
164 }
165 
ReleaseFreeMemory()166 void MallocExtension::ReleaseFreeMemory() {
167   ReleaseToSystem(static_cast<size_t>(-1));   // SIZE_T_MAX
168 }
169 
SetMemoryReleaseRate(double rate)170 void MallocExtension::SetMemoryReleaseRate(double rate) {
171   // Default implementation does nothing
172 }
173 
GetMemoryReleaseRate()174 double MallocExtension::GetMemoryReleaseRate() {
175   return -1.0;
176 }
177 
GetEstimatedAllocatedSize(size_t size)178 size_t MallocExtension::GetEstimatedAllocatedSize(size_t size) {
179   return size;
180 }
181 
GetAllocatedSize(const void * p)182 size_t MallocExtension::GetAllocatedSize(const void* p) {
183   assert(GetOwnership(p) != kNotOwned);
184   return 0;
185 }
186 
GetOwnership(const void * p)187 MallocExtension::Ownership MallocExtension::GetOwnership(const void* p) {
188   return kUnknownOwnership;
189 }
190 
GetFreeListSizes(vector<MallocExtension::FreeListInfo> * v)191 void MallocExtension::GetFreeListSizes(
192     vector<MallocExtension::FreeListInfo>* v) {
193   v->clear();
194 }
195 
GetThreadCacheSize()196 size_t MallocExtension::GetThreadCacheSize() {
197   return 0;
198 }
199 
MarkThreadTemporarilyIdle()200 void MallocExtension::MarkThreadTemporarilyIdle() {
201   // Default implementation does nothing
202 }
203 
204 // The current malloc extension object.
205 
206 static MallocExtension* current_instance;
207 
InitModule()208 static void InitModule() {
209   if (current_instance != NULL) {
210     return;
211   }
212   current_instance = new MallocExtension;
213 #ifndef NO_HEAP_CHECK
214   HeapLeakChecker::IgnoreObject(current_instance);
215 #endif
216 }
217 
REGISTER_MODULE_INITIALIZER(malloc_extension_init,InitModule ())218 REGISTER_MODULE_INITIALIZER(malloc_extension_init, InitModule())
219 
220 MallocExtension* MallocExtension::instance() {
221   InitModule();
222   return current_instance;
223 }
224 
Register(MallocExtension * implementation)225 void MallocExtension::Register(MallocExtension* implementation) {
226   InitModule();
227   // When running under valgrind, our custom malloc is replaced with
228   // valgrind's one and malloc extensions will not work.  (Note:
229   // callers should be responsible for checking that they are the
230   // malloc that is really being run, before calling Register.  This
231   // is just here as an extra sanity check.)
232   if (!RunningOnValgrind()) {
233     current_instance = implementation;
234   }
235 }
236 
237 // -----------------------------------------------------------------------
238 // Heap sampling support
239 // -----------------------------------------------------------------------
240 
241 namespace {
242 
243 // Accessors
Count(void ** entry)244 uintptr_t Count(void** entry) {
245   return reinterpret_cast<uintptr_t>(entry[0]);
246 }
Size(void ** entry)247 uintptr_t Size(void** entry) {
248   return reinterpret_cast<uintptr_t>(entry[1]);
249 }
Depth(void ** entry)250 uintptr_t Depth(void** entry) {
251   return reinterpret_cast<uintptr_t>(entry[2]);
252 }
PC(void ** entry,int i)253 void* PC(void** entry, int i) {
254   return entry[3+i];
255 }
256 
PrintCountAndSize(MallocExtensionWriter * writer,uintptr_t count,uintptr_t size)257 void PrintCountAndSize(MallocExtensionWriter* writer,
258                        uintptr_t count, uintptr_t size) {
259   char buf[100];
260   snprintf(buf, sizeof(buf),
261            "%6" PRIu64 ": %8" PRIu64 " [%6" PRIu64 ": %8" PRIu64 "] @",
262            static_cast<uint64>(count),
263            static_cast<uint64>(size),
264            static_cast<uint64>(count),
265            static_cast<uint64>(size));
266   writer->append(buf, strlen(buf));
267 }
268 
PrintHeader(MallocExtensionWriter * writer,const char * label,void ** entries)269 void PrintHeader(MallocExtensionWriter* writer,
270                  const char* label, void** entries) {
271   // Compute the total count and total size
272   uintptr_t total_count = 0;
273   uintptr_t total_size = 0;
274   for (void** entry = entries; Count(entry) != 0; entry += 3 + Depth(entry)) {
275     total_count += Count(entry);
276     total_size += Size(entry);
277   }
278 
279   const char* const kTitle = "heap profile: ";
280   writer->append(kTitle, strlen(kTitle));
281   PrintCountAndSize(writer, total_count, total_size);
282   writer->append(" ", 1);
283   writer->append(label, strlen(label));
284   writer->append("\n", 1);
285 }
286 
PrintStackEntry(MallocExtensionWriter * writer,void ** entry)287 void PrintStackEntry(MallocExtensionWriter* writer, void** entry) {
288   PrintCountAndSize(writer, Count(entry), Size(entry));
289 
290   for (int i = 0; i < Depth(entry); i++) {
291     char buf[32];
292     snprintf(buf, sizeof(buf), " %p", PC(entry, i));
293     writer->append(buf, strlen(buf));
294   }
295   writer->append("\n", 1);
296 }
297 
298 }
299 
GetHeapSample(MallocExtensionWriter * writer)300 void MallocExtension::GetHeapSample(MallocExtensionWriter* writer) {
301   int sample_period = 0;
302   void** entries = ReadStackTraces(&sample_period);
303   if (entries == NULL) {
304     const char* const kErrorMsg =
305         "This malloc implementation does not support sampling.\n"
306         "As of 2005/01/26, only tcmalloc supports sampling, and\n"
307         "you are probably running a binary that does not use\n"
308         "tcmalloc.\n";
309     writer->append(kErrorMsg, strlen(kErrorMsg));
310     return;
311   }
312 
313   char label[32];
314   sprintf(label, "heap_v2/%d", sample_period);
315   PrintHeader(writer, label, entries);
316   for (void** entry = entries; Count(entry) != 0; entry += 3 + Depth(entry)) {
317     PrintStackEntry(writer, entry);
318   }
319   delete[] entries;
320 
321   DumpAddressMap(writer);
322 }
323 
GetHeapGrowthStacks(MallocExtensionWriter * writer)324 void MallocExtension::GetHeapGrowthStacks(MallocExtensionWriter* writer) {
325   void** entries = ReadHeapGrowthStackTraces();
326   if (entries == NULL) {
327     const char* const kErrorMsg =
328         "This malloc implementation does not support "
329         "ReadHeapGrowthStackTraces().\n"
330         "As of 2005/09/27, only tcmalloc supports this, and you\n"
331         "are probably running a binary that does not use tcmalloc.\n";
332     writer->append(kErrorMsg, strlen(kErrorMsg));
333     return;
334   }
335 
336   // Do not canonicalize the stack entries, so that we get a
337   // time-ordered list of stack traces, which may be useful if the
338   // client wants to focus on the latest stack traces.
339   PrintHeader(writer, "growth", entries);
340   for (void** entry = entries; Count(entry) != 0; entry += 3 + Depth(entry)) {
341     PrintStackEntry(writer, entry);
342   }
343   delete[] entries;
344 
345   DumpAddressMap(writer);
346 }
347 
Ranges(void * arg,RangeFunction func)348 void MallocExtension::Ranges(void* arg, RangeFunction func) {
349   // No callbacks by default
350 }
351 
352 // These are C shims that work on the current instance.
353 
354 #define C_SHIM(fn, retval, paramlist, arglist)          \
355   extern "C" PERFTOOLS_DLL_DECL retval MallocExtension_##fn paramlist {    \
356     return MallocExtension::instance()->fn arglist;     \
357   }
358 
359 C_SHIM(VerifyAllMemory, int, (void), ());
360 C_SHIM(VerifyNewMemory, int, (const void* p), (p));
361 C_SHIM(VerifyArrayNewMemory, int, (const void* p), (p));
362 C_SHIM(VerifyMallocMemory, int, (const void* p), (p));
363 C_SHIM(MallocMemoryStats, int,
364        (int* blocks, size_t* total, int histogram[kMallocHistogramSize]),
365        (blocks, total, histogram));
366 
367 C_SHIM(GetStats, void,
368        (char* buffer, int buffer_length), (buffer, buffer_length));
369 C_SHIM(GetNumericProperty, int,
370        (const char* property, size_t* value), (property, value));
371 C_SHIM(SetNumericProperty, int,
372        (const char* property, size_t value), (property, value));
373 
374 C_SHIM(MarkThreadIdle, void, (void), ());
375 C_SHIM(MarkThreadBusy, void, (void), ());
376 C_SHIM(ReleaseFreeMemory, void, (void), ());
377 C_SHIM(ReleaseToSystem, void, (size_t num_bytes), (num_bytes));
378 C_SHIM(GetEstimatedAllocatedSize, size_t, (size_t size), (size));
379 C_SHIM(GetAllocatedSize, size_t, (const void* p), (p));
380 C_SHIM(GetThreadCacheSize, size_t, (void), ());
381 C_SHIM(MarkThreadTemporarilyIdle, void, (void), ());
382 
383 // Can't use the shim here because of the need to translate the enums.
384 extern "C"
MallocExtension_GetOwnership(const void * p)385 MallocExtension_Ownership MallocExtension_GetOwnership(const void* p) {
386   return static_cast<MallocExtension_Ownership>(
387       MallocExtension::instance()->GetOwnership(p));
388 }
389