Print this page
8236073: G1: Use SoftMaxHeapSize to guide GC heuristics


   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/g1/g1CollectedHeap.hpp"
  27 #include "gc/g1/g1HeapSizingPolicy.hpp"
  28 #include "gc/g1/g1Analytics.hpp"

  29 #include "logging/log.hpp"
  30 #include "runtime/globals.hpp"
  31 #include "utilities/debug.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 G1HeapSizingPolicy* G1HeapSizingPolicy::create(const G1CollectedHeap* g1h, const G1Analytics* analytics) {
  35   return new G1HeapSizingPolicy(g1h, analytics);
  36 }
  37 
  38 G1HeapSizingPolicy::G1HeapSizingPolicy(const G1CollectedHeap* g1h, const G1Analytics* analytics) :
  39   _g1h(g1h),
  40   _analytics(analytics),
  41   _num_prev_pauses_for_heuristics(analytics->number_of_recorded_pause_times()) {
  42 
  43   assert(MinOverThresholdForGrowth < _num_prev_pauses_for_heuristics, "Threshold must be less than %u", _num_prev_pauses_for_heuristics);
  44   clear_ratio_check_data();
  45 }
  46 
  47 void G1HeapSizingPolicy::clear_ratio_check_data() {
  48   _ratio_over_threshold_count = 0;


  71 
  72   // If the last GC time ratio is over the threshold, increment the count of
  73   // times it has been exceeded, and add this ratio to the sum of exceeded
  74   // ratios.
  75   if (last_gc_overhead > threshold) {
  76     _ratio_over_threshold_count++;
  77     _ratio_over_threshold_sum += last_gc_overhead;
  78   }
  79 
  80   // Check if we've had enough GC time ratio checks that were over the
  81   // threshold to trigger an expansion. We'll also expand if we've
  82   // reached the end of the history buffer and the average of all entries
  83   // is still over the threshold. This indicates a smaller number of GCs were
  84   // long enough to make the average exceed the threshold.
  85   bool filled_history_buffer = _pauses_since_start == _num_prev_pauses_for_heuristics;
  86   if ((_ratio_over_threshold_count == MinOverThresholdForGrowth) ||
  87       (filled_history_buffer && (recent_gc_overhead > threshold))) {
  88     size_t min_expand_bytes = HeapRegion::GrainBytes;
  89     size_t reserved_bytes = _g1h->max_capacity();
  90     size_t committed_bytes = _g1h->capacity();




  91     size_t uncommitted_bytes = reserved_bytes - committed_bytes;
  92     size_t expand_bytes_via_pct =
  93       uncommitted_bytes * G1ExpandByPercentOfAvailable / 100;
  94     double scale_factor = 1.0;
  95 
  96     // If the current size is less than 1/4 of the Initial heap size, expand
  97     // by half of the delta between the current and Initial sizes. IE, grow
  98     // back quickly.
  99     //
 100     // Otherwise, take the current size, or G1ExpandByPercentOfAvailable % of
 101     // the available expansion space, whichever is smaller, as the base
 102     // expansion size. Then possibly scale this size according to how much the
 103     // threshold has (on average) been exceeded by. If the delta is small
 104     // (less than the StartScaleDownAt value), scale the size down linearly, but
 105     // not by less than MinScaleDownFactor. If the delta is large (greater than
 106     // the StartScaleUpAt value), scale up, but adding no more than MaxScaleUpFactor
 107     // times the base size. The scaling will be linear in the range from
 108     // StartScaleUpAt to (StartScaleUpAt + ScaleUpRange). In other words,
 109     // ScaleUpRange sets the rate of scaling up.
 110     if (committed_bytes < InitialHeapSize / 4) {


 142     // Ensure the expansion size is at least the minimum growth amount
 143     // and at most the remaining uncommitted byte size.
 144     expand_bytes = MAX2(expand_bytes, min_expand_bytes);
 145     expand_bytes = MIN2(expand_bytes, uncommitted_bytes);
 146 
 147     clear_ratio_check_data();
 148   } else {
 149     // An expansion was not triggered. If we've started counting, increment
 150     // the number of checks we've made in the current window.  If we've
 151     // reached the end of the window without resizing, clear the counters to
 152     // start again the next time we see a ratio above the threshold.
 153     if (_ratio_over_threshold_count > 0) {
 154       _pauses_since_start++;
 155       if (_pauses_since_start > _num_prev_pauses_for_heuristics) {
 156         clear_ratio_check_data();
 157       }
 158     }
 159   }
 160 
 161   return expand_bytes;


























 162 }


   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/g1/g1CollectedHeap.hpp"
  27 #include "gc/g1/g1HeapSizingPolicy.hpp"
  28 #include "gc/g1/g1Analytics.hpp"
  29 #include "gc/g1/g1Policy.hpp"
  30 #include "logging/log.hpp"
  31 #include "runtime/globals.hpp"
  32 #include "utilities/debug.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 G1HeapSizingPolicy* G1HeapSizingPolicy::create(const G1CollectedHeap* g1h, const G1Analytics* analytics) {
  36   return new G1HeapSizingPolicy(g1h, analytics);
  37 }
  38 
  39 G1HeapSizingPolicy::G1HeapSizingPolicy(const G1CollectedHeap* g1h, const G1Analytics* analytics) :
  40   _g1h(g1h),
  41   _analytics(analytics),
  42   _num_prev_pauses_for_heuristics(analytics->number_of_recorded_pause_times()) {
  43 
  44   assert(MinOverThresholdForGrowth < _num_prev_pauses_for_heuristics, "Threshold must be less than %u", _num_prev_pauses_for_heuristics);
  45   clear_ratio_check_data();
  46 }
  47 
  48 void G1HeapSizingPolicy::clear_ratio_check_data() {
  49   _ratio_over_threshold_count = 0;


  72 
  73   // If the last GC time ratio is over the threshold, increment the count of
  74   // times it has been exceeded, and add this ratio to the sum of exceeded
  75   // ratios.
  76   if (last_gc_overhead > threshold) {
  77     _ratio_over_threshold_count++;
  78     _ratio_over_threshold_sum += last_gc_overhead;
  79   }
  80 
  81   // Check if we've had enough GC time ratio checks that were over the
  82   // threshold to trigger an expansion. We'll also expand if we've
  83   // reached the end of the history buffer and the average of all entries
  84   // is still over the threshold. This indicates a smaller number of GCs were
  85   // long enough to make the average exceed the threshold.
  86   bool filled_history_buffer = _pauses_since_start == _num_prev_pauses_for_heuristics;
  87   if ((_ratio_over_threshold_count == MinOverThresholdForGrowth) ||
  88       (filled_history_buffer && (recent_gc_overhead > threshold))) {
  89     size_t min_expand_bytes = HeapRegion::GrainBytes;
  90     size_t reserved_bytes = _g1h->max_capacity();
  91     size_t committed_bytes = _g1h->capacity();
  92     if (committed_bytes <= SoftMaxHeapSize) {
  93       // Use SoftMaxHeapSize to limit the max size
  94       reserved_bytes = SoftMaxHeapSize;
  95     }
  96     size_t uncommitted_bytes = reserved_bytes - committed_bytes;
  97     size_t expand_bytes_via_pct =
  98       uncommitted_bytes * G1ExpandByPercentOfAvailable / 100;
  99     double scale_factor = 1.0;
 100 
 101     // If the current size is less than 1/4 of the Initial heap size, expand
 102     // by half of the delta between the current and Initial sizes. IE, grow
 103     // back quickly.
 104     //
 105     // Otherwise, take the current size, or G1ExpandByPercentOfAvailable % of
 106     // the available expansion space, whichever is smaller, as the base
 107     // expansion size. Then possibly scale this size according to how much the
 108     // threshold has (on average) been exceeded by. If the delta is small
 109     // (less than the StartScaleDownAt value), scale the size down linearly, but
 110     // not by less than MinScaleDownFactor. If the delta is large (greater than
 111     // the StartScaleUpAt value), scale up, but adding no more than MaxScaleUpFactor
 112     // times the base size. The scaling will be linear in the range from
 113     // StartScaleUpAt to (StartScaleUpAt + ScaleUpRange). In other words,
 114     // ScaleUpRange sets the rate of scaling up.
 115     if (committed_bytes < InitialHeapSize / 4) {


 147     // Ensure the expansion size is at least the minimum growth amount
 148     // and at most the remaining uncommitted byte size.
 149     expand_bytes = MAX2(expand_bytes, min_expand_bytes);
 150     expand_bytes = MIN2(expand_bytes, uncommitted_bytes);
 151 
 152     clear_ratio_check_data();
 153   } else {
 154     // An expansion was not triggered. If we've started counting, increment
 155     // the number of checks we've made in the current window.  If we've
 156     // reached the end of the window without resizing, clear the counters to
 157     // start again the next time we see a ratio above the threshold.
 158     if (_ratio_over_threshold_count > 0) {
 159       _pauses_since_start++;
 160       if (_pauses_since_start > _num_prev_pauses_for_heuristics) {
 161         clear_ratio_check_data();
 162       }
 163     }
 164   }
 165 
 166   return expand_bytes;
 167 }
 168 
 169 bool G1HeapSizingPolicy::can_shrink_heap_size_to(size_t heap_size) {
 170   size_t cur_used_bytes = _g1h->non_young_capacity_bytes();
 171   uint used_regions = cur_used_bytes / HeapRegion::GrainBytes;
 172   uint new_number_of_regions = heap_size / HeapRegion::GrainBytes;
 173   // re-calculate the necessary reserve
 174   double reserve_regions_d = (double) new_number_of_regions * _g1h->policy()->_reserve_factor;
 175   // We use ceiling so that if reserve_regions_d is > 0.0 (but
 176   // smaller than 1.0) we'll get 1.
 177   uint reserve_regions = (uint) ceil(reserve_regions_d);
 178   if (new_number_of_regions <= (reserve_regions + used_regions)) {
 179     // No left for young generation
 180     return false;
 181   }
 182   // Rest region number for young gen
 183   uint young_regions = new_number_of_regions - reserve_regions - used_regions;
 184 
 185   // re-calculate the young length
 186   uint min_young_length;
 187   uint max_young_length;
 188   _g1h->policy()->_young_gen_sizer->recalculate_min_max_young_length(new_number_of_regions,
 189                                                                      &min_young_length,
 190                                                                      &max_young_length);
 191   // Rest young region length must be larger than min young length
 192   return young_regions >= max_young_length;
 193 }