central_freelist.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. // Author: Sanjay Ghemawat <opensource@google.com>
  32. #ifndef TCMALLOC_CENTRAL_FREELIST_H_
  33. #define TCMALLOC_CENTRAL_FREELIST_H_
  34. #include "config.h"
  35. #include <stddef.h> // for size_t
  36. #ifdef HAVE_STDINT_H
  37. #include <stdint.h> // for int32_t
  38. #endif
  39. #include "base/spinlock.h"
  40. #include "base/thread_annotations.h"
  41. #include "common.h"
  42. #include "span.h"
  43. namespace tcmalloc {
  44. // Data kept per size-class in central cache.
  45. class CentralFreeList {
  46. public:
  47. // A CentralFreeList may be used before its constructor runs.
  48. // So we prevent lock_'s constructor from doing anything to the
  49. // lock_ state.
  50. CentralFreeList() : lock_(base::LINKER_INITIALIZED) { }
  51. void Init(size_t cl);
  52. // These methods all do internal locking.
  53. // Insert the specified range into the central freelist. N is the number of
  54. // elements in the range. RemoveRange() is the opposite operation.
  55. void InsertRange(void *start, void *end, int N);
  56. // Returns the actual number of fetched elements and sets *start and *end.
  57. int RemoveRange(void **start, void **end, int N);
  58. // Returns the number of free objects in cache.
  59. int length() {
  60. SpinLockHolder h(&lock_);
  61. return counter_;
  62. }
  63. // Returns the number of free objects in the transfer cache.
  64. int tc_length();
  65. // Returns the memory overhead (internal fragmentation) attributable
  66. // to the freelist. This is memory lost when the size of elements
  67. // in a freelist doesn't exactly divide the page-size (an 8192-byte
  68. // page full of 5-byte objects would have 2 bytes memory overhead).
  69. size_t OverheadBytes();
  70. // Lock/Unlock the internal SpinLock. Used on the pthread_atfork call
  71. // to set the lock in a consistent state before the fork.
  72. void Lock() {
  73. lock_.Lock();
  74. }
  75. void Unlock() {
  76. lock_.Unlock();
  77. }
  78. private:
  79. // TransferCache is used to cache transfers of
  80. // sizemap.num_objects_to_move(size_class) back and forth between
  81. // thread caches and the central cache for a given size class.
  82. struct TCEntry {
  83. void *head; // Head of chain of objects.
  84. void *tail; // Tail of chain of objects.
  85. };
  86. // A central cache freelist can have anywhere from 0 to kMaxNumTransferEntries
  87. // slots to put link list chains into.
  88. #ifdef TCMALLOC_SMALL_BUT_SLOW
  89. // For the small memory model, the transfer cache is not used.
  90. static const int kMaxNumTransferEntries = 0;
  91. #else
  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;
  96. #endif
  97. // REQUIRES: lock_ is held
  98. // Remove object from cache and return.
  99. // Return NULL if no free entries in cache.
  100. int FetchFromOneSpans(int N, void **start, void **end) EXCLUSIVE_LOCKS_REQUIRED(lock_);
  101. // REQUIRES: lock_ is held
  102. // Remove object from cache and return. Fetches
  103. // from pageheap if cache is empty. Only returns
  104. // NULL on allocation failure.
  105. int FetchFromOneSpansSafe(int N, void **start, void **end) EXCLUSIVE_LOCKS_REQUIRED(lock_);
  106. // REQUIRES: lock_ is held
  107. // Release a linked list of objects to spans.
  108. // May temporarily release lock_.
  109. void ReleaseListToSpans(void *start) EXCLUSIVE_LOCKS_REQUIRED(lock_);
  110. // REQUIRES: lock_ is held
  111. // Release an object to spans.
  112. // May temporarily release lock_.
  113. void ReleaseToSpans(void* object) EXCLUSIVE_LOCKS_REQUIRED(lock_);
  114. // REQUIRES: lock_ is held
  115. // Populate cache by fetching from the page heap.
  116. // May temporarily release lock_.
  117. void Populate() EXCLUSIVE_LOCKS_REQUIRED(lock_);
  118. // REQUIRES: lock is held.
  119. // Tries to make room for a TCEntry. If the cache is full it will try to
  120. // expand it at the cost of some other cache size. Return false if there is
  121. // no space.
  122. bool MakeCacheSpace() EXCLUSIVE_LOCKS_REQUIRED(lock_);
  123. // REQUIRES: lock_ for locked_size_class is held.
  124. // Picks a "random" size class to steal TCEntry slot from. In reality it
  125. // just iterates over the sizeclasses but does so without taking a lock.
  126. // Returns true on success.
  127. // May temporarily lock a "random" size class.
  128. static bool EvictRandomSizeClass(int locked_size_class, bool force);
  129. // REQUIRES: lock_ is *not* held.
  130. // Tries to shrink the Cache. If force is true it will relase objects to
  131. // spans if it allows it to shrink the cache. Return false if it failed to
  132. // shrink the cache. Decrements cache_size_ on succeess.
  133. // May temporarily take lock_. If it takes lock_, the locked_size_class
  134. // lock is released to keep the thread from holding two size class locks
  135. // concurrently which could lead to a deadlock.
  136. bool ShrinkCache(int locked_size_class, bool force) LOCKS_EXCLUDED(lock_);
  137. // This lock protects all the data members. cached_entries and cache_size_
  138. // may be looked at without holding the lock.
  139. SpinLock lock_;
  140. // We keep linked lists of empty and non-empty spans.
  141. size_t size_class_; // My size class
  142. Span empty_; // Dummy header for list of empty spans
  143. Span nonempty_; // Dummy header for list of non-empty spans
  144. size_t num_spans_; // Number of spans in empty_ plus nonempty_
  145. size_t counter_; // Number of free objects in cache entry
  146. // Here we reserve space for TCEntry cache slots. Space is preallocated
  147. // for the largest possible number of entries than any one size class may
  148. // accumulate. Not all size classes are allowed to accumulate
  149. // kMaxNumTransferEntries, so there is some wasted space for those size
  150. // classes.
  151. TCEntry tc_slots_[kMaxNumTransferEntries];
  152. // Number of currently used cached entries in tc_slots_. This variable is
  153. // updated under a lock but can be read without one.
  154. int32_t used_slots_;
  155. // The current number of slots for this size class. This is an
  156. // adaptive value that is increased if there is lots of traffic
  157. // on a given size class.
  158. int32_t cache_size_;
  159. // Maximum size of the cache for a given size class.
  160. int32_t max_cache_size_;
  161. };
  162. // 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
  164. // template specialization to remove the padding entirely when
  165. // sizeof(CentralFreeList) is a multiple of 64.
  166. template<int kFreeListSizeMod64>
  167. class CentralFreeListPaddedTo : public CentralFreeList {
  168. private:
  169. char pad_[64 - kFreeListSizeMod64];
  170. };
  171. template<>
  172. class CentralFreeListPaddedTo<0> : public CentralFreeList {
  173. };
  174. class CentralFreeListPadded : public CentralFreeListPaddedTo<
  175. sizeof(CentralFreeList) % 64> {
  176. };
  177. } // namespace tcmalloc
  178. #endif // TCMALLOC_CENTRAL_FREELIST_H_