< prev index next >

src/share/vm/gc_implementation/shenandoah/heuristics/shenandoahAdaptiveHeuristics.cpp

Print this page
rev 10519 : [backport] Adaptive CSet selection selects excessively when memory is tight
rev 10544 : [backport] Adaptive CSet selection overshoots max-CSet
rev 10584 : [backport] Move periodic GC decision making to GC heuristics base class
rev 10598 : [backport] Shenandoah changes to allow enabling -Wreorder
rev 10604 : [backport] Comprehensible GC trigger logging
rev 10606 : [backport] Adaptive/Traversal heuristics rewrite for allocation rate
rev 10620 : [backport] Evac reserve: make sure GC has untouchable space to move the objects into

@@ -29,15 +29,13 @@
 #include "gc_implementation/shenandoah/shenandoahLogging.hpp"
 #include "utilities/quickSort.hpp"
 
 ShenandoahAdaptiveHeuristics::ShenandoahAdaptiveHeuristics() :
   ShenandoahHeuristics(),
-  _free_threshold(ShenandoahInitFreeThreshold),
-  _peak_occupancy(0),
+  _cycle_gap_history(new TruncatedSeq(5)),
   _conc_mark_duration_history(new TruncatedSeq(5)),
-  _conc_uprefs_duration_history(new TruncatedSeq(5)),
-  _cycle_gap_history(new TruncatedSeq(5)) {
+  _conc_uprefs_duration_history(new TruncatedSeq(5)) {
 }
 
 ShenandoahAdaptiveHeuristics::~ShenandoahAdaptiveHeuristics() {}
 
 void ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,

@@ -53,86 +51,55 @@
   //      available space. In non-overloaded heap, max_cset would contain all plausible candidates
   //      over garbage threshold.
   //
   //   2. We should not get cset too low so that free threshold would not be met right
   //      after the cycle. Otherwise we get back-to-back cycles for no reason if heap is
-  //      too fragmented. In non-overloaded non-fragmented heap min_cset would be around zero.
+  //      too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero.
   //
   // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates
-  // before we meet min_cset. Then we add all candidates that fit with a garbage threshold before
+  // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before
   // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme,
-  // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_cset is hit.
+  // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit.
 
-  size_t free_target = MIN2<size_t>(_free_threshold + MaxNormalStep, 100) * ShenandoahHeap::heap()->capacity() / 100;
-  size_t min_cset = free_target > actual_free ? (free_target - actual_free) : 0;
-  size_t max_cset = actual_free * 3 / 4;
-  min_cset = MIN2(min_cset, max_cset);
+  size_t capacity    = ShenandoahHeap::heap()->capacity();
+  size_t free_target = ShenandoahMinFreeThreshold * capacity / 100;
+  size_t min_garbage = free_target > actual_free ? (free_target - actual_free) : 0;
+  size_t max_cset    = (size_t)(1.0 * ShenandoahEvacReserve * capacity / 100 / ShenandoahEvacWaste);
 
   log_info(gc, ergo)("Adaptive CSet Selection. Target Free: " SIZE_FORMAT "M, Actual Free: "
-                             SIZE_FORMAT "M, Target CSet: [" SIZE_FORMAT "M, " SIZE_FORMAT "M]",
-                     free_target / M, actual_free / M, min_cset / M, max_cset / M);
+                     SIZE_FORMAT "M, Max CSet: " SIZE_FORMAT "M, Min Garbage: " SIZE_FORMAT "M",
+                     free_target / M, actual_free / M, max_cset / M, min_garbage / M);
 
   // Better select garbage-first regions
   QuickSort::sort<RegionData>(data, (int)size, compare_by_garbage, false);
 
-  size_t live_cset = 0;
+  size_t cur_cset = 0;
+  size_t cur_garbage = 0;
   _bytes_in_cset = 0;
+
   for (size_t idx = 0; idx < size; idx++) {
     ShenandoahHeapRegion* r = data[idx]._region;
 
-    size_t new_cset = live_cset + r->get_live_data_bytes();
+    size_t new_cset    = cur_cset + r->get_live_data_bytes();
+    size_t new_garbage = cur_garbage + r->garbage();
 
-    if (new_cset < min_cset) {
-      cset->add_region(r);
-      _bytes_in_cset += r->used();
-      live_cset = new_cset;
-    } else if (new_cset <= max_cset) {
-      if (r->garbage() > garbage_threshold) {
-        cset->add_region(r);
-        _bytes_in_cset += r->used();
-        live_cset = new_cset;
-      }
-    } else {
+    if (new_cset > max_cset) {
       break;
     }
-  }
-}
 
-void ShenandoahAdaptiveHeuristics::handle_cycle_success() {
-  ShenandoahHeap* heap = ShenandoahHeap::heap();
-  size_t capacity = heap->capacity();
-
-  size_t current_threshold = (capacity - _peak_occupancy) * 100 / capacity;
-  size_t min_threshold = ShenandoahMinFreeThreshold;
-  intx step = min_threshold - current_threshold;
-  step = MAX2(step, (intx) -MaxNormalStep);
-  step = MIN2(step, (intx) MaxNormalStep);
-
-  log_info(gc, ergo)("Capacity: " SIZE_FORMAT "M, Peak Occupancy: " SIZE_FORMAT
-                             "M, Lowest Free: " SIZE_FORMAT "M, Free Threshold: " UINTX_FORMAT "M",
-                     capacity / M, _peak_occupancy / M,
-                     (capacity - _peak_occupancy) / M, ShenandoahMinFreeThreshold * capacity / 100 / M);
-
-  if (step > 0) {
-    // Pessimize
-    adjust_free_threshold(step);
-  } else if (step < 0) {
-    // Optimize, if enough happy cycles happened
-    if (_successful_cycles_in_a_row > ShenandoahHappyCyclesThreshold &&
-        _free_threshold > 0) {
-      adjust_free_threshold(step);
-      _successful_cycles_in_a_row = 0;
+    if ((new_garbage < min_garbage) || (r->garbage() > garbage_threshold)) {
+      cset->add_region(r);
+      _bytes_in_cset += r->used();
+      cur_cset = new_cset;
+      cur_garbage = new_garbage;
     }
-  } else {
-    // do nothing
   }
-  _peak_occupancy = 0;
 }
 
 void ShenandoahAdaptiveHeuristics::record_cycle_start() {
   ShenandoahHeuristics::record_cycle_start();
-  double last_cycle_gap = (os::elapsedTime() - _last_cycle_end);
+  double last_cycle_gap = (_cycle_start - _last_cycle_end);
   _cycle_gap_history->add(last_cycle_gap);
 }
 
 void ShenandoahAdaptiveHeuristics::record_phase_time(ShenandoahPhaseTimings::Phase phase, double secs) {
   if (phase == ShenandoahPhaseTimings::conc_mark) {

@@ -140,72 +107,62 @@
   } else if (phase == ShenandoahPhaseTimings::conc_update_refs) {
     _conc_uprefs_duration_history->add(secs);
   } // Else ignore
 }
 
-void ShenandoahAdaptiveHeuristics::adjust_free_threshold(intx adj) {
-  intx new_value = adj + _free_threshold;
-  uintx new_threshold = (uintx)MAX2<intx>(new_value, 0);
-  new_threshold = MAX2(new_threshold, ShenandoahMinFreeThreshold);
-  new_threshold = MIN2(new_threshold, ShenandoahMaxFreeThreshold);
-  if (new_threshold != _free_threshold) {
-    _free_threshold = new_threshold;
-    log_info(gc, ergo)("Adjusting free threshold to: " UINTX_FORMAT "%% (" SIZE_FORMAT "M)",
-                      _free_threshold, _free_threshold * ShenandoahHeap::heap()->capacity() / 100 / M);
+bool ShenandoahAdaptiveHeuristics::should_start_normal_gc() const {
+  ShenandoahHeap* heap = ShenandoahHeap::heap();
+  size_t capacity = heap->capacity();
+  size_t available = heap->free_set()->available();
+
+  // Check if we are falling below the worst limit, time to trigger the GC, regardless of
+  // anything else.
+  size_t min_threshold = ShenandoahMinFreeThreshold * heap->capacity() / 100;
+  if (available < min_threshold) {
+    log_info(gc)("Trigger: Free (" SIZE_FORMAT "M) is below minimum threshold (" SIZE_FORMAT "M)",
+                 available / M, min_threshold / M);
+    return true;
   }
-}
 
-void ShenandoahAdaptiveHeuristics::record_success_concurrent() {
-  ShenandoahHeuristics::record_success_concurrent();
-  handle_cycle_success();
-}
+  // Check if are need to learn a bit about the application
+  const size_t max_learn = ShenandoahLearningSteps;
+  if (_gc_times_learned < max_learn) {
+    size_t init_threshold = ShenandoahInitFreeThreshold * heap->capacity() / 100;
+    if (available < init_threshold) {
+      log_info(gc)("Trigger: Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "M) is below initial threshold (" SIZE_FORMAT "M)",
+                   _gc_times_learned + 1, max_learn, available / M, init_threshold / M);
+      return true;
+    }
+  }
 
-void ShenandoahAdaptiveHeuristics::record_success_degenerated() {
-  ShenandoahHeuristics::record_success_degenerated();
-  adjust_free_threshold(DegeneratedGC_Hit);
-}
+  // Check if allocation headroom is still okay. This also factors in:
+  //   1. Some space to absorb allocation spikes
+  //   2. Accumulated penalties from Degenerated and Full GC
 
-void ShenandoahAdaptiveHeuristics::record_success_full() {
-  ShenandoahHeuristics::record_success_full();
-  adjust_free_threshold(AllocFailure_Hit);
-}
+  size_t allocation_headroom = available;
 
-void ShenandoahAdaptiveHeuristics::record_explicit_gc() {
-  ShenandoahHeuristics::record_explicit_gc();
-  adjust_free_threshold(UserRequested_Hit);
-}
+  size_t spike_headroom = ShenandoahAllocSpikeFactor * capacity / 100;
+  size_t penalties      = _gc_time_penalties         * capacity / 100;
 
-void ShenandoahAdaptiveHeuristics::record_peak_occupancy() {
-  _peak_occupancy = MAX2(_peak_occupancy, ShenandoahHeap::heap()->used());
-}
+  allocation_headroom -= MIN2(allocation_headroom, spike_headroom);
+  allocation_headroom -= MIN2(allocation_headroom, penalties);
 
-bool ShenandoahAdaptiveHeuristics::should_start_normal_gc() const {
-  ShenandoahHeap* heap = ShenandoahHeap::heap();
-  size_t capacity = heap->capacity();
-  size_t available = heap->free_set()->available();
+  // TODO: Allocation rate is way too averaged to be useful during state changes
 
-  double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;
-  bool periodic_gc = (last_time_ms > ShenandoahGuaranteedGCInterval);
-  size_t threshold_available = (capacity * _free_threshold) / 100;
-  size_t bytes_allocated = heap->bytes_allocated_since_gc_start();
-  size_t threshold_bytes_allocated = heap->capacity() * ShenandoahAllocationThreshold / 100;
-
-  if (available < threshold_available &&
-      bytes_allocated > threshold_bytes_allocated) {
-    log_info(gc,ergo)("Concurrent marking triggered. Free: " SIZE_FORMAT "M, Free Threshold: " SIZE_FORMAT
-                              "M; Allocated: " SIZE_FORMAT "M, Alloc Threshold: " SIZE_FORMAT "M",
-                      available / M, threshold_available / M, bytes_allocated / M, threshold_bytes_allocated / M);
-    // Need to check that an appropriate number of regions have
-    // been allocated since last concurrent mark too.
-    return true;
-  } else if (periodic_gc) {
-    log_info(gc,ergo)("Periodic GC triggered. Time since last GC: %.0f ms, Guaranteed Interval: " UINTX_FORMAT " ms",
-                      last_time_ms, ShenandoahGuaranteedGCInterval);
+  double average_gc = _gc_time_history->avg();
+  double time_since_last = os::elapsedTime() - _cycle_start;
+  double allocation_rate = heap->bytes_allocated_since_gc_start() / time_since_last;
+
+  if (average_gc > allocation_headroom / allocation_rate) {
+    log_info(gc)("Trigger: Average GC time (%.2f ms) is above the time for allocation rate (%.2f MB/s) to deplete free headroom (" SIZE_FORMAT "M)",
+                 average_gc * 1000, allocation_rate / M, allocation_headroom / M);
+    log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "M (free) - " SIZE_FORMAT "M (spike) - " SIZE_FORMAT "M (penalties) = " SIZE_FORMAT "M",
+                       available / M, spike_headroom / M, penalties / M, allocation_headroom / M);
     return true;
   }
 
-  return false;
+  return ShenandoahHeuristics::should_start_normal_gc();
 }
 
 bool ShenandoahAdaptiveHeuristics::should_start_update_refs() {
   if (! _update_refs_adaptive) {
     return _update_refs_early;
< prev index next >