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   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 ); }
 208   static ByteSize slow_allocations_offset()      { return byte_offset_of(ThreadLocalAllocBuffer, _slow_allocations ); }
 209 
 210   void verify();
 211 };
 212 
 213 class GlobalTLABStats: public CHeapObj<mtThread> {
 214 private:
 215 
 216   // Accumulate perfdata in private variables because
 217   // PerfData should be write-only for security reasons
 218   // (see perfData.hpp)
 219   unsigned _allocating_threads;
 220   unsigned _total_refills;
 221   unsigned _max_refills;
 222   size_t   _total_allocation;
 223   size_t   _total_gc_waste;
 224   size_t   _max_gc_waste;
 225   size_t   _total_slow_refill_waste;
 226   size_t   _max_slow_refill_waste;
 227   size_t   _total_fast_refill_waste;
 228   size_t   _max_fast_refill_waste;
 229   unsigned _total_slow_allocations;
 230   unsigned _max_slow_allocations;
 231 
 232   PerfVariable* _perf_allocating_threads;
 233   PerfVariable* _perf_total_refills;
 234   PerfVariable* _perf_max_refills;
 235   PerfVariable* _perf_allocation;
 236   PerfVariable* _perf_gc_waste;
 237   PerfVariable* _perf_max_gc_waste;
 238   PerfVariable* _perf_slow_refill_waste;
 239   PerfVariable* _perf_max_slow_refill_waste;
 240   PerfVariable* _perf_fast_refill_waste;
 241   PerfVariable* _perf_max_fast_refill_waste;
 242   PerfVariable* _perf_slow_allocations;
 243   PerfVariable* _perf_max_slow_allocations;
 244 
 245   AdaptiveWeightedAverage _allocating_threads_avg;
 246 
 247 public:
 248   GlobalTLABStats();
 249 
 250   // Initialize all counters
 251   void initialize();
 252 
 253   // Write all perf counters to the perf_counters
 254   void publish();
 255 
 256   void print();
 257 
 258   // Accessors
 259   unsigned allocating_threads_avg() {
 260     return MAX2((unsigned)(_allocating_threads_avg.average() + 0.5), 1U);
 261   }
 262 
 263   size_t allocation() {
 264     return _total_allocation;
 265   }
 266 
 267   // Update methods
 268 
 269   void update_allocating_threads() {
 270     _allocating_threads++;
 271   }
 272   void update_number_of_refills(unsigned value) {
 273     _total_refills += value;
 274     _max_refills    = MAX2(_max_refills, value);
 275   }
 276   void update_allocation(size_t value) {
 277     _total_allocation += value;
 278   }
 279   void update_gc_waste(size_t value) {
 280     _total_gc_waste += value;
 281     _max_gc_waste    = MAX2(_max_gc_waste, value);
 282   }
 283   void update_fast_refill_waste(size_t value) {
 284     _total_fast_refill_waste += value;
 285     _max_fast_refill_waste    = MAX2(_max_fast_refill_waste, value);
 286   }
 287   void update_slow_refill_waste(size_t value) {
 288     _total_slow_refill_waste += value;
 289     _max_slow_refill_waste    = MAX2(_max_slow_refill_waste, value);
 290   }
 291   void update_slow_allocations(unsigned value) {
 292     _total_slow_allocations += value;
 293     _max_slow_allocations    = MAX2(_max_slow_allocations, value);
 294   }
 295 };
 296 
 297 #endif // SHARE_VM_GC_SHARED_THREADLOCALALLOCBUFFER_HPP