OLD | NEW |
1 // Copyright (c) 2008, Google Inc. | 1 // Copyright (c) 2008, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 // --- | 30 // --- |
31 // Author: Sanjay Ghemawat <opensource@google.com> | 31 // Author: Sanjay Ghemawat <opensource@google.com> |
32 // | 32 // |
33 // Common definitions for tcmalloc code. | 33 // Common definitions for tcmalloc code. |
34 | |
35 #ifndef TCMALLOC_COMMON_H_ | 34 #ifndef TCMALLOC_COMMON_H_ |
36 #define TCMALLOC_COMMON_H_ | 35 #define TCMALLOC_COMMON_H_ |
37 | 36 |
38 #include "config.h" | 37 #include "config.h" |
39 #include <stddef.h> // for size_t | 38 #include <stddef.h> // for size_t |
40 #ifdef HAVE_STDINT_H | 39 #ifdef HAVE_STDINT_H |
41 #include <stdint.h> // for uintptr_t, uint64_t | 40 #include <stdint.h> // for uintptr_t, uint64_t |
42 #endif | 41 #endif |
| 42 #include "free_list.h" // for SIZE_CLASS macros |
43 #include "internal_logging.h" // for ASSERT, etc | 43 #include "internal_logging.h" // for ASSERT, etc |
44 | 44 |
45 // Type that can hold a page number | 45 // Type that can hold a page number |
46 typedef uintptr_t PageID; | 46 typedef uintptr_t PageID; |
47 | 47 |
48 // Type that can hold the length of a run of pages | 48 // Type that can hold the length of a run of pages |
49 typedef uintptr_t Length; | 49 typedef uintptr_t Length; |
50 | 50 |
51 //------------------------------------------------------------------- | 51 //------------------------------------------------------------------- |
52 // Configuration | 52 // Configuration |
53 //------------------------------------------------------------------- | 53 //------------------------------------------------------------------- |
54 | 54 |
55 // Using large pages speeds up the execution at a cost of larger memory use. | 55 // Using large pages speeds up the execution at a cost of larger memory use. |
56 // Deallocation may speed up by a factor as the page map gets 8x smaller, so | 56 // Deallocation may speed up by a factor as the page map gets 8x smaller, so |
57 // lookups in the page map result in fewer L2 cache misses, which translates to | 57 // lookups in the page map result in fewer L2 cache misses, which translates to |
58 // speedup for application/platform combinations with high L2 cache pressure. | 58 // speedup for application/platform combinations with high L2 cache pressure. |
59 // As the number of size classes increases with large pages, we increase | 59 // As the number of size classes increases with large pages, we increase |
60 // the thread cache allowance to avoid passing more free ranges to and from | 60 // the thread cache allowance to avoid passing more free ranges to and from |
61 // central lists. Also, larger pages are less likely to get freed. | 61 // central lists. Also, larger pages are less likely to get freed. |
62 // These two factors cause a bounded increase in memory use. | 62 // These two factors cause a bounded increase in memory use. |
63 | 63 |
| 64 static const size_t kAlignment = 8; |
| 65 |
| 66 // Constants dependant on tcmalloc configuration and archetecture. |
| 67 // We need to guarantee the smallest class size is big enough to hold the |
| 68 // pointers that form the free list. |
| 69 static const size_t kNumFreeListPointers = |
| 70 (tcmalloc::kSupportsDoublyLinkedList ? 2 : 1); |
| 71 static const size_t kLinkSize = kNumFreeListPointers * sizeof(void *); |
| 72 static const size_t kMinClassSize = |
| 73 (kLinkSize > kAlignment ? kLinkSize : kAlignment); |
| 74 static const size_t kSkippedClasses = (kAlignment < kMinClassSize ? 1 : 0); |
| 75 |
64 #if defined(TCMALLOC_LARGE_PAGES) | 76 #if defined(TCMALLOC_LARGE_PAGES) |
65 static const size_t kPageShift = 15; | 77 static const size_t kPageShift = 15; |
66 static const size_t kNumClasses = 78; | 78 static const size_t kNumClasses = 78 - kSkippedClasses; |
67 #else | 79 #else |
68 static const size_t kPageShift = 13; | 80 static const size_t kPageShift = 13; |
69 static const size_t kNumClasses = 86; | 81 static const size_t kNumClasses = 86 - kSkippedClasses; |
70 #endif | 82 #endif |
71 static const size_t kMaxThreadCacheSize = 4 << 20; | 83 static const size_t kMaxThreadCacheSize = 4 << 20; |
72 | 84 |
73 static const size_t kPageSize = 1 << kPageShift; | 85 static const size_t kPageSize = 1 << kPageShift; |
74 static const size_t kMaxSize = 256 * 1024; | 86 static const size_t kMaxSize = 256 * 1024; |
75 static const size_t kAlignment = 8; | |
76 static const size_t kLargeSizeClass = 0; | 87 static const size_t kLargeSizeClass = 0; |
77 // For all span-lengths < kMaxPages we keep an exact-size list. | 88 // For all span-lengths < kMaxPages we keep an exact-size list. |
78 static const size_t kMaxPages = 1 << (20 - kPageShift); | 89 static const size_t kMaxPages = 1 << (20 - kPageShift); |
79 | 90 |
80 // Default bound on the total amount of thread caches. | 91 // Default bound on the total amount of thread caches. |
81 #ifdef TCMALLOC_SMALL_BUT_SLOW | 92 #ifdef TCMALLOC_SMALL_BUT_SLOW |
82 // Make the overall thread cache no bigger than that of a single thread | 93 // Make the overall thread cache no bigger than that of a single thread |
83 // for the small memory footprint case. | 94 // for the small memory footprint case. |
84 static const size_t kDefaultOverallThreadCacheSize = kMaxThreadCacheSize; | 95 static const size_t kDefaultOverallThreadCacheSize = kMaxThreadCacheSize; |
85 #else | 96 #else |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 236 |
226 // Allocates "bytes" worth of memory and returns it. Increments | 237 // Allocates "bytes" worth of memory and returns it. Increments |
227 // metadata_system_bytes appropriately. May return NULL if allocation | 238 // metadata_system_bytes appropriately. May return NULL if allocation |
228 // fails. Requires pageheap_lock is held. | 239 // fails. Requires pageheap_lock is held. |
229 void* MetaDataAlloc(size_t bytes); | 240 void* MetaDataAlloc(size_t bytes); |
230 | 241 |
231 // Returns the total number of bytes allocated from the system. | 242 // Returns the total number of bytes allocated from the system. |
232 // Requires pageheap_lock is held. | 243 // Requires pageheap_lock is held. |
233 uint64_t metadata_system_bytes(); | 244 uint64_t metadata_system_bytes(); |
234 | 245 |
| 246 // Adjust metadata_system_bytes to indicate that bytes are actually committed. |
| 247 // Requires pageheap_lock is held. |
| 248 void increment_metadata_system_bytes(size_t bytes); |
| 249 |
235 // size/depth are made the same size as a pointer so that some generic | 250 // size/depth are made the same size as a pointer so that some generic |
236 // code below can conveniently cast them back and forth to void*. | 251 // code below can conveniently cast them back and forth to void*. |
237 static const int kMaxStackDepth = 31; | 252 static const int kMaxStackDepth = 31; |
238 struct StackTrace { | 253 struct StackTrace { |
239 uintptr_t size; // Size of object | 254 uintptr_t size; // Size of object |
240 uintptr_t depth; // Number of PC values stored in array below | 255 uintptr_t depth; // Number of PC values stored in array below |
241 void* stack[kMaxStackDepth]; | 256 void* stack[kMaxStackDepth]; |
242 }; | 257 }; |
243 | 258 |
244 } // namespace tcmalloc | 259 } // namespace tcmalloc |
245 | 260 |
246 #endif // TCMALLOC_COMMON_H_ | 261 #endif // TCMALLOC_COMMON_H_ |
OLD | NEW |