blob: fef6ed1aff4f10b0168bb82847515bac78913486 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- 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 Silverman20350ac2021-11-17 18:19:55 -080046#include "maybe_threads.h"
Austin Schuh745610d2015-09-06 18:19:50 -070047
48namespace 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 Silverman20350ac2021-11-17 18:19:55 -080055void CentralCacheLockAll() NO_THREAD_SAFETY_ANALYSIS
Austin Schuh745610d2015-09-06 18:19:50 -070056{
57 Static::pageheap_lock()->Lock();
Brian Silverman20350ac2021-11-17 18:19:55 -080058 for (int i = 0; i < Static::num_size_classes(); ++i)
Austin Schuh745610d2015-09-06 18:19:50 -070059 Static::central_cache()[i].Lock();
60}
61
Brian Silverman20350ac2021-11-17 18:19:55 -080062void CentralCacheUnlockAll() NO_THREAD_SAFETY_ANALYSIS
Austin Schuh745610d2015-09-06 18:19:50 -070063{
Brian Silverman20350ac2021-11-17 18:19:55 -080064 for (int i = 0; i < Static::num_size_classes(); ++i)
Austin Schuh745610d2015-09-06 18:19:50 -070065 Static::central_cache()[i].Unlock();
66 Static::pageheap_lock()->Unlock();
67}
68#endif
69
Brian Silverman20350ac2021-11-17 18:19:55 -080070bool Static::inited_;
Austin Schuh745610d2015-09-06 18:19:50 -070071SpinLock Static::pageheap_lock_(SpinLock::LINKER_INITIALIZED);
72SizeMap Static::sizemap_;
Brian Silverman20350ac2021-11-17 18:19:55 -080073CentralFreeListPadded Static::central_cache_[kClassSizesMax];
Austin Schuh745610d2015-09-06 18:19:50 -070074PageHeapAllocator<Span> Static::span_allocator_;
75PageHeapAllocator<StackTrace> Static::stacktrace_allocator_;
76Span Static::sampled_objects_;
Austin Schuh745610d2015-09-06 18:19:50 -070077StackTrace* Static::growth_stacks_ = NULL;
Brian Silverman20350ac2021-11-17 18:19:55 -080078Static::PageHeapStorage Static::pageheap_;
Austin Schuh745610d2015-09-06 18:19:50 -070079
80void 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 Schuh745610d2015-09-06 18:19:50 -070086 // Do a bit of sanitizing: make sure central_cache is aligned properly
87 CHECK_CONDITION((sizeof(central_cache_[0]) % 64) == 0);
Brian Silverman20350ac2021-11-17 18:19:55 -080088 for (int i = 0; i < num_size_classes(); ++i) {
Austin Schuh745610d2015-09-06 18:19:50 -070089 central_cache_[i].Init(i);
90 }
91
Brian Silverman20350ac2021-11-17 18:19:55 -080092 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 Schuh745610d2015-09-06 18:19:50 -0700100
101 bool aggressive_decommit =
102 tcmalloc::commandlineflags::StringToBool(
Brian Silverman20350ac2021-11-17 18:19:55 -0800103 TCMallocGetenvSafe("TCMALLOC_AGGRESSIVE_DECOMMIT"),
104 kDefaultAggressiveDecommit);
Austin Schuh745610d2015-09-06 18:19:50 -0700105
Brian Silverman20350ac2021-11-17 18:19:55 -0800106 pageheap()->SetAggressiveDecommit(aggressive_decommit);
107
108 inited_ = true;
Austin Schuh745610d2015-09-06 18:19:50 -0700109
110 DLL_Init(&sampled_objects_);
Austin Schuh745610d2015-09-06 18:19:50 -0700111}
112
Brian Silverman20350ac2021-11-17 18:19:55 -0800113void 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 Schuh745610d2015-09-06 18:19:50 -0700138
Brian Silverman20350ac2021-11-17 18:19:55 -0800139 perftools_pthread_atfork(
140 CentralCacheLockAll, // parent calls before fork
141 CentralCacheUnlockAll, // parent calls after fork
142 CentralCacheUnlockAll); // child calls after fork
143#endif
Austin Schuh745610d2015-09-06 18:19:50 -0700144
Brian Silverman20350ac2021-11-17 18:19:55 -0800145#ifndef NDEBUG
146 // pthread_atfork above may malloc sometimes. Lets ensure we test
147 // that malloc works from here.
148 free(malloc(1));
Austin Schuh745610d2015-09-06 18:19:50 -0700149#endif
150}
Austin Schuh745610d2015-09-06 18:19:50 -0700151
152} // namespace tcmalloc