1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  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   // Make parsable and release it.
  93   void reset();
  94 
  95   void invariants() const { assert(top() >= start() && top() <= end(), "invalid tlab"); }
  96 
  97   void initialize(HeapWord* start, HeapWord* top, HeapWord* end);
  98 
  99   void print_stats(const char* tag);
 100 
 101   Thread* thread();
 102 
 103   // statistics
 104 
 105   int number_of_refills() const { return _number_of_refills; }
 106   int fast_refill_waste() const { return _fast_refill_waste; }
 107   int slow_refill_waste() const { return _slow_refill_waste; }
 108   int gc_waste() const          { return _gc_waste; }
 109   int slow_allocations() const  { return _slow_allocations; }
 110 
 111   static GlobalTLABStats* _global_stats;
 112   static GlobalTLABStats* global_stats() { return _global_stats; }
 113 
 114 public:
 115   ThreadLocalAllocBuffer() : _allocated_before_last_gc(0), _allocation_fraction(TLABAllocationWeight) {
 116     // do nothing.  tlabs must be inited by initialize() calls
 117   }
 118 
 119   static size_t min_size()                       { return align_object_size(MinTLABSize / HeapWordSize) + alignment_reserve(); }
 120   static size_t max_size()                       { assert(_max_size != 0, "max_size not set up"); return _max_size; }
 121   static size_t max_size_in_bytes()              { return max_size() * BytesPerWord; }
 122   static void set_max_size(size_t max_size)      { _max_size = max_size; }
 123 
 124   HeapWord* start() const                        { return _start; }
 125   HeapWord* end() const                          { return _end; }
 126   HeapWord* top() const                          { return _top; }
 127   HeapWord* hard_end();
 128   HeapWord* pf_top() const                       { return _pf_top; }
 129   size_t desired_size() const                    { return _desired_size; }
 130   size_t used() const                            { return pointer_delta(top(), start()); }
 131   size_t used_bytes() const                      { return pointer_delta(top(), start(), 1); }
 132   size_t free() const                            { return pointer_delta(end(), top()); }
 133   // Don't discard tlab if remaining space is larger than this.
 134   size_t refill_waste_limit() const              { return _refill_waste_limit; }
 135   size_t bytes_since_last_sample_point() const   { return _bytes_since_last_sample_point; }
 136 
 137   // Allocate size HeapWords. The memory is NOT initialized to zero.
 138   inline HeapWord* allocate(size_t size);
 139 
 140   // Reserve space at the end of TLAB
 141   static size_t end_reserve() {
 142     int reserve_size = typeArrayOopDesc::header_size(T_INT);
 143     return MAX2(reserve_size, _reserve_for_allocation_prefetch);
 144   }
 145   static size_t alignment_reserve()              { return align_object_size(end_reserve()); }
 146   static size_t alignment_reserve_in_bytes()     { return alignment_reserve() * HeapWordSize; }
 147 
 148   // Return tlab size or remaining space in eden such that the
 149   // space is large enough to hold obj_size and necessary fill space.
 150   // Otherwise return 0;
 151   inline size_t compute_size(size_t obj_size);
 152 
 153   // Compute the minimal needed tlab size for the given object size.
 154   static inline size_t compute_min_size(size_t obj_size);
 155 
 156   // Record slow allocation
 157   inline void record_slow_allocation(size_t obj_size);
 158 
 159   // Initialization at startup
 160   static void startup_initialization();
 161 
 162   // Make an in-use tlab parsable, optionally retiring and/or zapping it.
 163   void make_parsable(bool retire, bool zap = true);
 164 
 165   // Retire in-use tlab before allocation of a new tlab
 166   void clear_before_allocation();
 167 
 168   // Resize based on amount of allocation, etc.
 169   void resize();
 170 
 171   // Accumulate statistics across all tlabs before gc
 172   static void accumulate_statistics_before_gc();
 173 
 174   void fill(HeapWord* start, HeapWord* top, size_t new_size);
 175   void initialize();
 176 
 177   void set_back_allocation_end();
 178   void set_sample_end();
 179 
 180   static size_t refill_waste_limit_increment()   { return TLABWasteIncrement; }
 181 
 182   template <typename T> void addresses_do(T f) {
 183     f(&_start);
 184     f(&_top);
 185     f(&_pf_top);
 186     f(&_end);
 187     f(&_allocation_end);
 188   }
 189 
 190   // Code generation support
 191   static ByteSize start_offset()                 { return byte_offset_of(ThreadLocalAllocBuffer, _start); }
 192   static ByteSize end_offset()                   { return byte_offset_of(ThreadLocalAllocBuffer, _end); }
 193   static ByteSize top_offset()                   { return byte_offset_of(ThreadLocalAllocBuffer, _top); }
 194   static ByteSize pf_top_offset()                { return byte_offset_of(ThreadLocalAllocBuffer, _pf_top); }
 195 
 196   void verify();
 197 };
 198 
 199 class GlobalTLABStats: public CHeapObj<mtThread> {
 200 private:
 201 
 202   // Accumulate perfdata in private variables because
 203   // PerfData should be write-only for security reasons
 204   // (see perfData.hpp)
 205   unsigned _allocating_threads;
 206   unsigned _total_refills;
 207   unsigned _max_refills;
 208   size_t   _total_allocation;
 209   size_t   _total_gc_waste;
 210   size_t   _max_gc_waste;
 211   size_t   _total_slow_refill_waste;
 212   size_t   _max_slow_refill_waste;
 213   size_t   _total_fast_refill_waste;
 214   size_t   _max_fast_refill_waste;
 215   unsigned _total_slow_allocations;
 216   unsigned _max_slow_allocations;
 217 
 218   PerfVariable* _perf_allocating_threads;
 219   PerfVariable* _perf_total_refills;
 220   PerfVariable* _perf_max_refills;
 221   PerfVariable* _perf_allocation;
 222   PerfVariable* _perf_gc_waste;
 223   PerfVariable* _perf_max_gc_waste;
 224   PerfVariable* _perf_slow_refill_waste;
 225   PerfVariable* _perf_max_slow_refill_waste;
 226   PerfVariable* _perf_fast_refill_waste;
 227   PerfVariable* _perf_max_fast_refill_waste;
 228   PerfVariable* _perf_slow_allocations;
 229   PerfVariable* _perf_max_slow_allocations;
 230 
 231   AdaptiveWeightedAverage _allocating_threads_avg;
 232 
 233 public:
 234   GlobalTLABStats();
 235 
 236   // Initialize all counters
 237   void initialize();
 238 
 239   // Write all perf counters to the perf_counters
 240   void publish();
 241 
 242   void print();
 243 
 244   // Accessors
 245   unsigned allocating_threads_avg() {
 246     return MAX2((unsigned)(_allocating_threads_avg.average() + 0.5), 1U);
 247   }
 248 
 249   size_t allocation() {
 250     return _total_allocation;
 251   }
 252 
 253   // Update methods
 254 
 255   void update_allocating_threads() {
 256     _allocating_threads++;
 257   }
 258   void update_number_of_refills(unsigned value) {
 259     _total_refills += value;
 260     _max_refills    = MAX2(_max_refills, value);
 261   }
 262   void update_allocation(size_t value) {
 263     _total_allocation += value;
 264   }
 265   void update_gc_waste(size_t value) {
 266     _total_gc_waste += value;
 267     _max_gc_waste    = MAX2(_max_gc_waste, value);
 268   }
 269   void update_fast_refill_waste(size_t value) {
 270     _total_fast_refill_waste += value;
 271     _max_fast_refill_waste    = MAX2(_max_fast_refill_waste, value);
 272   }
 273   void update_slow_refill_waste(size_t value) {
 274     _total_slow_refill_waste += value;
 275     _max_slow_refill_waste    = MAX2(_max_slow_refill_waste, value);
 276   }
 277   void update_slow_allocations(unsigned value) {
 278     _total_slow_allocations += value;
 279     _max_slow_allocations    = MAX2(_max_slow_allocations, value);
 280   }
 281 };
 282 
 283 #endif // SHARE_VM_GC_SHARED_THREADLOCALALLOCBUFFER_HPP