malloc_extension.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. // Author: Sanjay Ghemawat <opensource@google.com>
  32. #include <config.h>
  33. #include <assert.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #if defined HAVE_STDINT_H
  37. #include <stdint.h>
  38. #elif defined HAVE_INTTYPES_H
  39. #include <inttypes.h>
  40. #else
  41. #include <sys/types.h>
  42. #endif
  43. #include <string>
  44. #include "base/dynamic_annotations.h"
  45. #ifndef TCMALLOC_SGX
  46. #include "base/sysinfo.h" // for FillProcSelfMaps
  47. #endif
  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. using STL_NAMESPACE::string;
  56. using STL_NAMESPACE::vector;
  57. #ifndef TCMALLOC_SGX
  58. static void DumpAddressMap(string* result) {
  59. *result += "\nMAPPED_LIBRARIES:\n";
  60. // We keep doubling until we get a fit
  61. const size_t old_resultlen = result->size();
  62. for (int amap_size = 10240; amap_size < 10000000; amap_size *= 2) {
  63. result->resize(old_resultlen + amap_size);
  64. bool wrote_all = false;
  65. const int bytes_written =
  66. tcmalloc::FillProcSelfMaps(&((*result)[old_resultlen]), amap_size,
  67. &wrote_all);
  68. if (wrote_all) { // we fit!
  69. (*result)[old_resultlen + bytes_written] = '\0';
  70. result->resize(old_resultlen + bytes_written);
  71. return;
  72. }
  73. }
  74. result->reserve(old_resultlen); // just don't print anything
  75. }
  76. #endif
  77. // Note: this routine is meant to be called before threads are spawned.
  78. void MallocExtension::Initialize() {
  79. static bool initialize_called = false;
  80. if (initialize_called) return;
  81. initialize_called = true;
  82. #ifdef __GLIBC__
  83. // GNU libc++ versions 3.3 and 3.4 obey the environment variables
  84. // GLIBCPP_FORCE_NEW and GLIBCXX_FORCE_NEW respectively. Setting
  85. // one of these variables forces the STL default allocator to call
  86. // new() or delete() for each allocation or deletion. Otherwise
  87. // the STL allocator tries to avoid the high cost of doing
  88. // allocations by pooling memory internally. However, tcmalloc
  89. // does allocations really fast, especially for the types of small
  90. // items one sees in STL, so it's better off just using us.
  91. // TODO: control whether we do this via an environment variable?
  92. setenv("GLIBCPP_FORCE_NEW", "1", false /* no overwrite*/);
  93. setenv("GLIBCXX_FORCE_NEW", "1", false /* no overwrite*/);
  94. // Now we need to make the setenv 'stick', which it may not do since
  95. // the env is flakey before main() is called. But luckily stl only
  96. // looks at this env var the first time it tries to do an alloc, and
  97. // caches what it finds. So we just cause an stl alloc here.
  98. string dummy("I need to be allocated");
  99. dummy += "!"; // so the definition of dummy isn't optimized out
  100. #endif /* __GLIBC__ */
  101. }
  102. // SysAllocator implementation
  103. SysAllocator::~SysAllocator() {}
  104. // Default implementation -- does nothing
  105. MallocExtension::~MallocExtension() { }
  106. bool MallocExtension::VerifyAllMemory() { return true; }
  107. bool MallocExtension::VerifyNewMemory(const void* p) { return true; }
  108. bool MallocExtension::VerifyArrayNewMemory(const void* p) { return true; }
  109. bool MallocExtension::VerifyMallocMemory(const void* p) { return true; }
  110. bool MallocExtension::GetNumericProperty(const char* property, size_t* value) {
  111. return false;
  112. }
  113. bool MallocExtension::SetNumericProperty(const char* property, size_t value) {
  114. return false;
  115. }
  116. void MallocExtension::GetStats(char* buffer, int length) {
  117. assert(length > 0);
  118. buffer[0] = '\0';
  119. }
  120. bool MallocExtension::MallocMemoryStats(int* blocks, size_t* total,
  121. int histogram[kMallocHistogramSize]) {
  122. *blocks = 0;
  123. *total = 0;
  124. memset(histogram, 0, sizeof(*histogram) * kMallocHistogramSize);
  125. return true;
  126. }
  127. void** MallocExtension::ReadStackTraces(int* sample_period) {
  128. return NULL;
  129. }
  130. void** MallocExtension::ReadHeapGrowthStackTraces() {
  131. return NULL;
  132. }
  133. void MallocExtension::MarkThreadIdle() {
  134. // Default implementation does nothing
  135. }
  136. void MallocExtension::MarkThreadBusy() {
  137. // Default implementation does nothing
  138. }
  139. SysAllocator* MallocExtension::GetSystemAllocator() {
  140. return NULL;
  141. }
  142. void MallocExtension::SetSystemAllocator(SysAllocator *a) {
  143. // Default implementation does nothing
  144. }
  145. void MallocExtension::ReleaseToSystem(size_t num_bytes) {
  146. // Default implementation does nothing
  147. }
  148. void MallocExtension::ReleaseFreeMemory() {
  149. ReleaseToSystem(static_cast<size_t>(-1)); // SIZE_T_MAX
  150. }
  151. void MallocExtension::SetMemoryReleaseRate(double rate) {
  152. // Default implementation does nothing
  153. }
  154. double MallocExtension::GetMemoryReleaseRate() {
  155. return -1.0;
  156. }
  157. size_t MallocExtension::GetEstimatedAllocatedSize(size_t size) {
  158. return size;
  159. }
  160. size_t MallocExtension::GetAllocatedSize(const void* p) {
  161. assert(GetOwnership(p) != kNotOwned);
  162. return 0;
  163. }
  164. MallocExtension::Ownership MallocExtension::GetOwnership(const void* p) {
  165. return kUnknownOwnership;
  166. }
  167. void MallocExtension::GetFreeListSizes(
  168. vector<MallocExtension::FreeListInfo>* v) {
  169. v->clear();
  170. }
  171. size_t MallocExtension::GetThreadCacheSize() {
  172. return 0;
  173. }
  174. void MallocExtension::MarkThreadTemporarilyIdle() {
  175. // Default implementation does nothing
  176. }
  177. // The current malloc extension object.
  178. static MallocExtension* current_instance;
  179. static void InitModule() {
  180. if (current_instance != NULL) {
  181. return;
  182. }
  183. current_instance = new MallocExtension;
  184. #ifndef NO_HEAP_CHECK
  185. HeapLeakChecker::IgnoreObject(current_instance);
  186. #endif
  187. }
  188. REGISTER_MODULE_INITIALIZER(malloc_extension_init, InitModule())
  189. MallocExtension* MallocExtension::instance() {
  190. InitModule();
  191. return current_instance;
  192. }
  193. void MallocExtension::Register(MallocExtension* implementation) {
  194. InitModule();
  195. // When running under valgrind, our custom malloc is replaced with
  196. // valgrind's one and malloc extensions will not work. (Note:
  197. // callers should be responsible for checking that they are the
  198. // malloc that is really being run, before calling Register. This
  199. // is just here as an extra sanity check.)
  200. if (!RunningOnValgrind()) {
  201. current_instance = implementation;
  202. }
  203. }
  204. // -----------------------------------------------------------------------
  205. // Heap sampling support
  206. // -----------------------------------------------------------------------
  207. #ifndef TCMALLOC_SGX
  208. namespace {
  209. // Accessors
  210. uintptr_t Count(void** entry) {
  211. return reinterpret_cast<uintptr_t>(entry[0]);
  212. }
  213. uintptr_t Size(void** entry) {
  214. return reinterpret_cast<uintptr_t>(entry[1]);
  215. }
  216. uintptr_t Depth(void** entry) {
  217. return reinterpret_cast<uintptr_t>(entry[2]);
  218. }
  219. void* PC(void** entry, int i) {
  220. return entry[3+i];
  221. }
  222. void PrintCountAndSize(MallocExtensionWriter* writer,
  223. uintptr_t count, uintptr_t size) {
  224. char buf[100];
  225. snprintf(buf, sizeof(buf),
  226. "%6" PRIu64 ": %8" PRIu64 " [%6" PRIu64 ": %8" PRIu64 "] @",
  227. static_cast<uint64>(count),
  228. static_cast<uint64>(size),
  229. static_cast<uint64>(count),
  230. static_cast<uint64>(size));
  231. writer->append(buf, strlen(buf));
  232. }
  233. void PrintHeader(MallocExtensionWriter* writer,
  234. const char* label, void** entries) {
  235. // Compute the total count and total size
  236. uintptr_t total_count = 0;
  237. uintptr_t total_size = 0;
  238. for (void** entry = entries; Count(entry) != 0; entry += 3 + Depth(entry)) {
  239. total_count += Count(entry);
  240. total_size += Size(entry);
  241. }
  242. const char* const kTitle = "heap profile: ";
  243. writer->append(kTitle, strlen(kTitle));
  244. PrintCountAndSize(writer, total_count, total_size);
  245. writer->append(" ", 1);
  246. writer->append(label, strlen(label));
  247. writer->append("\n", 1);
  248. }
  249. void PrintStackEntry(MallocExtensionWriter* writer, void** entry) {
  250. PrintCountAndSize(writer, Count(entry), Size(entry));
  251. for (int i = 0; i < Depth(entry); i++) {
  252. char buf[32];
  253. snprintf(buf, sizeof(buf), " %p", PC(entry, i));
  254. writer->append(buf, strlen(buf));
  255. }
  256. writer->append("\n", 1);
  257. }
  258. }
  259. #endif
  260. void MallocExtension::GetHeapSample(MallocExtensionWriter* writer) {
  261. /*Disable in SGX*/
  262. #ifndef TCMALLOC_SGX
  263. int sample_period = 0;
  264. void** entries = ReadStackTraces(&sample_period);
  265. if (entries == NULL) {
  266. const char* const kErrorMsg =
  267. "This malloc implementation does not support sampling.\n"
  268. "As of 2005/01/26, only tcmalloc supports sampling, and\n"
  269. "you are probably running a binary that does not use\n"
  270. "tcmalloc.\n";
  271. writer->append(kErrorMsg, strlen(kErrorMsg));
  272. return;
  273. }
  274. char label[32];
  275. sprintf(label, "heap_v2/%d", sample_period);
  276. PrintHeader(writer, label, entries);
  277. for (void** entry = entries; Count(entry) != 0; entry += 3 + Depth(entry)) {
  278. PrintStackEntry(writer, entry);
  279. }
  280. delete[] entries;
  281. DumpAddressMap(writer);
  282. #endif
  283. }
  284. void MallocExtension::GetHeapGrowthStacks(MallocExtensionWriter* writer) {
  285. /*disabled in SGX*/
  286. #ifndef TCMALLOC_SGX
  287. void** entries = ReadHeapGrowthStackTraces();
  288. if (entries == NULL) {
  289. const char* const kErrorMsg =
  290. "This malloc implementation does not support "
  291. "ReadHeapGrowthStackTraces().\n"
  292. "As of 2005/09/27, only tcmalloc supports this, and you\n"
  293. "are probably running a binary that does not use tcmalloc.\n";
  294. writer->append(kErrorMsg, strlen(kErrorMsg));
  295. return;
  296. }
  297. // Do not canonicalize the stack entries, so that we get a
  298. // time-ordered list of stack traces, which may be useful if the
  299. // client wants to focus on the latest stack traces.
  300. PrintHeader(writer, "growth", entries);
  301. for (void** entry = entries; Count(entry) != 0; entry += 3 + Depth(entry)) {
  302. PrintStackEntry(writer, entry);
  303. }
  304. delete[] entries;
  305. DumpAddressMap(writer);
  306. #endif
  307. }
  308. void MallocExtension::Ranges(void* arg, RangeFunction func) {
  309. // No callbacks by default
  310. }
  311. // These are C shims that work on the current instance.
  312. #define C_SHIM(fn, retval, paramlist, arglist) \
  313. extern "C" PERFTOOLS_DLL_DECL retval MallocExtension_##fn paramlist { \
  314. return MallocExtension::instance()->fn arglist; \
  315. }
  316. C_SHIM(VerifyAllMemory, int, (void), ());
  317. C_SHIM(VerifyNewMemory, int, (const void* p), (p));
  318. C_SHIM(VerifyArrayNewMemory, int, (const void* p), (p));
  319. C_SHIM(VerifyMallocMemory, int, (const void* p), (p));
  320. C_SHIM(MallocMemoryStats, int,
  321. (int* blocks, size_t* total, int histogram[kMallocHistogramSize]),
  322. (blocks, total, histogram));
  323. C_SHIM(GetStats, void,
  324. (char* buffer, int buffer_length), (buffer, buffer_length));
  325. C_SHIM(GetNumericProperty, int,
  326. (const char* property, size_t* value), (property, value));
  327. C_SHIM(SetNumericProperty, int,
  328. (const char* property, size_t value), (property, value));
  329. C_SHIM(MarkThreadIdle, void, (void), ());
  330. C_SHIM(MarkThreadBusy, void, (void), ());
  331. C_SHIM(ReleaseFreeMemory, void, (void), ());
  332. C_SHIM(ReleaseToSystem, void, (size_t num_bytes), (num_bytes));
  333. C_SHIM(GetEstimatedAllocatedSize, size_t, (size_t size), (size));
  334. C_SHIM(GetAllocatedSize, size_t, (const void* p), (p));
  335. C_SHIM(GetThreadCacheSize, size_t, (void), ());
  336. C_SHIM(MarkThreadTemporarilyIdle, void, (void), ());
  337. // Can't use the shim here because of the need to translate the enums.
  338. extern "C"
  339. MallocExtension_Ownership MallocExtension_GetOwnership(const void* p) {
  340. return static_cast<MallocExtension_Ownership>(
  341. MallocExtension::instance()->GetOwnership(p));
  342. }