static_vars.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2008, 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: Ken Ashcraft <opensource@google.com>
  32. #include <config.h>
  33. #include "static_vars.h"
  34. #include <stddef.h> // for NULL
  35. #include <new> // for operator new
  36. #ifdef HAVE_PTHREAD
  37. #include <pthread.h> // for pthread_atfork
  38. #endif
  39. #include "internal_logging.h" // for CHECK_CONDITION
  40. #include "common.h"
  41. #include "sampler.h" // for Sampler
  42. #include "getenv_safe.h" // TCMallocGetenvSafe
  43. #include "base/googleinit.h"
  44. #include "maybe_threads.h"
  45. namespace tcmalloc {
  46. #if defined(HAVE_FORK) && defined(HAVE_PTHREAD)
  47. // These following two functions are registered via pthread_atfork to make
  48. // sure the central_cache locks remain in a consisten state in the forked
  49. // version of the thread.
  50. void CentralCacheLockAll()
  51. {
  52. Static::pageheap_lock()->Lock();
  53. for (int i = 0; i < kNumClasses; ++i)
  54. Static::central_cache()[i].Lock();
  55. }
  56. void CentralCacheUnlockAll()
  57. {
  58. for (int i = 0; i < kNumClasses; ++i)
  59. Static::central_cache()[i].Unlock();
  60. Static::pageheap_lock()->Unlock();
  61. }
  62. #endif
  63. SpinLock Static::pageheap_lock_(SpinLock::LINKER_INITIALIZED);
  64. SizeMap Static::sizemap_;
  65. CentralFreeListPadded Static::central_cache_[kNumClasses];
  66. PageHeapAllocator<Span> Static::span_allocator_;
  67. PageHeapAllocator<StackTrace> Static::stacktrace_allocator_;
  68. Span Static::sampled_objects_;
  69. PageHeapAllocator<StackTraceTable::Bucket> Static::bucket_allocator_;
  70. StackTrace* Static::growth_stacks_ = NULL;
  71. PageHeap* Static::pageheap_ = NULL;
  72. void Static::InitStaticVars() {
  73. sizemap_.Init();
  74. span_allocator_.Init();
  75. span_allocator_.New(); // Reduce cache conflicts
  76. span_allocator_.New(); // Reduce cache conflicts
  77. stacktrace_allocator_.Init();
  78. bucket_allocator_.Init();
  79. // Do a bit of sanitizing: make sure central_cache is aligned properly
  80. CHECK_CONDITION((sizeof(central_cache_[0]) % 64) == 0);
  81. for (int i = 0; i < kNumClasses; ++i) {
  82. central_cache_[i].Init(i);
  83. }
  84. // It's important to have PageHeap allocated, not in static storage,
  85. // so that HeapLeakChecker does not consider all the byte patterns stored
  86. // in is caches as pointers that are sources of heap object liveness,
  87. // which leads to it missing some memory leaks.
  88. pageheap_ = new (MetaDataAlloc(sizeof(PageHeap))) PageHeap;
  89. bool aggressive_decommit =
  90. tcmalloc::commandlineflags::StringToBool(
  91. TCMallocGetenvSafe("TCMALLOC_AGGRESSIVE_DECOMMIT"), true);
  92. pageheap_->SetAggressiveDecommit(aggressive_decommit);
  93. DLL_Init(&sampled_objects_);
  94. Sampler::InitStatics();
  95. }
  96. #if defined(HAVE_FORK) && defined(HAVE_PTHREAD) && !defined(__APPLE__)
  97. static inline
  98. void SetupAtForkLocksHandler()
  99. {
  100. perftools_pthread_atfork(
  101. CentralCacheLockAll, // parent calls before fork
  102. CentralCacheUnlockAll, // parent calls after fork
  103. CentralCacheUnlockAll); // child calls after fork
  104. }
  105. REGISTER_MODULE_INITIALIZER(tcmalloc_fork_handler, SetupAtForkLocksHandler());
  106. #endif
  107. } // namespace tcmalloc