Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 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 | // --- |
| 32 | // Author: Ken Ashcraft <opensource@google.com> |
| 33 | |
| 34 | #include <config.h> |
| 35 | #include "static_vars.h" |
| 36 | #include <stddef.h> // for NULL |
| 37 | #include <new> // for operator new |
| 38 | #ifdef HAVE_PTHREAD |
| 39 | #include <pthread.h> // for pthread_atfork |
| 40 | #endif |
| 41 | #include "internal_logging.h" // for CHECK_CONDITION |
| 42 | #include "common.h" |
| 43 | #include "sampler.h" // for Sampler |
| 44 | #include "getenv_safe.h" // TCMallocGetenvSafe |
| 45 | #include "base/googleinit.h" |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 46 | #include "maybe_threads.h" |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 47 | |
| 48 | namespace tcmalloc { |
| 49 | |
| 50 | #if defined(HAVE_FORK) && defined(HAVE_PTHREAD) |
| 51 | // These following two functions are registered via pthread_atfork to make |
| 52 | // sure the central_cache locks remain in a consisten state in the forked |
| 53 | // version of the thread. |
| 54 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 55 | void CentralCacheLockAll() NO_THREAD_SAFETY_ANALYSIS |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 56 | { |
| 57 | Static::pageheap_lock()->Lock(); |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 58 | for (int i = 0; i < Static::num_size_classes(); ++i) |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 59 | Static::central_cache()[i].Lock(); |
| 60 | } |
| 61 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 62 | void CentralCacheUnlockAll() NO_THREAD_SAFETY_ANALYSIS |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 63 | { |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 64 | for (int i = 0; i < Static::num_size_classes(); ++i) |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 65 | Static::central_cache()[i].Unlock(); |
| 66 | Static::pageheap_lock()->Unlock(); |
| 67 | } |
| 68 | #endif |
| 69 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 70 | bool Static::inited_; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 71 | SpinLock Static::pageheap_lock_(SpinLock::LINKER_INITIALIZED); |
| 72 | SizeMap Static::sizemap_; |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 73 | CentralFreeListPadded Static::central_cache_[kClassSizesMax]; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 74 | PageHeapAllocator<Span> Static::span_allocator_; |
| 75 | PageHeapAllocator<StackTrace> Static::stacktrace_allocator_; |
| 76 | Span Static::sampled_objects_; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 77 | StackTrace* Static::growth_stacks_ = NULL; |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 78 | Static::PageHeapStorage Static::pageheap_; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 79 | |
| 80 | void Static::InitStaticVars() { |
| 81 | sizemap_.Init(); |
| 82 | span_allocator_.Init(); |
| 83 | span_allocator_.New(); // Reduce cache conflicts |
| 84 | span_allocator_.New(); // Reduce cache conflicts |
| 85 | stacktrace_allocator_.Init(); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 86 | // Do a bit of sanitizing: make sure central_cache is aligned properly |
| 87 | CHECK_CONDITION((sizeof(central_cache_[0]) % 64) == 0); |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 88 | for (int i = 0; i < num_size_classes(); ++i) { |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 89 | central_cache_[i].Init(i); |
| 90 | } |
| 91 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 92 | new (&pageheap_.memory) PageHeap; |
| 93 | |
| 94 | #if defined(ENABLE_AGGRESSIVE_DECOMMIT_BY_DEFAULT) |
| 95 | const bool kDefaultAggressiveDecommit = true; |
| 96 | #else |
| 97 | const bool kDefaultAggressiveDecommit = false; |
| 98 | #endif |
| 99 | |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 100 | |
| 101 | bool aggressive_decommit = |
| 102 | tcmalloc::commandlineflags::StringToBool( |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 103 | TCMallocGetenvSafe("TCMALLOC_AGGRESSIVE_DECOMMIT"), |
| 104 | kDefaultAggressiveDecommit); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 105 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 106 | pageheap()->SetAggressiveDecommit(aggressive_decommit); |
| 107 | |
| 108 | inited_ = true; |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 109 | |
| 110 | DLL_Init(&sampled_objects_); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 113 | void Static::InitLateMaybeRecursive() { |
| 114 | #if defined(HAVE_FORK) && defined(HAVE_PTHREAD) \ |
| 115 | && !defined(__APPLE__) && !defined(TCMALLOC_NO_ATFORK) |
| 116 | // OSX has it's own way of handling atfork in malloc (see |
| 117 | // libc_override_osx.h). |
| 118 | // |
| 119 | // For other OSes we do pthread_atfork even if standard seemingly |
| 120 | // discourages pthread_atfork, asking apps to do only |
| 121 | // async-signal-safe calls between fork and exec. |
| 122 | // |
| 123 | // We're deliberately attempting to register atfork handlers as part |
| 124 | // of malloc initialization. So very early. This ensures that our |
| 125 | // handler is called last and that means fork will try to grab |
| 126 | // tcmalloc locks last avoiding possible issues with many other |
| 127 | // locks that are held around calls to malloc. I.e. if we don't do |
| 128 | // that, fork() grabbing malloc lock before such other lock would be |
| 129 | // prone to deadlock, if some other thread holds other lock and |
| 130 | // calls malloc. |
| 131 | // |
| 132 | // We still leave some way of disabling it via |
| 133 | // -DTCMALLOC_NO_ATFORK. It looks like on glibc even with fully |
| 134 | // static binaries malloc is really initialized very early. But I |
| 135 | // can see how combination of static linking and other libc-s could |
| 136 | // be less fortunate and allow some early app constructors to run |
| 137 | // before malloc is ever called. |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 138 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 139 | perftools_pthread_atfork( |
| 140 | CentralCacheLockAll, // parent calls before fork |
| 141 | CentralCacheUnlockAll, // parent calls after fork |
| 142 | CentralCacheUnlockAll); // child calls after fork |
| 143 | #endif |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 144 | |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 145 | #ifndef NDEBUG |
| 146 | // pthread_atfork above may malloc sometimes. Lets ensure we test |
| 147 | // that malloc works from here. |
| 148 | free(malloc(1)); |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 149 | #endif |
| 150 | } |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 151 | |
| 152 | } // namespace tcmalloc |