rev 52072 : 8211955: GC abstraction for LAB reserve

   1 /*
   2  * Copyright (c) 2001, 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 #include "precompiled.hpp"
  26 #include "gc/shared/collectedHeap.hpp"
  27 #include "gc/shared/plab.inline.hpp"
  28 #include "gc/shared/threadLocalAllocBuffer.hpp"
  29 #include "logging/log.hpp"

  30 #include "oops/oop.inline.hpp"
  31 
  32 size_t PLAB::min_size() {
  33   // Make sure that we return something that is larger than AlignmentReserve
  34   return align_object_size(MAX2(MinTLABSize / HeapWordSize, (size_t)oopDesc::header_size())) + AlignmentReserve;
  35 }
  36 
  37 size_t PLAB::max_size() {
  38   return ThreadLocalAllocBuffer::max_size();
  39 }
  40 
  41 PLAB::PLAB(size_t desired_plab_sz_) :
  42   _word_sz(desired_plab_sz_), _bottom(NULL), _top(NULL),
  43   _end(NULL), _hard_end(NULL), _allocated(0), _wasted(0), _undo_wasted(0)
  44 {
  45   AlignmentReserve = Universe::heap()->tlab_alloc_reserve();

  46   assert(min_size() > AlignmentReserve,
  47          "Minimum PLAB size " SIZE_FORMAT " must be larger than alignment reserve " SIZE_FORMAT " "
  48          "to be able to contain objects", min_size(), AlignmentReserve);
  49 }
  50 
  51 // If the minimum object size is greater than MinObjAlignment, we can
  52 // end up with a shard at the end of the buffer that's smaller than
  53 // the smallest object.  We can't allow that because the buffer must
  54 // look like it's full of objects when we retire it, so we make
  55 // sure we have enough space for a filler int array object.
  56 size_t PLAB::AlignmentReserve;
  57 
  58 void PLAB::flush_and_retire_stats(PLABStats* stats) {
  59   // Retire the last allocation buffer.
  60   size_t unused = retire_internal();
  61 
  62   // Now flush the statistics.
  63   stats->add_allocated(_allocated);
  64   stats->add_wasted(_wasted);
  65   stats->add_undo_wasted(_undo_wasted);
  66   stats->add_unused(unused);
  67 
  68   // Since we have flushed the stats we need to clear  the _allocated and _wasted
  69   // fields in case somebody retains an instance of this over GCs. Not doing so
  70   // will artifically inflate the values in the statistics.
  71   _allocated   = 0;
  72   _wasted      = 0;
  73   _undo_wasted = 0;
  74 }
  75 
  76 void PLAB::retire() {
  77   _wasted += retire_internal();
  78 }
  79 
  80 size_t PLAB::retire_internal() {
  81   size_t result = 0;
  82   if (_top < _hard_end) {
  83     Universe::heap()->fill_with_dummy_object(_top, _hard_end, true);
  84     result += invalidate();
  85   }
  86   return result;
  87 }
  88 
  89 void PLAB::add_undo_waste(HeapWord* obj, size_t word_sz) {
  90   Universe::heap()->fill_with_dummy_object(obj, obj + word_sz, true);
  91   _undo_wasted += word_sz;
  92 }
  93 
  94 void PLAB::undo_last_allocation(HeapWord* obj, size_t word_sz) {
  95   assert(pointer_delta(_top, _bottom) >= word_sz, "Bad undo");
  96   assert(pointer_delta(_top, obj) == word_sz, "Bad undo");
  97   _top = obj;
  98 }
  99 
 100 void PLAB::undo_allocation(HeapWord* obj, size_t word_sz) {
 101   // Is the alloc in the current alloc buffer?
 102   if (contains(obj)) {
 103     assert(contains(obj + word_sz - 1),
 104       "should contain whole object");
 105     undo_last_allocation(obj, word_sz);
 106   } else {
 107     add_undo_waste(obj, word_sz);
 108   }
 109 }
 110 
 111 void PLABStats::log_plab_allocation() {
 112   log_debug(gc, plab)("%s PLAB allocation: "
 113                       "allocated: " SIZE_FORMAT "B, "
 114                       "wasted: " SIZE_FORMAT "B, "
 115                       "unused: " SIZE_FORMAT "B, "
 116                       "used: " SIZE_FORMAT "B, "
 117                       "undo waste: " SIZE_FORMAT "B, ",
 118                       _description,
 119                       _allocated * HeapWordSize,
 120                       _wasted * HeapWordSize,
 121                       _unused * HeapWordSize,
 122                       used() * HeapWordSize,
 123                       _undo_wasted * HeapWordSize);
 124 }
 125 
 126 void PLABStats::log_sizing(size_t calculated_words, size_t net_desired_words) {
 127   log_debug(gc, plab)("%s sizing: "
 128                       "calculated: " SIZE_FORMAT "B, "
 129                       "actual: " SIZE_FORMAT "B",
 130                       _description,
 131                       calculated_words * HeapWordSize,
 132                       net_desired_words * HeapWordSize);
 133 }
 134 
 135 // Calculates plab size for current number of gc worker threads.
 136 size_t PLABStats::desired_plab_sz(uint no_of_gc_workers) {
 137   return align_object_size(MIN2(MAX2(min_size(), _desired_net_plab_sz / no_of_gc_workers), max_size()));
 138 }
 139 
 140 // Compute desired plab size for one gc worker thread and latch result for later
 141 // use. This should be called once at the end of parallel
 142 // scavenge; it clears the sensor accumulators.
 143 void PLABStats::adjust_desired_plab_sz() {
 144   log_plab_allocation();
 145 
 146   if (!ResizePLAB) {
 147     // Clear accumulators for next round.
 148     reset();
 149     return;
 150   }
 151 
 152   assert(is_object_aligned(max_size()) && min_size() <= max_size(),
 153          "PLAB clipping computation may be incorrect");
 154 
 155   assert(_allocated != 0 || _unused == 0,
 156          "Inconsistency in PLAB stats: "
 157          "_allocated: " SIZE_FORMAT ", "
 158          "_wasted: " SIZE_FORMAT ", "
 159          "_unused: " SIZE_FORMAT ", "
 160          "_undo_wasted: " SIZE_FORMAT,
 161          _allocated, _wasted, _unused, _undo_wasted);
 162 
 163   size_t plab_sz = compute_desired_plab_sz();
 164   // Take historical weighted average
 165   _filter.sample(plab_sz);
 166   _desired_net_plab_sz = MAX2(min_size(), (size_t)_filter.average());
 167 
 168   log_sizing(plab_sz, _desired_net_plab_sz);
 169   // Clear accumulators for next round
 170   reset();
 171 }
 172 
 173 size_t PLABStats::compute_desired_plab_sz() {
 174   size_t allocated      = MAX2(_allocated, size_t(1));
 175   double wasted_frac    = (double)_unused / (double)allocated;
 176   size_t target_refills = (size_t)((wasted_frac * TargetSurvivorRatio) / TargetPLABWastePct);
 177   if (target_refills == 0) {
 178     target_refills = 1;
 179   }
 180   size_t used = allocated - _wasted - _unused;
 181   // Assumed to have 1 gc worker thread
 182   size_t recent_plab_sz = used / target_refills;
 183   return recent_plab_sz;
 184 }
--- EOF ---