< prev index next >

src/hotspot/share/gc/shared/threadLocalAllocBuffer.hpp

Print this page
rev 50392 : JEP 331


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_SHARED_THREADLOCALALLOCBUFFER_HPP
  26 #define SHARE_VM_GC_SHARED_THREADLOCALALLOCBUFFER_HPP
  27 
  28 #include "gc/shared/gcUtil.hpp"
  29 #include "oops/typeArrayOop.hpp"
  30 #include "runtime/perfData.hpp"
  31 #include "runtime/vm_version.hpp"
  32 
  33 class GlobalTLABStats;
  34 
  35 // ThreadLocalAllocBuffer: a descriptor for thread-local storage used by
  36 // the threads for allocation.
  37 //            It is thread-private at any time, but maybe multiplexed over
  38 //            time across multiple threads. The park()/unpark() pair is
  39 //            used to make it available for such multiplexing.






  40 class ThreadLocalAllocBuffer: public CHeapObj<mtThread> {
  41   friend class VMStructs;
  42   friend class JVMCIVMStructs;
  43 private:
  44   HeapWord* _start;                              // address of TLAB
  45   HeapWord* _top;                                // address after last allocation
  46   HeapWord* _pf_top;                             // allocation prefetch watermark
  47   HeapWord* _end;                                // allocation end (excluding alignment_reserve)


  48   size_t    _desired_size;                       // desired size   (including alignment_reserve)
  49   size_t    _refill_waste_limit;                 // hold onto tlab if free() is larger than this
  50   size_t    _allocated_before_last_gc;           // total bytes allocated up until the last gc

  51 
  52   static size_t   _max_size;                          // maximum size of any TLAB
  53   static int      _reserve_for_allocation_prefetch;   // Reserve at the end of the TLAB
  54   static unsigned _target_refills;                    // expected number of refills between GCs
  55 
  56   unsigned  _number_of_refills;
  57   unsigned  _fast_refill_waste;
  58   unsigned  _slow_refill_waste;
  59   unsigned  _gc_waste;
  60   unsigned  _slow_allocations;
  61   size_t    _allocated_size;
  62 
  63   AdaptiveWeightedAverage _allocation_fraction;  // fraction of eden allocated in tlabs
  64 
  65   void accumulate_statistics();
  66   void initialize_statistics();
  67 
  68   void set_start(HeapWord* start)                { _start = start; }
  69   void set_end(HeapWord* end)                    { _end = end; }

  70   void set_top(HeapWord* top)                    { _top = top; }
  71   void set_pf_top(HeapWord* pf_top)              { _pf_top = pf_top; }
  72   void set_desired_size(size_t desired_size)     { _desired_size = desired_size; }
  73   void set_refill_waste_limit(size_t waste)      { _refill_waste_limit = waste;  }
  74 
  75   size_t initial_refill_waste_limit()            { return desired_size() / TLABRefillWasteFraction; }
  76 
  77   static int    target_refills()                 { return _target_refills; }
  78   size_t initial_desired_size();
  79 
  80   size_t remaining() const                       { return end() == NULL ? 0 : pointer_delta(hard_end(), top()); }
  81 
  82   bool is_last_allocation(HeapWord* obj, size_t size) { return pointer_delta(top(), obj) == size; }
  83 
  84   // Make parsable and release it.
  85   void reset();
  86 
  87   // Resize based on amount of allocation, etc.
  88   void resize();
  89 
  90   void invariants() const { assert(top() >= start() && top() <= end(), "invalid tlab"); }
  91 
  92   void initialize(HeapWord* start, HeapWord* top, HeapWord* end);
  93 
  94   void print_stats(const char* tag);
  95 
  96   Thread* myThread();
  97 
  98   // statistics
  99 
 100   int number_of_refills() const { return _number_of_refills; }
 101   int fast_refill_waste() const { return _fast_refill_waste; }
 102   int slow_refill_waste() const { return _slow_refill_waste; }
 103   int gc_waste() const          { return _gc_waste; }
 104   int slow_allocations() const  { return _slow_allocations; }
 105 
 106   static GlobalTLABStats* _global_stats;
 107   static GlobalTLABStats* global_stats() { return _global_stats; }
 108 
 109 public:
 110   ThreadLocalAllocBuffer() : _allocation_fraction(TLABAllocationWeight), _allocated_before_last_gc(0) {
 111     // do nothing.  tlabs must be inited by initialize() calls
 112   }
 113 
 114   static size_t min_size()                       { return align_object_size(MinTLABSize / HeapWordSize) + alignment_reserve(); }
 115   static size_t max_size()                       { assert(_max_size != 0, "max_size not set up"); return _max_size; }
 116   static size_t max_size_in_bytes()              { return max_size() * BytesPerWord; }
 117   static void set_max_size(size_t max_size)      { _max_size = max_size; }
 118 
 119   HeapWord* start() const                        { return _start; }
 120   HeapWord* end() const                          { return _end; }
 121   HeapWord* hard_end() const                     { return _end + alignment_reserve(); }
 122   HeapWord* top() const                          { return _top; }

 123   HeapWord* pf_top() const                       { return _pf_top; }
 124   size_t desired_size() const                    { return _desired_size; }
 125   size_t used() const                            { return pointer_delta(top(), start()); }
 126   size_t used_bytes() const                      { return pointer_delta(top(), start(), 1); }
 127   size_t free() const                            { return pointer_delta(end(), top()); }
 128   // Don't discard tlab if remaining space is larger than this.
 129   size_t refill_waste_limit() const              { return _refill_waste_limit; }

 130 
 131   // Allocate size HeapWords. The memory is NOT initialized to zero.
 132   inline HeapWord* allocate(size_t size);

 133 
 134   // Undo last allocation.
 135   inline bool undo_allocate(HeapWord* obj, size_t size);
 136 
 137   // Reserve space at the end of TLAB
 138   static size_t end_reserve() {
 139     int reserve_size = typeArrayOopDesc::header_size(T_INT);
 140     return MAX2(reserve_size, _reserve_for_allocation_prefetch);
 141   }
 142   static size_t alignment_reserve()              { return align_object_size(end_reserve()); }
 143   static size_t alignment_reserve_in_bytes()     { return alignment_reserve() * HeapWordSize; }
 144 
 145   // Return tlab size or remaining space in eden such that the
 146   // space is large enough to hold obj_size and necessary fill space.
 147   // Otherwise return 0;
 148   inline size_t compute_size(size_t obj_size);
 149 
 150   // Compute the minimal needed tlab size for the given object size.
 151   static inline size_t compute_min_size(size_t obj_size);
 152 
 153   // Record slow allocation
 154   inline void record_slow_allocation(size_t obj_size);
 155 
 156   // Initialization at startup
 157   static void startup_initialization();
 158 
 159   // Make an in-use tlab parsable, optionally retiring and/or zapping it.
 160   void make_parsable(bool retire, bool zap = true);
 161 
 162   // Retire in-use tlab before allocation of a new tlab
 163   void clear_before_allocation();
 164 
 165   // Accumulate statistics across all tlabs before gc
 166   static void accumulate_statistics_before_gc();
 167 
 168   // Resize tlabs for all threads
 169   static void resize_all_tlabs();
 170 
 171   void fill(HeapWord* start, HeapWord* top, size_t new_size);
 172   void initialize();



 173 
 174   static size_t refill_waste_limit_increment()   { return TLABWasteIncrement; }
 175 
 176   template <typename T> void addresses_do(T f) {
 177     f(&_start);
 178     f(&_top);
 179     f(&_pf_top);
 180     f(&_end);
 181   }
 182 
 183   // Code generation support
 184   static ByteSize start_offset()                 { return byte_offset_of(ThreadLocalAllocBuffer, _start); }
 185   static ByteSize end_offset()                   { return byte_offset_of(ThreadLocalAllocBuffer, _end  ); }
 186   static ByteSize top_offset()                   { return byte_offset_of(ThreadLocalAllocBuffer, _top  ); }
 187   static ByteSize pf_top_offset()                { return byte_offset_of(ThreadLocalAllocBuffer, _pf_top  ); }
 188   static ByteSize size_offset()                  { return byte_offset_of(ThreadLocalAllocBuffer, _desired_size ); }
 189   static ByteSize refill_waste_limit_offset()    { return byte_offset_of(ThreadLocalAllocBuffer, _refill_waste_limit ); }
 190 
 191   static ByteSize number_of_refills_offset()     { return byte_offset_of(ThreadLocalAllocBuffer, _number_of_refills ); }
 192   static ByteSize fast_refill_waste_offset()     { return byte_offset_of(ThreadLocalAllocBuffer, _fast_refill_waste ); }




  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_SHARED_THREADLOCALALLOCBUFFER_HPP
  26 #define SHARE_VM_GC_SHARED_THREADLOCALALLOCBUFFER_HPP
  27 
  28 #include "gc/shared/gcUtil.hpp"
  29 #include "oops/typeArrayOop.hpp"
  30 #include "runtime/perfData.hpp"
  31 #include "runtime/vm_version.hpp"
  32 
  33 class GlobalTLABStats;
  34 
  35 // ThreadLocalAllocBuffer: a descriptor for thread-local storage used by
  36 // the threads for allocation.
  37 //            It is thread-private at any time, but maybe multiplexed over
  38 //            time across multiple threads. The park()/unpark() pair is
  39 //            used to make it available for such multiplexing.
  40 //
  41 //            Heap sampling is performed via the end and allocation_end
  42 //            fields.
  43 //            allocation_end contains the real end of the tlab allocation,
  44 //            whereas end can be set to an arbitrary spot in the tlab to
  45 //            trip the return and sample the allocation.
  46 class ThreadLocalAllocBuffer: public CHeapObj<mtThread> {
  47   friend class VMStructs;
  48   friend class JVMCIVMStructs;
  49 private:
  50   HeapWord* _start;                              // address of TLAB
  51   HeapWord* _top;                                // address after last allocation
  52   HeapWord* _pf_top;                             // allocation prefetch watermark
  53   HeapWord* _end;                                // allocation end (can be the sampling end point or _allocation_end)
  54   HeapWord* _allocation_end;                     // end for allocations (actual TLAB end, excluding alignment_reserve)
  55 
  56   size_t    _desired_size;                       // desired size   (including alignment_reserve)
  57   size_t    _refill_waste_limit;                 // hold onto tlab if free() is larger than this
  58   size_t    _allocated_before_last_gc;           // total bytes allocated up until the last gc
  59   size_t    _bytes_since_last_sample_point;      // bytes since last sample point.
  60 
  61   static size_t   _max_size;                          // maximum size of any TLAB
  62   static int      _reserve_for_allocation_prefetch;   // Reserve at the end of the TLAB
  63   static unsigned _target_refills;                    // expected number of refills between GCs
  64 
  65   unsigned  _number_of_refills;
  66   unsigned  _fast_refill_waste;
  67   unsigned  _slow_refill_waste;
  68   unsigned  _gc_waste;
  69   unsigned  _slow_allocations;
  70   size_t    _allocated_size;
  71 
  72   AdaptiveWeightedAverage _allocation_fraction;  // fraction of eden allocated in tlabs
  73 
  74   void accumulate_statistics();
  75   void initialize_statistics();
  76 
  77   void set_start(HeapWord* start)                { _start = start; }
  78   void set_end(HeapWord* end)                    { _end = end; }
  79   void set_allocation_end(HeapWord* ptr)         { _allocation_end = ptr; }
  80   void set_top(HeapWord* top)                    { _top = top; }
  81   void set_pf_top(HeapWord* pf_top)              { _pf_top = pf_top; }
  82   void set_desired_size(size_t desired_size)     { _desired_size = desired_size; }
  83   void set_refill_waste_limit(size_t waste)      { _refill_waste_limit = waste;  }
  84 
  85   size_t initial_refill_waste_limit()            { return desired_size() / TLABRefillWasteFraction; }
  86 
  87   static int    target_refills()                 { return _target_refills; }
  88   size_t initial_desired_size();
  89 
  90   size_t remaining();
  91 
  92   bool is_last_allocation(HeapWord* obj, size_t size) { return pointer_delta(top(), obj) == size; }
  93 
  94   // Make parsable and release it.
  95   void reset();
  96 
  97   // Resize based on amount of allocation, etc.
  98   void resize();
  99 
 100   void invariants() const { assert(top() >= start() && top() <= end(), "invalid tlab"); }
 101 
 102   void initialize(HeapWord* start, HeapWord* top, HeapWord* end);
 103 
 104   void print_stats(const char* tag);
 105 
 106   Thread* myThread();
 107 
 108   // statistics
 109 
 110   int number_of_refills() const { return _number_of_refills; }
 111   int fast_refill_waste() const { return _fast_refill_waste; }
 112   int slow_refill_waste() const { return _slow_refill_waste; }
 113   int gc_waste() const          { return _gc_waste; }
 114   int slow_allocations() const  { return _slow_allocations; }
 115 
 116   static GlobalTLABStats* _global_stats;
 117   static GlobalTLABStats* global_stats() { return _global_stats; }
 118 
 119 public:
 120   ThreadLocalAllocBuffer() : _allocation_fraction(TLABAllocationWeight), _allocated_before_last_gc(0) {
 121     // do nothing.  tlabs must be inited by initialize() calls
 122   }
 123 
 124   static size_t min_size()                       { return align_object_size(MinTLABSize / HeapWordSize) + alignment_reserve(); }
 125   static size_t max_size()                       { assert(_max_size != 0, "max_size not set up"); return _max_size; }
 126   static size_t max_size_in_bytes()              { return max_size() * BytesPerWord; }
 127   static void set_max_size(size_t max_size)      { _max_size = max_size; }
 128 
 129   HeapWord* start() const                        { return _start; }
 130   HeapWord* end() const                          { return _end; }

 131   HeapWord* top() const                          { return _top; }
 132   HeapWord* hard_end();
 133   HeapWord* pf_top() const                       { return _pf_top; }
 134   size_t desired_size() const                    { return _desired_size; }
 135   size_t used() const                            { return pointer_delta(top(), start()); }
 136   size_t used_bytes() const                      { return pointer_delta(top(), start(), 1); }
 137   size_t free() const                            { return pointer_delta(end(), top()); }
 138   // Don't discard tlab if remaining space is larger than this.
 139   size_t refill_waste_limit() const              { return _refill_waste_limit; }
 140   size_t bytes_since_last_sample_point() const   { return _bytes_since_last_sample_point; }
 141 
 142   // Allocate size HeapWords. The memory is NOT initialized to zero.
 143   inline HeapWord* allocate(size_t size);
 144   HeapWord* allocate_sampled_object(size_t size);
 145 
 146   // Undo last allocation.
 147   inline bool undo_allocate(HeapWord* obj, size_t size);
 148 
 149   // Reserve space at the end of TLAB
 150   static size_t end_reserve() {
 151     int reserve_size = typeArrayOopDesc::header_size(T_INT);
 152     return MAX2(reserve_size, _reserve_for_allocation_prefetch);
 153   }
 154   static size_t alignment_reserve()              { return align_object_size(end_reserve()); }
 155   static size_t alignment_reserve_in_bytes()     { return alignment_reserve() * HeapWordSize; }
 156 
 157   // Return tlab size or remaining space in eden such that the
 158   // space is large enough to hold obj_size and necessary fill space.
 159   // Otherwise return 0;
 160   inline size_t compute_size(size_t obj_size);
 161 
 162   // Compute the minimal needed tlab size for the given object size.
 163   static inline size_t compute_min_size(size_t obj_size);
 164 
 165   // Record slow allocation
 166   inline void record_slow_allocation(size_t obj_size);
 167 
 168   // Initialization at startup
 169   static void startup_initialization();
 170 
 171   // Make an in-use tlab parsable, optionally retiring and/or zapping it.
 172   void make_parsable(bool retire, bool zap = true);
 173 
 174   // Retire in-use tlab before allocation of a new tlab
 175   void clear_before_allocation();
 176 
 177   // Accumulate statistics across all tlabs before gc
 178   static void accumulate_statistics_before_gc();
 179 
 180   // Resize tlabs for all threads
 181   static void resize_all_tlabs();
 182 
 183   void fill(HeapWord* start, HeapWord* top, size_t new_size);
 184   void initialize();
 185 
 186   void set_back_allocation_end();
 187   void set_sample_end();
 188 
 189   static size_t refill_waste_limit_increment()   { return TLABWasteIncrement; }
 190 
 191   template <typename T> void addresses_do(T f) {
 192     f(&_start);
 193     f(&_top);
 194     f(&_pf_top);
 195     f(&_end);
 196   }
 197 
 198   // Code generation support
 199   static ByteSize start_offset()                 { return byte_offset_of(ThreadLocalAllocBuffer, _start); }
 200   static ByteSize end_offset()                   { return byte_offset_of(ThreadLocalAllocBuffer, _end  ); }
 201   static ByteSize top_offset()                   { return byte_offset_of(ThreadLocalAllocBuffer, _top  ); }
 202   static ByteSize pf_top_offset()                { return byte_offset_of(ThreadLocalAllocBuffer, _pf_top  ); }
 203   static ByteSize size_offset()                  { return byte_offset_of(ThreadLocalAllocBuffer, _desired_size ); }
 204   static ByteSize refill_waste_limit_offset()    { return byte_offset_of(ThreadLocalAllocBuffer, _refill_waste_limit ); }
 205 
 206   static ByteSize number_of_refills_offset()     { return byte_offset_of(ThreadLocalAllocBuffer, _number_of_refills ); }
 207   static ByteSize fast_refill_waste_offset()     { return byte_offset_of(ThreadLocalAllocBuffer, _fast_refill_waste ); }


< prev index next >