< prev index next >

src/hotspot/share/gc/shenandoah/heuristics/shenandoahAdaptiveHeuristics.cpp

Print this page

 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 
 27 #include "gc/shenandoah/heuristics/shenandoahAdaptiveHeuristics.hpp"
 28 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
 29 #include "gc/shenandoah/shenandoahFreeSet.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"

 32 #include "logging/log.hpp"
 33 #include "logging/logTag.hpp"
 34 #include "utilities/quickSort.hpp"
 35 
 36 // These constants are used to adjust the margin of error for the moving
 37 // average of the allocation rate and cycle time. The units are standard
 38 // deviations.
 39 const double ShenandoahAdaptiveHeuristics::FULL_PENALTY_SD = 0.2;
 40 const double ShenandoahAdaptiveHeuristics::DEGENERATE_PENALTY_SD = 0.1;
 41 
 42 // These are used to decide if we want to make any adjustments at all
 43 // at the end of a successful concurrent cycle.
 44 const double ShenandoahAdaptiveHeuristics::LOWEST_EXPECTED_AVAILABLE_AT_END = -0.5;
 45 const double ShenandoahAdaptiveHeuristics::HIGHEST_EXPECTED_AVAILABLE_AT_END = 0.5;
 46 
 47 // These values are the confidence interval expressed as standard deviations.
 48 // At the minimum confidence level, there is a 25% chance that the true value of
 49 // the estimate (average cycle time or allocation rate) is not more than
 50 // MINIMUM_CONFIDENCE standard deviations away from our estimate. Similarly, the
 51 // MAXIMUM_CONFIDENCE interval here means there is a one in a thousand chance
 52 // that the true value of our estimate is outside the interval. These are used
 53 // as bounds on the adjustments applied at the outcome of a GC cycle.
 54 const double ShenandoahAdaptiveHeuristics::MINIMUM_CONFIDENCE = 0.319; // 25%
 55 const double ShenandoahAdaptiveHeuristics::MAXIMUM_CONFIDENCE = 3.291; // 99.9%
 56 
 57 ShenandoahAdaptiveHeuristics::ShenandoahAdaptiveHeuristics() :
 58   ShenandoahHeuristics(),
 59   _margin_of_error_sd(ShenandoahAdaptiveInitialConfidence),
 60   _spike_threshold_sd(ShenandoahAdaptiveInitialSpikeThreshold),
 61   _last_trigger(OTHER) { }
 62 
 63 ShenandoahAdaptiveHeuristics::~ShenandoahAdaptiveHeuristics() {}
 64 
 65 void ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
 66                                                                          RegionData* data, size_t size,
 67                                                                          size_t actual_free) {
 68   size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;


 69 
 70   // The logic for cset selection in adaptive is as follows:
 71   //
 72   //   1. We cannot get cset larger than available free space. Otherwise we guarantee OOME
 73   //      during evacuation, and thus guarantee full GC. In practice, we also want to let
 74   //      application to allocate something. This is why we limit CSet to some fraction of
 75   //      available space. In non-overloaded heap, max_cset would contain all plausible candidates
 76   //      over garbage threshold.
 77   //
 78   //   2. We should not get cset too low so that free threshold would not be met right
 79   //      after the cycle. Otherwise we get back-to-back cycles for no reason if heap is
 80   //      too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero.
 81   //
 82   // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates
 83   // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before
 84   // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme,
 85   // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit.
 86 
 87   size_t capacity    = ShenandoahHeap::heap()->soft_max_capacity();
 88   size_t max_cset    = (size_t)((1.0 * capacity / 100 * ShenandoahEvacReserve) / ShenandoahEvacWaste);
 89   size_t free_target = (capacity / 100 * ShenandoahMinFreeThreshold) + max_cset;
 90   size_t min_garbage = (free_target > actual_free ? (free_target - actual_free) : 0);



 91 
 92   log_info(gc, ergo)("Adaptive CSet Selection. Target Free: " SIZE_FORMAT "%s, Actual Free: "
 93                      SIZE_FORMAT "%s, Max CSet: " SIZE_FORMAT "%s, Min Garbage: " SIZE_FORMAT "%s",
 94                      byte_size_in_proper_unit(free_target), proper_unit_for_byte_size(free_target),
 95                      byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free),
 96                      byte_size_in_proper_unit(max_cset),    proper_unit_for_byte_size(max_cset),
 97                      byte_size_in_proper_unit(min_garbage), proper_unit_for_byte_size(min_garbage));
 98 
 99   // Better select garbage-first regions
100   QuickSort::sort<RegionData>(data, (int)size, compare_by_garbage, false);
101 
102   size_t cur_cset = 0;
103   size_t cur_garbage = 0;
104 
105   for (size_t idx = 0; idx < size; idx++) {
106     ShenandoahHeapRegion* r = data[idx]._region;
107 
108     size_t new_cset    = cur_cset + r->get_live_data_bytes();
109     size_t new_garbage = cur_garbage + r->garbage();
110 
111     if (new_cset > max_cset) {
112       break;




























































































113     }
114 
115     if ((new_garbage < min_garbage) || (r->garbage() > garbage_threshold)) {
116       cset->add_region(r);
117       cur_cset = new_cset;
118       cur_garbage = new_garbage;

















119     }
120   }
121 }
122 
123 void ShenandoahAdaptiveHeuristics::record_cycle_start() {
124   ShenandoahHeuristics::record_cycle_start();
125   _allocation_rate.allocation_counter_reset();
126 }
127 
128 void ShenandoahAdaptiveHeuristics::record_success_concurrent() {
129   ShenandoahHeuristics::record_success_concurrent();
130 
131   size_t available = ShenandoahHeap::heap()->free_set()->available();
132 
133   _available.add(available);
134   double z_score = 0.0;
135   if (_available.sd() > 0) {
136     z_score = (available - _available.avg()) / _available.sd();
137   }
138 
139   log_debug(gc, ergo)("Available: " SIZE_FORMAT " %sB, z-score=%.3f. Average available: %.1f %sB +/- %.1f %sB.",
140                       byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
141                       z_score,
142                       byte_size_in_proper_unit(_available.avg()), proper_unit_for_byte_size(_available.avg()),
143                       byte_size_in_proper_unit(_available.sd()), proper_unit_for_byte_size(_available.sd()));
144 
145   // In the case when a concurrent GC cycle completes successfully but with an
146   // unusually small amount of available memory we will adjust our trigger
147   // parameters so that they are more likely to initiate a new cycle.
148   // Conversely, when a GC cycle results in an above average amount of available
149   // memory, we will adjust the trigger parameters to be less likely to initiate

179   ShenandoahHeuristics::record_success_degenerated();
180   // Adjust both trigger's parameters in the case of a degenerated GC because
181   // either of them should have triggered earlier to avoid this case.
182   adjust_margin_of_error(DEGENERATE_PENALTY_SD);
183   adjust_spike_threshold(DEGENERATE_PENALTY_SD);
184 }
185 
186 void ShenandoahAdaptiveHeuristics::record_success_full() {
187   ShenandoahHeuristics::record_success_full();
188   // Adjust both trigger's parameters in the case of a full GC because
189   // either of them should have triggered earlier to avoid this case.
190   adjust_margin_of_error(FULL_PENALTY_SD);
191   adjust_spike_threshold(FULL_PENALTY_SD);
192 }
193 
194 static double saturate(double value, double min, double max) {
195   return MAX2(MIN2(value, max), min);
196 }
197 
198 bool ShenandoahAdaptiveHeuristics::should_start_gc() {
199   ShenandoahHeap* heap = ShenandoahHeap::heap();
200   size_t max_capacity = heap->max_capacity();
201   size_t capacity = heap->soft_max_capacity();
202   size_t available = heap->free_set()->available();
203   size_t allocated = heap->bytes_allocated_since_gc_start();



204 
205   // Make sure the code below treats available without the soft tail.
206   size_t soft_tail = max_capacity - capacity;
207   available = (available > soft_tail) ? (available - soft_tail) : 0;
208 
































209   // Track allocation rate even if we decide to start a cycle for other reasons.
210   double rate = _allocation_rate.sample(allocated);
211   _last_trigger = OTHER;
212 
213   size_t min_threshold = capacity / 100 * ShenandoahMinFreeThreshold;
214   if (available < min_threshold) {
215     log_info(gc)("Trigger: Free (" SIZE_FORMAT "%s) is below minimum threshold (" SIZE_FORMAT "%s)",


216                  byte_size_in_proper_unit(available),     proper_unit_for_byte_size(available),
217                  byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold));
218     return true;
219   }
220 

221   const size_t max_learn = ShenandoahLearningSteps;
222   if (_gc_times_learned < max_learn) {
223     size_t init_threshold = capacity / 100 * ShenandoahInitFreeThreshold;
224     if (available < init_threshold) {
225       log_info(gc)("Trigger: Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "%s) is below initial threshold (" SIZE_FORMAT "%s)",
226                    _gc_times_learned + 1, max_learn,
227                    byte_size_in_proper_unit(available),      proper_unit_for_byte_size(available),
228                    byte_size_in_proper_unit(init_threshold), proper_unit_for_byte_size(init_threshold));
229       return true;
230     }
231   }
232 
233   // Check if allocation headroom is still okay. This also factors in:
234   //   1. Some space to absorb allocation spikes
235   //   2. Accumulated penalties from Degenerated and Full GC
236   size_t allocation_headroom = available;
























237 
238   size_t spike_headroom = capacity / 100 * ShenandoahAllocSpikeFactor;
239   size_t penalties      = capacity / 100 * _gc_time_penalties;
240 
241   allocation_headroom -= MIN2(allocation_headroom, spike_headroom);
242   allocation_headroom -= MIN2(allocation_headroom, penalties);






243 
244   double avg_cycle_time = _gc_time_history->davg() + (_margin_of_error_sd * _gc_time_history->dsd());



















245   double avg_alloc_rate = _allocation_rate.upper_bound(_margin_of_error_sd);



246   if (avg_cycle_time > allocation_headroom / avg_alloc_rate) {
247     log_info(gc)("Trigger: Average GC time (%.2f ms) is above the time for average allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s) (margin of error = %.2f)",
248                  avg_cycle_time * 1000,





249                  byte_size_in_proper_unit(avg_alloc_rate), proper_unit_for_byte_size(avg_alloc_rate),
250                  byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom),
251                  _margin_of_error_sd);
252 
253     log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "%s (free) - " SIZE_FORMAT "%s (spike) - " SIZE_FORMAT "%s (penalties) = " SIZE_FORMAT "%s",
254                        byte_size_in_proper_unit(available),           proper_unit_for_byte_size(available),
255                        byte_size_in_proper_unit(spike_headroom),      proper_unit_for_byte_size(spike_headroom),
256                        byte_size_in_proper_unit(penalties),           proper_unit_for_byte_size(penalties),
257                        byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom));
258 
259     _last_trigger = RATE;
260     return true;
261   }
262 
263   bool is_spiking = _allocation_rate.is_spiking(rate, _spike_threshold_sd);
264   if (is_spiking && avg_cycle_time > allocation_headroom / rate) {
265     log_info(gc)("Trigger: Average GC time (%.2f ms) is above the time for instantaneous allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s) (spike threshold = %.2f)",
266                  avg_cycle_time * 1000,
267                  byte_size_in_proper_unit(rate), proper_unit_for_byte_size(rate),
268                  byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom),

269                  _spike_threshold_sd);
270     _last_trigger = SPIKE;
271     return true;
272   }
273 
274   return ShenandoahHeuristics::should_start_gc();
275 }
276 
277 void ShenandoahAdaptiveHeuristics::adjust_last_trigger_parameters(double amount) {
278   switch (_last_trigger) {
279     case RATE:
280       adjust_margin_of_error(amount);
281       break;
282     case SPIKE:
283       adjust_spike_threshold(amount);
284       break;
285     case OTHER:
286       // nothing to adjust here.
287       break;
288     default:

 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 
 27 #include "gc/shenandoah/heuristics/shenandoahAdaptiveHeuristics.hpp"
 28 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
 29 #include "gc/shenandoah/shenandoahFreeSet.hpp"
 30 #include "gc/shenandoah/shenandoahGeneration.hpp"
 31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 32 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
 33 #include "logging/log.hpp"
 34 #include "logging/logTag.hpp"
 35 #include "utilities/quickSort.hpp"
 36 
 37 // These constants are used to adjust the margin of error for the moving
 38 // average of the allocation rate and cycle time. The units are standard
 39 // deviations.
 40 const double ShenandoahAdaptiveHeuristics::FULL_PENALTY_SD = 0.2;
 41 const double ShenandoahAdaptiveHeuristics::DEGENERATE_PENALTY_SD = 0.1;
 42 
 43 // These are used to decide if we want to make any adjustments at all
 44 // at the end of a successful concurrent cycle.
 45 const double ShenandoahAdaptiveHeuristics::LOWEST_EXPECTED_AVAILABLE_AT_END = -0.5;
 46 const double ShenandoahAdaptiveHeuristics::HIGHEST_EXPECTED_AVAILABLE_AT_END = 0.5;
 47 
 48 // These values are the confidence interval expressed as standard deviations.
 49 // At the minimum confidence level, there is a 25% chance that the true value of
 50 // the estimate (average cycle time or allocation rate) is not more than
 51 // MINIMUM_CONFIDENCE standard deviations away from our estimate. Similarly, the
 52 // MAXIMUM_CONFIDENCE interval here means there is a one in a thousand chance
 53 // that the true value of our estimate is outside the interval. These are used
 54 // as bounds on the adjustments applied at the outcome of a GC cycle.
 55 const double ShenandoahAdaptiveHeuristics::MINIMUM_CONFIDENCE = 0.319; // 25%
 56 const double ShenandoahAdaptiveHeuristics::MAXIMUM_CONFIDENCE = 3.291; // 99.9%
 57 
 58 ShenandoahAdaptiveHeuristics::ShenandoahAdaptiveHeuristics(ShenandoahGeneration* generation) :
 59   ShenandoahHeuristics(generation),
 60   _margin_of_error_sd(ShenandoahAdaptiveInitialConfidence),
 61   _spike_threshold_sd(ShenandoahAdaptiveInitialSpikeThreshold),
 62   _last_trigger(OTHER) { }
 63 
 64 ShenandoahAdaptiveHeuristics::~ShenandoahAdaptiveHeuristics() {}
 65 
 66 void ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
 67                                                                          RegionData* data, size_t size,
 68                                                                          size_t actual_free) {
 69   size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;
 70   size_t ignore_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahIgnoreGarbageThreshold / 100;
 71   ShenandoahHeap* heap = ShenandoahHeap::heap();
 72 
 73   // The logic for cset selection in adaptive is as follows:
 74   //
 75   //   1. We cannot get cset larger than available free space. Otherwise we guarantee OOME
 76   //      during evacuation, and thus guarantee full GC. In practice, we also want to let
 77   //      application to allocate something. This is why we limit CSet to some fraction of
 78   //      available space. In non-overloaded heap, max_cset would contain all plausible candidates
 79   //      over garbage threshold.
 80   //
 81   //   2. We should not get cset too low so that free threshold would not be met right
 82   //      after the cycle. Otherwise we get back-to-back cycles for no reason if heap is
 83   //      too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero.
 84   //
 85   // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates
 86   // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before
 87   // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme,
 88   // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit.
 89 
 90   // In generational mode, the sort order within the data array is not strictly descending amounts of garbage.  In
 91   // particular, regions that have reached tenure age will be sorted into this array before younger regions that contain
 92   // more garbage.  This represents one of the reasons why we keep looking at regions even after we decide, for example,
 93   // to exclude one of the regions because it might require evacuation of too much live data.
 94   bool is_generational = heap->mode()->is_generational();
 95   bool is_global = (_generation->generation_mode() == GLOBAL);
 96   size_t capacity = heap->young_generation()->max_capacity();
 97 
 98   // cur_young_garbage represents the amount of memory to be reclaimed from young-gen.  In the case that live objects
 99   // are known to be promoted out of young-gen, we count this as cur_young_garbage because this memory is reclaimed
100   // from young-gen and becomes available to serve future young-gen allocation requests.
101   size_t cur_young_garbage = 0;


102 
103   // Better select garbage-first regions
104   QuickSort::sort<RegionData>(data, (int)size, compare_by_garbage, false);
105 
106   if (is_generational) {
107     if (is_global) {
108       size_t max_young_cset    = (size_t) (heap->get_young_evac_reserve() / ShenandoahEvacWaste);
109       size_t young_cur_cset = 0;
110       size_t max_old_cset    = (size_t) (heap->get_old_evac_reserve() / ShenandoahEvacWaste);
111       size_t old_cur_cset = 0;
112       size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_young_cset;
113       size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0;
114 
115       log_info(gc, ergo)("Adaptive CSet Selection for GLOBAL. Max Young Cset: " SIZE_FORMAT
116                          "%s, Max Old CSet: " SIZE_FORMAT "%s, Actual Free: " SIZE_FORMAT "%s.",
117                          byte_size_in_proper_unit(max_young_cset),    proper_unit_for_byte_size(max_young_cset),
118                          byte_size_in_proper_unit(max_old_cset),    proper_unit_for_byte_size(max_old_cset),
119                          byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free));
120 
121       for (size_t idx = 0; idx < size; idx++) {
122         ShenandoahHeapRegion* r = data[idx]._region;
123         bool add_region = false;
124         if (r->is_old()) {
125           size_t new_cset = old_cur_cset + r->get_live_data_bytes();
126           if ((new_cset <= max_old_cset) && (r->garbage() > garbage_threshold)) {
127             add_region = true;
128             old_cur_cset = new_cset;
129           }
130         } else if (cset->is_preselected(r->index())) {
131           assert(r->age() >= InitialTenuringThreshold, "Preselected regions must have tenure age");
132           // Entire region will be promoted, This region does not impact young-gen or old-gen evacuation reserve.
133           // This region has been pre-selected and its impact on promotion reserve is already accounted for.
134           add_region = true;
135           // r->used() is r->garbage() + r->get_live_data_bytes()
136           // Since all live data in this region is being evacuated from young-gen, it is as if this memory
137           // is garbage insofar as young-gen is concerned.  Counting this as garbage reduces the need to
138           // reclaim highly utilized young-gen regions just for the sake of finding min_garbage to reclaim
139           // within youn-gen memory.
140           cur_young_garbage += r->used();
141         } else if (r->age() < InitialTenuringThreshold) {
142           size_t new_cset = young_cur_cset + r->get_live_data_bytes();
143           size_t region_garbage = r->garbage();
144           size_t new_garbage = cur_young_garbage + region_garbage;
145           bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage);
146           if ((new_cset <= max_young_cset) && (add_regardless || (region_garbage > garbage_threshold))) {
147             add_region = true;
148             young_cur_cset = new_cset;
149             cur_young_garbage = new_garbage;
150           }
151         }
152         // Note that we do not add aged regions if they were not pre-selected.  The reason they were not preselected
153         // is because there is not sufficient room in old-gen to hold their to-be-promoted live objects.
154 
155         if (add_region) {
156           cset->add_region(r);
157         }
158       }
159     } else {
160       // This is young-gen collection or a mixed evacuation.  If this is mixed evacuation, the old-gen candidate regions
161       // have already been added.
162       size_t max_cset    = (size_t) (heap->get_young_evac_reserve() / ShenandoahEvacWaste);
163       size_t cur_cset = 0;
164       size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_cset;
165       size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0;
166 
167       log_info(gc, ergo)("Adaptive CSet Selection for YOUNG. Max CSet: " SIZE_FORMAT "%s, Actual Free: " SIZE_FORMAT "%s.",
168                          byte_size_in_proper_unit(max_cset),    proper_unit_for_byte_size(max_cset),
169                          byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free));
170 
171       for (size_t idx = 0; idx < size; idx++) {
172         ShenandoahHeapRegion* r = data[idx]._region;
173         size_t new_cset;
174         size_t region_garbage = r->garbage();
175         size_t new_garbage = cur_young_garbage + region_garbage;
176         bool add_region = false;
177 
178         if (!r->is_old()) {
179           if (cset->is_preselected(r->index())) {
180             assert(r->age() >= InitialTenuringThreshold, "Preselected regions must have tenure age");
181             // Entire region will be promoted, This region does not impact young-gen evacuation reserve.  Memory has already
182             // been set aside to hold evacuation results as advance_promotion_reserve.
183             add_region = true;
184             new_cset = cur_cset;
185             // Since all live data in this region is being evacuated from young-gen, it is as if this memory
186             // is garbage insofar as young-gen is concerned.  Counting this as garbage reduces the need to
187             // reclaim highly utilized young-gen regions just for the sake of finding min_garbage to reclaim
188             // within youn-gen memory
189             cur_young_garbage += r->get_live_data_bytes();
190           } else if  (r->age() < InitialTenuringThreshold) {
191             new_cset = cur_cset + r->get_live_data_bytes();
192             size_t region_garbage = r->garbage();
193             size_t new_garbage = cur_young_garbage + region_garbage;
194             bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage);
195             if ((new_cset <= max_cset) && (add_regardless || (region_garbage > garbage_threshold))) {
196               add_region = true;
197               cur_cset = new_cset;
198               cur_young_garbage = new_garbage;
199             }
200           }
201           // Note that we do not add aged regions if they were not pre-selected.  The reason they were not preselected
202           // is because there is not sufficient room in old-gen to hold their to-be-promoted live objects.
203 
204           if (add_region) {
205             cset->add_region(r);
206           }
207         }
208       }
209     }
210   } else {
211     // Traditional Shenandoah (non-generational)
212     size_t max_cset    = (size_t) (heap->get_young_evac_reserve() / ShenandoahEvacWaste);
213     size_t cur_cset = 0;
214     size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_cset;
215     size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0;
216 
217     log_info(gc, ergo)("Adaptive CSet Selection. Max CSet: " SIZE_FORMAT "%s, Actual Free: " SIZE_FORMAT "%s.",
218                          byte_size_in_proper_unit(max_cset),    proper_unit_for_byte_size(max_cset),
219                          byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free));
220 
221     for (size_t idx = 0; idx < size; idx++) {
222       ShenandoahHeapRegion* r = data[idx]._region;
223       size_t new_cset = cur_cset + r->get_live_data_bytes();
224       size_t region_garbage = r->garbage();
225       size_t new_garbage = cur_young_garbage + region_garbage;
226       bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage);
227       if ((new_cset <= max_cset) && (add_regardless || (region_garbage > garbage_threshold))) {
228         cset->add_region(r);
229         cur_cset = new_cset;
230         cur_young_garbage = new_garbage;
231       }
232     }
233   }
234 }
235 
236 void ShenandoahAdaptiveHeuristics::record_cycle_start() {
237   ShenandoahHeuristics::record_cycle_start();
238   _allocation_rate.allocation_counter_reset();
239 }
240 
241 void ShenandoahAdaptiveHeuristics::record_success_concurrent(bool abbreviated) {
242   ShenandoahHeuristics::record_success_concurrent(abbreviated);
243 
244   size_t available = ShenandoahHeap::heap()->free_set()->available();
245 
246   _available.add(available);
247   double z_score = 0.0;
248   if (_available.sd() > 0) {
249     z_score = (available - _available.avg()) / _available.sd();
250   }
251 
252   log_debug(gc, ergo)("Available: " SIZE_FORMAT " %sB, z-score=%.3f. Average available: %.1f %sB +/- %.1f %sB.",
253                       byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
254                       z_score,
255                       byte_size_in_proper_unit(_available.avg()), proper_unit_for_byte_size(_available.avg()),
256                       byte_size_in_proper_unit(_available.sd()), proper_unit_for_byte_size(_available.sd()));
257 
258   // In the case when a concurrent GC cycle completes successfully but with an
259   // unusually small amount of available memory we will adjust our trigger
260   // parameters so that they are more likely to initiate a new cycle.
261   // Conversely, when a GC cycle results in an above average amount of available
262   // memory, we will adjust the trigger parameters to be less likely to initiate

292   ShenandoahHeuristics::record_success_degenerated();
293   // Adjust both trigger's parameters in the case of a degenerated GC because
294   // either of them should have triggered earlier to avoid this case.
295   adjust_margin_of_error(DEGENERATE_PENALTY_SD);
296   adjust_spike_threshold(DEGENERATE_PENALTY_SD);
297 }
298 
299 void ShenandoahAdaptiveHeuristics::record_success_full() {
300   ShenandoahHeuristics::record_success_full();
301   // Adjust both trigger's parameters in the case of a full GC because
302   // either of them should have triggered earlier to avoid this case.
303   adjust_margin_of_error(FULL_PENALTY_SD);
304   adjust_spike_threshold(FULL_PENALTY_SD);
305 }
306 
307 static double saturate(double value, double min, double max) {
308   return MAX2(MIN2(value, max), min);
309 }
310 
311 bool ShenandoahAdaptiveHeuristics::should_start_gc() {
312   size_t max_capacity = _generation->max_capacity();
313   size_t capacity = _generation->soft_max_capacity();
314   size_t available = _generation->available();
315   size_t allocated = _generation->bytes_allocated_since_gc_start();
316 
317   log_debug(gc)("should_start_gc (%s)? available: " SIZE_FORMAT ", soft_max_capacity: " SIZE_FORMAT
318                 ", max_capacity: " SIZE_FORMAT ", allocated: " SIZE_FORMAT,
319                 _generation->name(), available, capacity, max_capacity, allocated);
320 
321   // Make sure the code below treats available without the soft tail.
322   size_t soft_tail = max_capacity - capacity;
323   available = (available > soft_tail) ? (available - soft_tail) : 0;
324 
325   // The collector reserve may eat into what the mutator is allowed to use. Make sure we are looking
326   // at what is available to the mutator when deciding whether to start a GC.
327   size_t usable = ShenandoahHeap::heap()->free_set()->available();
328   if (usable < available) {
329     log_debug(gc)("Usable (" SIZE_FORMAT "%s) is less than available (" SIZE_FORMAT "%s)",
330                   byte_size_in_proper_unit(usable), proper_unit_for_byte_size(usable),
331                   byte_size_in_proper_unit(available), proper_unit_for_byte_size(available));
332     available = usable;
333   }
334 
335   // Allocation spikes are a characteristic of both the application ahd the JVM configuration.  On the JVM command line,
336   // the application developer may want to supply a hint of the nature of spikes that are inherent in the application
337   // workload, and this information would normally be independent of heap size (not a percentage thereof).  On the
338   // other hand, some allocation spikes are correlated with JVM configuration.  For example, there are allocation
339   // spikes at the starts of concurrent marking and evacuation to refresh all local allocation buffers.  The nature
340   // of these spikes is determined by LAB min and max sizes and numbers of threads, but also on frequency of GC passes,
341   // and on "periodic" behavior of these threads  If GC frequency is much higher than the periodic trigger for mutator
342   // threads, then many of the mutator threads may be able to "sit out" of most GC passes.  Though the thread's stack
343   // must be scanned, the thread does not need to refresh its LABs if it sits idle throughout the duration of the GC
344   // pass.  The best prediction for this aspect of spikes in allocation patterns is probably recent past history.
345   // TODO: and dive deeper into _gc_time_penalties as this may also need to be corrected
346 
347   // Check if allocation headroom is still okay. This also factors in:
348   //   1. Some space to absorb allocation spikes (ShenandoahAllocSpikeFactor)
349   //   2. Accumulated penalties from Degenerated and Full GC
350   size_t allocation_headroom = available;
351   size_t spike_headroom = capacity / 100 * ShenandoahAllocSpikeFactor;
352   size_t penalties      = capacity / 100 * _gc_time_penalties;
353 
354   allocation_headroom -= MIN2(allocation_headroom, penalties);
355   allocation_headroom -= MIN2(allocation_headroom, spike_headroom);
356 
357   // Track allocation rate even if we decide to start a cycle for other reasons.
358   double rate = _allocation_rate.sample(allocated);
359   _last_trigger = OTHER;
360 
361   size_t min_threshold = min_free_threshold();
362 
363   if (allocation_headroom < min_threshold) {
364     log_info(gc)("Trigger (%s): Free (" SIZE_FORMAT "%s) is below minimum threshold (" SIZE_FORMAT "%s)",
365                  _generation->name(),
366                  byte_size_in_proper_unit(available),     proper_unit_for_byte_size(available),
367                  byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold));
368     return true;
369   }
370 
371   // Check if we need to learn a bit about the application
372   const size_t max_learn = ShenandoahLearningSteps;
373   if (_gc_times_learned < max_learn) {
374     size_t init_threshold = capacity / 100 * ShenandoahInitFreeThreshold;
375     if (allocation_headroom < init_threshold) {
376       log_info(gc)("Trigger (%s): Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "%s) is below initial threshold (" SIZE_FORMAT "%s)",
377                    _generation->name(), _gc_times_learned + 1, max_learn,
378                    byte_size_in_proper_unit(available),      proper_unit_for_byte_size(available),
379                    byte_size_in_proper_unit(init_threshold), proper_unit_for_byte_size(init_threshold));
380       return true;
381     }
382   }
383 
384   //  Rationale:
385   //    The idea is that there is an average allocation rate and there are occasional abnormal bursts (or spikes) of
386   //    allocations that exceed the average allocation rate.  What do these spikes look like?
387   //
388   //    1. At certain phase changes, we may discard large amounts of data and replace it with large numbers of newly
389   //       allocated objects.  This "spike" looks more like a phase change.  We were in steady state at M bytes/sec
390   //       allocation rate and now we're in a "reinitialization phase" that looks like N bytes/sec.  We need the "spike"
391   //       accomodation to give us enough runway to recalibrate our "average allocation rate".
392   //
393   //   2. The typical workload changes.  "Suddenly", our typical workload of N TPS increases to N+delta TPS.  This means
394   //       our average allocation rate needs to be adjusted.  Once again, we need the "spike" accomodation to give us
395   //       enough runway to recalibrate our "average allocation rate".
396   //
397   //    3. Though there is an "average" allocation rate, a given workload's demand for allocation may be very bursty.  We
398   //       allocate a bunch of LABs during the 5 ms that follow completion of a GC, then we perform no more allocations for
399   //       the next 150 ms.  It seems we want the "spike" to represent the maximum divergence from average within the
400   //       period of time between consecutive evaluation of the should_start_gc() service.  Here's the thinking:
401   //
402   //       a) Between now and the next time I ask whether should_start_gc(), we might experience a spike representing
403   //          the anticipated burst of allocations.  If that would put us over budget, then we should start GC immediately.
404   //       b) Between now and the anticipated depletion of allocation pool, there may be two or more bursts of allocations.
405   //          If there are more than one of these bursts, we can "approximate" that these will be separated by spans of
406   //          time with very little or no allocations so the "average" allocation rate should be a suitable approximation
407   //          of how this will behave.
408   //
409   //    For cases 1 and 2, we need to "quickly" recalibrate the average allocation rate whenever we detect a change
410   //    in operation mode.  We want some way to decide that the average rate has changed.  Make average allocation rate
411   //    computations an independent effort.
412 


413 
414   // TODO: Account for inherent delays in responding to GC triggers
415   //  1. It has been observed that delays of 200 ms or greater are common between the moment we return true from should_start_gc()
416   //     and the moment at which we begin execution of the concurrent reset phase.  Add this time into the calculation of
417   //     avg_cycle_time below.  (What is "this time"?  Perhaps we should remember recent history of this delay for the
418   //     running workload and use the maximum delay recently seen for "this time".)
419   //  2. The frequency of inquiries to should_start_gc() is adaptive, ranging between ShenandoahControlIntervalMin and
420   //     ShenandoahControlIntervalMax.  The current control interval (or the max control interval) should also be added into
421   //     the calculation of avg_cycle_time below.
422 
423   double avg_cycle_time = _gc_time_history->davg() + (_margin_of_error_sd * _gc_time_history->dsd());
424 
425   size_t last_live_memory = get_last_live_memory();
426   size_t penultimate_live_memory = get_penultimate_live_memory();
427   double original_cycle_time = avg_cycle_time;
428   if ((penultimate_live_memory < last_live_memory) && (penultimate_live_memory != 0)) {
429     // If the live-memory size is growing, our estimates of cycle time are based on lighter workload, so adjust.
430     // TODO: Be more precise about how to scale when live memory is growing.  Existing code is a very rough approximation
431     // tuned with very limited workload observations.
432     avg_cycle_time = (avg_cycle_time * 2 * last_live_memory) / penultimate_live_memory;
433   } else {
434     int degen_cycles = degenerated_cycles_in_a_row();
435     if (degen_cycles > 0) {
436       // If we've degenerated recently, we might be waiting too long between triggers so adjust trigger forward.
437       // TODO: Be more precise about how to scale when we've experienced recent degenerated GC.  Existing code is a very
438       // rough approximation tuned with very limited workload observations.
439       avg_cycle_time += degen_cycles * avg_cycle_time;
440     }
441   }
442 
443   double avg_alloc_rate = _allocation_rate.upper_bound(_margin_of_error_sd);
444   log_debug(gc)("%s: average GC time: %.2f ms, allocation rate: %.0f %s/s",
445     _generation->name(), avg_cycle_time * 1000, byte_size_in_proper_unit(avg_alloc_rate), proper_unit_for_byte_size(avg_alloc_rate));
446 
447   if (avg_cycle_time > allocation_headroom / avg_alloc_rate) {
448     if (avg_cycle_time > original_cycle_time) {
449       log_debug(gc)("%s: average GC time adjusted from: %.2f ms to %.2f ms because upward trend in live memory retention",
450                     _generation->name(), original_cycle_time, avg_cycle_time);
451     }
452 
453     log_info(gc)("Trigger (%s): Average GC time (%.2f ms) is above the time for average allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s) (margin of error = %.2f)",
454                  _generation->name(), avg_cycle_time * 1000,
455                  byte_size_in_proper_unit(avg_alloc_rate), proper_unit_for_byte_size(avg_alloc_rate),
456                  byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom),
457                  _margin_of_error_sd);
458 
459     log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "%s (free) - " SIZE_FORMAT "%s (spike) - " SIZE_FORMAT "%s (penalties) = " SIZE_FORMAT "%s",
460                        byte_size_in_proper_unit(available),           proper_unit_for_byte_size(available),
461                        byte_size_in_proper_unit(spike_headroom),      proper_unit_for_byte_size(spike_headroom),
462                        byte_size_in_proper_unit(penalties),           proper_unit_for_byte_size(penalties),
463                        byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom));
464 
465     _last_trigger = RATE;
466     return true;
467   }
468 
469   bool is_spiking = _allocation_rate.is_spiking(rate, _spike_threshold_sd);
470   if (is_spiking && avg_cycle_time > allocation_headroom / rate) {
471     log_info(gc)("Trigger (%s): Average GC time (%.2f ms) is above the time for instantaneous allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s) (spike threshold = %.2f)",
472                  _generation->name(), avg_cycle_time * 1000,
473                  byte_size_in_proper_unit(rate), proper_unit_for_byte_size(rate),
474                  byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom),
475 
476                  _spike_threshold_sd);
477     _last_trigger = SPIKE;
478     return true;
479   }
480 
481   return ShenandoahHeuristics::should_start_gc();
482 }
483 
484 void ShenandoahAdaptiveHeuristics::adjust_last_trigger_parameters(double amount) {
485   switch (_last_trigger) {
486     case RATE:
487       adjust_margin_of_error(amount);
488       break;
489     case SPIKE:
490       adjust_spike_threshold(amount);
491       break;
492     case OTHER:
493       // nothing to adjust here.
494       break;
495     default:
< prev index next >