| 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 |
| 34 #ifndef TCMALLOC_COMMON_H_ | 35 #ifndef TCMALLOC_COMMON_H_ |
| 35 #define TCMALLOC_COMMON_H_ | 36 #define TCMALLOC_COMMON_H_ |
| 36 | 37 |
| 37 #include "config.h" | 38 #include "config.h" |
| 38 #include <stddef.h> // for size_t | 39 #include <stddef.h> // for size_t |
| 39 #ifdef HAVE_STDINT_H | 40 #ifdef HAVE_STDINT_H |
| 40 #include <stdint.h> // for uintptr_t, uint64_t | 41 #include <stdint.h> // for uintptr_t, uint64_t |
| 41 #endif | 42 #endif |
| 42 #include "free_list.h" // for SIZE_CLASS macros | 43 #include "free_list.h" // for SIZE_CLASS macros |
| 43 #include "internal_logging.h" // for ASSERT, etc | 44 #include "internal_logging.h" // for ASSERT, etc |
| (...skipping 12 matching lines...) Expand all Loading... |
| 56 // Deallocation may speed up by a factor as the page map gets 8x smaller, so | 57 // 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 | 58 // 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. | 59 // speedup for application/platform combinations with high L2 cache pressure. |
| 59 // As the number of size classes increases with large pages, we increase | 60 // 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 | 61 // 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. | 62 // central lists. Also, larger pages are less likely to get freed. |
| 62 // These two factors cause a bounded increase in memory use. | 63 // These two factors cause a bounded increase in memory use. |
| 63 | 64 |
| 64 static const size_t kAlignment = 8; | 65 static const size_t kAlignment = 8; |
| 65 | 66 |
| 66 // Constants dependant on tcmalloc configuration and archetecture. | 67 // Constants dependant on tcmalloc configuration and archetecture. Chromium |
| 68 // tunes these constants. |
| 67 // We need to guarantee the smallest class size is big enough to hold the | 69 // We need to guarantee the smallest class size is big enough to hold the |
| 68 // pointers that form the free list. | 70 // pointers that form the free list. |
| 69 static const size_t kNumFreeListPointers = | 71 static const size_t kNumFreeListPointers = |
| 70 (tcmalloc::kSupportsDoublyLinkedList ? 2 : 1); | 72 (tcmalloc::kSupportsDoublyLinkedList ? 2 : 1); |
| 71 static const size_t kLinkSize = kNumFreeListPointers * sizeof(void *); | 73 static const size_t kLinkSize = kNumFreeListPointers * sizeof(void *); |
| 72 static const size_t kMinClassSize = | 74 static const size_t kMinClassSize = |
| 73 (kLinkSize > kAlignment ? kLinkSize : kAlignment); | 75 (kLinkSize > kAlignment ? kLinkSize : kAlignment); |
| 74 static const size_t kSkippedClasses = (kAlignment < kMinClassSize ? 1 : 0); | 76 static const size_t kSkippedClasses = (kAlignment < kMinClassSize ? 1 : 0); |
| 75 | 77 |
| 76 #if defined(TCMALLOC_LARGE_PAGES) | 78 #if defined(TCMALLOC_LARGE_PAGES) |
| 77 static const size_t kPageShift = 15; | 79 static const size_t kPageShift = 15; |
| 78 static const size_t kNumClasses = 95 - kSkippedClasses; | 80 static const size_t kNumClasses = 78 - kSkippedClasses; |
| 81 #else |
| 82 static const size_t kPageShift = 13; |
| 83 static const size_t kNumClasses = 86 - kSkippedClasses; |
| 84 #endif |
| 79 static const size_t kMaxThreadCacheSize = 4 << 20; | 85 static const size_t kMaxThreadCacheSize = 4 << 20; |
| 80 #else | |
| 81 static const size_t kPageShift = 12; | |
| 82 static const size_t kNumClasses = 61 - kSkippedClasses; | |
| 83 static const size_t kMaxThreadCacheSize = 2 << 20; | |
| 84 #endif | |
| 85 | 86 |
| 86 static const size_t kPageSize = 1 << kPageShift; | 87 static const size_t kPageSize = 1 << kPageShift; |
| 87 static const size_t kMaxSize = 8u * kPageSize; | 88 // TODO(dmikurube): We Chromium may want to tune this kMaxSize. |
| 89 static const size_t kMaxSize = 256 * 1024; |
| 88 // For all span-lengths < kMaxPages we keep an exact-size list. | 90 // For all span-lengths < kMaxPages we keep an exact-size list. |
| 89 static const size_t kMaxPages = 1 << (20 - kPageShift); | 91 static const size_t kMaxPages = 1 << (20 - kPageShift); |
| 90 | 92 |
| 91 // Default bound on the total amount of thread caches. | 93 // Default bound on the total amount of thread caches. |
| 92 #ifdef TCMALLOC_SMALL_BUT_SLOW | 94 #ifdef TCMALLOC_SMALL_BUT_SLOW |
| 93 // Make the overall thread cache no bigger than that of a single thread | 95 // Make the overall thread cache no bigger than that of a single thread |
| 94 // for the small memory footprint case. | 96 // for the small memory footprint case. |
| 95 static const size_t kDefaultOverallThreadCacheSize = kMaxThreadCacheSize; | 97 static const size_t kDefaultOverallThreadCacheSize = kMaxThreadCacheSize; |
| 96 #else | 98 #else |
| 97 static const size_t kDefaultOverallThreadCacheSize = 8u * kMaxThreadCacheSize; | 99 static const size_t kDefaultOverallThreadCacheSize = 8u * kMaxThreadCacheSize; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 // ------------------------------------------------------- | 171 // ------------------------------------------------------- |
| 170 // 0 (0 + 7) / 8 0 | 172 // 0 (0 + 7) / 8 0 |
| 171 // 1 (1 + 7) / 8 1 | 173 // 1 (1 + 7) / 8 1 |
| 172 // ... | 174 // ... |
| 173 // 1024 (1024 + 7) / 8 128 | 175 // 1024 (1024 + 7) / 8 128 |
| 174 // 1025 (1025 + 127 + (120<<7)) / 128 129 | 176 // 1025 (1025 + 127 + (120<<7)) / 128 129 |
| 175 // ... | 177 // ... |
| 176 // 32768 (32768 + 127 + (120<<7)) / 128 376 | 178 // 32768 (32768 + 127 + (120<<7)) / 128 376 |
| 177 static const int kMaxSmallSize = 1024; | 179 static const int kMaxSmallSize = 1024; |
| 178 static const size_t kClassArraySize = | 180 static const size_t kClassArraySize = |
| 179 (((1 << kPageShift) * 8u + 127 + (120 << 7)) >> 7) + 1; | 181 ((kMaxSize + 127 + (120 << 7)) >> 7) + 1; |
| 180 unsigned char class_array_[kClassArraySize]; | 182 unsigned char class_array_[kClassArraySize]; |
| 181 | 183 |
| 182 // Compute index of the class_array[] entry for a given size | 184 // Compute index of the class_array[] entry for a given size |
| 183 static inline int ClassIndex(int s) { | 185 static inline int ClassIndex(int s) { |
| 184 ASSERT(0 <= s); | 186 ASSERT(0 <= s); |
| 185 ASSERT(s <= kMaxSize); | 187 ASSERT(s <= kMaxSize); |
| 186 const bool big = (s > kMaxSmallSize); | 188 const bool big = (s > kMaxSmallSize); |
| 187 const int add_amount = big ? (127 + (120<<7)) : 7; | 189 const int add_amount = big ? (127 + (120<<7)) : 7; |
| 188 const int shift_amount = big ? 7 : 3; | 190 const int shift_amount = big ? 7 : 3; |
| 189 return (s + add_amount) >> shift_amount; | 191 return (s + add_amount) >> shift_amount; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 227 } |
| 226 | 228 |
| 227 // Number of objects to move between a per-thread list and a central | 229 // Number of objects to move between a per-thread list and a central |
| 228 // list in one shot. We want this to be not too small so we can | 230 // list in one shot. We want this to be not too small so we can |
| 229 // amortize the lock overhead for accessing the central list. Making | 231 // amortize the lock overhead for accessing the central list. Making |
| 230 // it too big may temporarily cause unnecessary memory wastage in the | 232 // it too big may temporarily cause unnecessary memory wastage in the |
| 231 // per-thread free list until the scavenger cleans up the list. | 233 // per-thread free list until the scavenger cleans up the list. |
| 232 inline int num_objects_to_move(size_t cl) { | 234 inline int num_objects_to_move(size_t cl) { |
| 233 return num_objects_to_move_[cl]; | 235 return num_objects_to_move_[cl]; |
| 234 } | 236 } |
| 235 | |
| 236 // Dump contents of the computed size map | |
| 237 void Dump(TCMalloc_Printer* out); | |
| 238 }; | 237 }; |
| 239 | 238 |
| 240 // Allocates "bytes" worth of memory and returns it. Increments | 239 // Allocates "bytes" worth of memory and returns it. Increments |
| 241 // metadata_system_bytes appropriately. May return NULL if allocation | 240 // metadata_system_bytes appropriately. May return NULL if allocation |
| 242 // fails. Requires pageheap_lock is held. | 241 // fails. Requires pageheap_lock is held. |
| 243 void* MetaDataAlloc(size_t bytes); | 242 void* MetaDataAlloc(size_t bytes); |
| 244 | 243 |
| 245 // Returns the total number of bytes allocated from the system. | 244 // Returns the total number of bytes allocated from the system. |
| 246 // Requires pageheap_lock is held. | 245 // Requires pageheap_lock is held. |
| 247 uint64_t metadata_system_bytes(); | 246 uint64_t metadata_system_bytes(); |
| 248 | 247 |
| 249 // Adjust metadata_system_bytes to indicate that bytes are actually committed. | 248 // Adjust metadata_system_bytes to indicate that bytes are actually committed. |
| 250 // Requires pageheap_lock is held. | 249 // Requires pageheap_lock is held. |
| 251 void increment_metadata_system_bytes(size_t bytes); | 250 void increment_metadata_system_bytes(size_t bytes); |
| 252 | 251 |
| 253 // size/depth are made the same size as a pointer so that some generic | 252 // size/depth are made the same size as a pointer so that some generic |
| 254 // code below can conveniently cast them back and forth to void*. | 253 // code below can conveniently cast them back and forth to void*. |
| 255 static const int kMaxStackDepth = 31; | 254 static const int kMaxStackDepth = 31; |
| 256 struct StackTrace { | 255 struct StackTrace { |
| 257 uintptr_t size; // Size of object | 256 uintptr_t size; // Size of object |
| 258 uintptr_t depth; // Number of PC values stored in array below | 257 uintptr_t depth; // Number of PC values stored in array below |
| 259 void* stack[kMaxStackDepth]; | 258 void* stack[kMaxStackDepth]; |
| 260 }; | 259 }; |
| 261 | 260 |
| 262 } // namespace tcmalloc | 261 } // namespace tcmalloc |
| 263 | 262 |
| 264 #endif // TCMALLOC_COMMON_H_ | 263 #endif // TCMALLOC_COMMON_H_ |
| OLD | NEW |