| 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 17 matching lines...) Expand all Loading... |
| 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 #ifndef TCMALLOC_CENTRAL_FREELIST_H_ | 33 #ifndef TCMALLOC_CENTRAL_FREELIST_H_ |
| 34 #define TCMALLOC_CENTRAL_FREELIST_H_ | 34 #define TCMALLOC_CENTRAL_FREELIST_H_ |
| 35 | 35 |
| 36 #include "config.h" | 36 #include "config.h" |
| 37 #include <stddef.h> // for size_t | 37 #include <stddef.h> // for size_t |
| 38 #ifdef HAVE_STDINT_H |
| 38 #include <stdint.h> // for int32_t | 39 #include <stdint.h> // for int32_t |
| 40 #endif |
| 39 #include "base/spinlock.h" | 41 #include "base/spinlock.h" |
| 40 #include "base/thread_annotations.h" | 42 #include "base/thread_annotations.h" |
| 41 #include "common.h" | 43 #include "common.h" |
| 42 #include "span.h" | 44 #include "span.h" |
| 43 | 45 |
| 44 namespace tcmalloc { | 46 namespace tcmalloc { |
| 45 | 47 |
| 46 // Data kept per size-class in central cache. | 48 // Data kept per size-class in central cache. |
| 47 class CentralFreeList { | 49 class CentralFreeList { |
| 48 public: | 50 public: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 59 | 61 |
| 60 // Returns the number of free objects in cache. | 62 // Returns the number of free objects in cache. |
| 61 int length() { | 63 int length() { |
| 62 SpinLockHolder h(&lock_); | 64 SpinLockHolder h(&lock_); |
| 63 return counter_; | 65 return counter_; |
| 64 } | 66 } |
| 65 | 67 |
| 66 // Returns the number of free objects in the transfer cache. | 68 // Returns the number of free objects in the transfer cache. |
| 67 int tc_length(); | 69 int tc_length(); |
| 68 | 70 |
| 71 // Returns the memory overhead (internal fragmentation) attributable |
| 72 // to the freelist. This is memory lost when the size of elements |
| 73 // in a freelist doesn't exactly divide the page-size (an 8192-byte |
| 74 // page full of 5-byte objects would have 2 bytes memory overhead). |
| 75 size_t OverheadBytes(); |
| 76 |
| 69 private: | 77 private: |
| 70 // TransferCache is used to cache transfers of | 78 // TransferCache is used to cache transfers of |
| 71 // sizemap.num_objects_to_move(size_class) back and forth between | 79 // sizemap.num_objects_to_move(size_class) back and forth between |
| 72 // thread caches and the central cache for a given size class. | 80 // thread caches and the central cache for a given size class. |
| 73 struct TCEntry { | 81 struct TCEntry { |
| 74 void *head; // Head of chain of objects. | 82 void *head; // Head of chain of objects. |
| 75 void *tail; // Tail of chain of objects. | 83 void *tail; // Tail of chain of objects. |
| 76 }; | 84 }; |
| 77 | 85 |
| 78 // A central cache freelist can have anywhere from 0 to kNumTransferEntries | 86 // A central cache freelist can have anywhere from 0 to kMaxNumTransferEntries |
| 79 // slots to put link list chains into. To keep memory usage bounded the total | 87 // slots to put link list chains into. |
| 80 // number of TCEntries across size classes is fixed. Currently each size | |
| 81 // class is initially given one TCEntry which also means that the maximum any | |
| 82 // one class can have is kNumClasses. | |
| 83 #ifdef TCMALLOC_SMALL_BUT_SLOW | 88 #ifdef TCMALLOC_SMALL_BUT_SLOW |
| 84 // For the small memory model, the transfer cache is not used. | 89 // For the small memory model, the transfer cache is not used. |
| 85 static const int kNumTransferEntries = 0; | 90 static const int kMaxNumTransferEntries = 0; |
| 86 #else | 91 #else |
| 87 static const int kNumTransferEntries = kNumClasses; | 92 // Starting point for the the maximum number of entries in the transfer cache. |
| 93 // This actual maximum for a given size class may be lower than this |
| 94 // maximum value. |
| 95 static const int kMaxNumTransferEntries = 64; |
| 88 #endif | 96 #endif |
| 89 | 97 |
| 90 // REQUIRES: lock_ is held | 98 // REQUIRES: lock_ is held |
| 91 // Remove object from cache and return. | 99 // Remove object from cache and return. |
| 92 // Return NULL if no free entries in cache. | 100 // Return NULL if no free entries in cache. |
| 93 void* FetchFromSpans() EXCLUSIVE_LOCKS_REQUIRED(lock_); | 101 void* FetchFromSpans() EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 94 | 102 |
| 95 // REQUIRES: lock_ is held | 103 // REQUIRES: lock_ is held |
| 96 // Remove object from cache and return. Fetches | 104 // Remove object from cache and return. Fetches |
| 97 // from pageheap if cache is empty. Only returns | 105 // from pageheap if cache is empty. Only returns |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 bool ShrinkCache(int locked_size_class, bool force) LOCKS_EXCLUDED(lock_); | 144 bool ShrinkCache(int locked_size_class, bool force) LOCKS_EXCLUDED(lock_); |
| 137 | 145 |
| 138 // This lock protects all the data members. cached_entries and cache_size_ | 146 // This lock protects all the data members. cached_entries and cache_size_ |
| 139 // may be looked at without holding the lock. | 147 // may be looked at without holding the lock. |
| 140 SpinLock lock_; | 148 SpinLock lock_; |
| 141 | 149 |
| 142 // We keep linked lists of empty and non-empty spans. | 150 // We keep linked lists of empty and non-empty spans. |
| 143 size_t size_class_; // My size class | 151 size_t size_class_; // My size class |
| 144 Span empty_; // Dummy header for list of empty spans | 152 Span empty_; // Dummy header for list of empty spans |
| 145 Span nonempty_; // Dummy header for list of non-empty spans | 153 Span nonempty_; // Dummy header for list of non-empty spans |
| 154 size_t num_spans_; // Number of spans in empty_ plus nonempty_ |
| 146 size_t counter_; // Number of free objects in cache entry | 155 size_t counter_; // Number of free objects in cache entry |
| 147 | 156 |
| 148 // Here we reserve space for TCEntry cache slots. Since one size class can | 157 // Here we reserve space for TCEntry cache slots. Space is preallocated |
| 149 // end up getting all the TCEntries quota in the system we just preallocate | 158 // for the largest possible number of entries than any one size class may |
| 150 // sufficient number of entries here. | 159 // accumulate. Not all size classes are allowed to accumulate |
| 151 TCEntry tc_slots_[kNumTransferEntries]; | 160 // kMaxNumTransferEntries, so there is some wasted space for those size |
| 161 // classes. |
| 162 TCEntry tc_slots_[kMaxNumTransferEntries]; |
| 152 | 163 |
| 153 // Number of currently used cached entries in tc_slots_. This variable is | 164 // Number of currently used cached entries in tc_slots_. This variable is |
| 154 // updated under a lock but can be read without one. | 165 // updated under a lock but can be read without one. |
| 155 int32_t used_slots_; | 166 int32_t used_slots_; |
| 156 // The current number of slots for this size class. This is an | 167 // The current number of slots for this size class. This is an |
| 157 // adaptive value that is increased if there is lots of traffic | 168 // adaptive value that is increased if there is lots of traffic |
| 158 // on a given size class. | 169 // on a given size class. |
| 159 int32_t cache_size_; | 170 int32_t cache_size_; |
| 171 // Maximum size of the cache for a given size class. |
| 172 int32_t max_cache_size_; |
| 160 }; | 173 }; |
| 161 | 174 |
| 162 // Pads each CentralCache object to multiple of 64 bytes. Since some | 175 // Pads each CentralCache object to multiple of 64 bytes. Since some |
| 163 // compilers (such as MSVC) don't like it when the padding is 0, I use | 176 // compilers (such as MSVC) don't like it when the padding is 0, I use |
| 164 // template specialization to remove the padding entirely when | 177 // template specialization to remove the padding entirely when |
| 165 // sizeof(CentralFreeList) is a multiple of 64. | 178 // sizeof(CentralFreeList) is a multiple of 64. |
| 166 template<int kFreeListSizeMod64> | 179 template<int kFreeListSizeMod64> |
| 167 class CentralFreeListPaddedTo : public CentralFreeList { | 180 class CentralFreeListPaddedTo : public CentralFreeList { |
| 168 private: | 181 private: |
| 169 char pad_[64 - kFreeListSizeMod64]; | 182 char pad_[64 - kFreeListSizeMod64]; |
| 170 }; | 183 }; |
| 171 | 184 |
| 172 template<> | 185 template<> |
| 173 class CentralFreeListPaddedTo<0> : public CentralFreeList { | 186 class CentralFreeListPaddedTo<0> : public CentralFreeList { |
| 174 }; | 187 }; |
| 175 | 188 |
| 176 class CentralFreeListPadded : public CentralFreeListPaddedTo< | 189 class CentralFreeListPadded : public CentralFreeListPaddedTo< |
| 177 sizeof(CentralFreeList) % 64> { | 190 sizeof(CentralFreeList) % 64> { |
| 178 }; | 191 }; |
| 179 | 192 |
| 180 } // namespace tcmalloc | 193 } // namespace tcmalloc |
| 181 | 194 |
| 182 #endif // TCMALLOC_CENTRAL_FREELIST_H_ | 195 #endif // TCMALLOC_CENTRAL_FREELIST_H_ |
| OLD | NEW |