1 /*
  2  * Copyright Amazon.com Inc. 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 
 27 #include "gc/shenandoah/heuristics/shenandoahOldHeuristics.hpp"
 28 #include "gc/shenandoah/heuristics/shenandoahYoungHeuristics.hpp"
 29 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
 30 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
 31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 32 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
 33 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
 34 
 35 #include "utilities/quickSort.hpp"
 36 
 37 ShenandoahYoungHeuristics::ShenandoahYoungHeuristics(ShenandoahYoungGeneration* generation)
 38         : ShenandoahGenerationalHeuristics(generation) {
 39 }
 40 
 41 
 42 void ShenandoahYoungHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
 43                                                                       RegionData* data, size_t size,
 44                                                                       size_t actual_free) {
 45   // The logic for cset selection in adaptive is as follows:
 46   //
 47   //   1. We cannot get cset larger than available free space. Otherwise we guarantee OOME
 48   //      during evacuation, and thus guarantee full GC. In practice, we also want to let
 49   //      application to allocate something. This is why we limit CSet to some fraction of
 50   //      available space. In non-overloaded heap, max_cset would contain all plausible candidates
 51   //      over garbage threshold.
 52   //
 53   //   2. We should not get cset too low so that free threshold would not be met right
 54   //      after the cycle. Otherwise we get back-to-back cycles for no reason if heap is
 55   //      too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero.
 56   //
 57   // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates
 58   // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before
 59   // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme,
 60   // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit.
 61 
 62   // In generational mode, the sort order within the data array is not strictly descending amounts of garbage.  In
 63   // particular, regions that have reached tenure age will be sorted into this array before younger regions that contain
 64   // more garbage.  This represents one of the reasons why we keep looking at regions even after we decide, for example,
 65   // to exclude one of the regions because it might require evacuation of too much live data.
 66 
 67   // Better select garbage-first regions
 68   QuickSort::sort<RegionData>(data, (int) size, compare_by_garbage, false);
 69 
 70   size_t cur_young_garbage = add_preselected_regions_to_collection_set(cset, data, size);
 71 
 72   choose_young_collection_set(cset, data, size, actual_free, cur_young_garbage);
 73 
 74   log_cset_composition(cset);
 75 }
 76 
 77 void ShenandoahYoungHeuristics::choose_young_collection_set(ShenandoahCollectionSet* cset,
 78                                                             const RegionData* data,
 79                                                             size_t size, size_t actual_free,
 80                                                             size_t cur_young_garbage) const {
 81 
 82   auto heap = ShenandoahGenerationalHeap::heap();
 83 
 84   size_t capacity = heap->young_generation()->max_capacity();
 85   size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;
 86   size_t ignore_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahIgnoreGarbageThreshold / 100;
 87   const uint tenuring_threshold = heap->age_census()->tenuring_threshold();
 88 
 89   // This is young-gen collection or a mixed evacuation.
 90   // If this is mixed evacuation, the old-gen candidate regions have already been added.
 91   size_t max_cset = (size_t) (heap->young_generation()->get_evacuation_reserve() / ShenandoahEvacWaste);
 92   size_t cur_cset = 0;
 93   size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_cset;
 94   size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0;
 95 
 96 
 97   log_info(gc, ergo)(
 98           "Adaptive CSet Selection for YOUNG. Max Evacuation: " SIZE_FORMAT "%s, Actual Free: " SIZE_FORMAT "%s.",
 99           byte_size_in_proper_unit(max_cset), proper_unit_for_byte_size(max_cset),
100           byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free));
101 
102   for (size_t idx = 0; idx < size; idx++) {
103     ShenandoahHeapRegion* r = data[idx]._region;
104     if (cset->is_preselected(r->index())) {
105       continue;
106     }
107     if (r->age() < tenuring_threshold) {
108       size_t new_cset = cur_cset + r->get_live_data_bytes();
109       size_t region_garbage = r->garbage();
110       size_t new_garbage = cur_young_garbage + region_garbage;
111       bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage);
112       assert(r->is_young(), "Only young candidates expected in the data array");
113       if ((new_cset <= max_cset) && (add_regardless || (region_garbage > garbage_threshold))) {
114         cur_cset = new_cset;
115         cur_young_garbage = new_garbage;
116         cset->add_region(r);
117       }
118     }
119     // Note that we do not add aged regions if they were not pre-selected.  The reason they were not preselected
120     // is because there is not sufficient room in old-gen to hold their to-be-promoted live objects or because
121     // they are to be promoted in place.
122   }
123 }
124 
125 
126 bool ShenandoahYoungHeuristics::should_start_gc() {
127   auto heap = ShenandoahGenerationalHeap::heap();
128   ShenandoahOldHeuristics* old_heuristics = heap->old_generation()->heuristics();
129 
130   // Checks that an old cycle has run for at least ShenandoahMinimumOldMarkTimeMs before allowing a young cycle.
131   if (ShenandoahMinimumOldMarkTimeMs > 0 && heap->is_concurrent_old_mark_in_progress()) {
132     size_t old_mark_elapsed = size_t(old_heuristics->elapsed_cycle_time() * 1000);
133     if (old_mark_elapsed < ShenandoahMinimumOldMarkTimeMs) {
134       return false;
135     }
136   }
137 
138   // inherited triggers have already decided to start a cycle, so no further evaluation is required
139   if (ShenandoahAdaptiveHeuristics::should_start_gc()) {
140     return true;
141   }
142 
143   // Get through promotions and mixed evacuations as quickly as possible.  These cycles sometimes require significantly
144   // more time than traditional young-generation cycles so start them up as soon as possible.  This is a "mitigation"
145   // for the reality that old-gen and young-gen activities are not truly "concurrent".  If there is old-gen work to
146   // be done, we start up the young-gen GC threads so they can do some of this old-gen work.  As implemented, promotion
147   // gets priority over old-gen marking.
148   size_t promo_expedite_threshold = percent_of(heap->young_generation()->max_capacity(), ShenandoahExpeditePromotionsThreshold);
149   size_t promo_potential = heap->old_generation()->get_promotion_potential();
150   if (promo_potential > promo_expedite_threshold) {
151     // Detect unsigned arithmetic underflow
152     assert(promo_potential < heap->capacity(), "Sanity");
153     log_info(gc)("Trigger (%s): expedite promotion of " SIZE_FORMAT "%s",
154                  _space_info->name(),
155                  byte_size_in_proper_unit(promo_potential),
156                  proper_unit_for_byte_size(promo_potential));
157     return true;
158   }
159 
160   size_t mixed_candidates = old_heuristics->unprocessed_old_collection_candidates();
161   if (mixed_candidates > ShenandoahExpediteMixedThreshold && !heap->is_concurrent_weak_root_in_progress()) {
162     // We need to run young GC in order to open up some free heap regions so we can finish mixed evacuations.
163     // If concurrent weak root processing is in progress, it means the old cycle has chosen mixed collection
164     // candidates, but has not completed. There is no point in trying to start the young cycle before the old
165     // cycle completes.
166     log_info(gc)("Trigger (%s): expedite mixed evacuation of " SIZE_FORMAT " regions",
167                  _space_info->name(), mixed_candidates);
168     return true;
169   }
170 
171   return false;
172 }
173 
174 // Return a conservative estimate of how much memory can be allocated before we need to start GC. The estimate is based
175 // on memory that is currently available within young generation plus all of the memory that will be added to the young
176 // generation at the end of the current cycle (as represented by young_regions_to_be_reclaimed) and on the anticipated
177 // amount of time required to perform a GC.
178 size_t ShenandoahYoungHeuristics::bytes_of_allocation_runway_before_gc_trigger(size_t young_regions_to_be_reclaimed) {
179   size_t capacity = _space_info->max_capacity();
180   size_t usage = _space_info->used();
181   size_t available = (capacity > usage)? capacity - usage: 0;
182   size_t allocated = _space_info->bytes_allocated_since_gc_start();
183 
184   size_t available_young_collected = ShenandoahHeap::heap()->collection_set()->get_young_available_bytes_collected();
185   size_t anticipated_available =
186           available + young_regions_to_be_reclaimed * ShenandoahHeapRegion::region_size_bytes() - available_young_collected;
187   size_t spike_headroom = capacity * ShenandoahAllocSpikeFactor / 100;
188   size_t penalties      = capacity * _gc_time_penalties / 100;
189 
190   double rate = _allocation_rate.sample(allocated);
191 
192   // At what value of available, would avg and spike triggers occur?
193   //  if allocation_headroom < avg_cycle_time * avg_alloc_rate, then we experience avg trigger
194   //  if allocation_headroom < avg_cycle_time * rate, then we experience spike trigger if is_spiking
195   //
196   // allocation_headroom =
197   //     0, if penalties > available or if penalties + spike_headroom > available
198   //     available - penalties - spike_headroom, otherwise
199   //
200   // so we trigger if available - penalties - spike_headroom < avg_cycle_time * avg_alloc_rate, which is to say
201   //                  available < avg_cycle_time * avg_alloc_rate + penalties + spike_headroom
202   //            or if available < penalties + spike_headroom
203   //
204   // since avg_cycle_time * avg_alloc_rate > 0, the first test is sufficient to test both conditions
205   //
206   // thus, evac_slack_avg is MIN2(0,  available - avg_cycle_time * avg_alloc_rate + penalties + spike_headroom)
207   //
208   // similarly, evac_slack_spiking is MIN2(0, available - avg_cycle_time * rate + penalties + spike_headroom)
209   // but evac_slack_spiking is only relevant if is_spiking, as defined below.
210 
211   double avg_cycle_time = _gc_cycle_time_history->davg() + (_margin_of_error_sd * _gc_cycle_time_history->dsd());
212 
213   // TODO: Consider making conservative adjustments to avg_cycle_time, such as: (avg_cycle_time *= 2) in cases where
214   // we expect a longer-than-normal GC duration.  This includes mixed evacuations, evacuation that perform promotion
215   // including promotion in place, and OLD GC bootstrap cycles.  It has been observed that these cycles sometimes
216   // require twice or more the duration of "normal" GC cycles.  We have experimented with this approach.  While it
217   // does appear to reduce the frequency of degenerated cycles due to late triggers, it also has the effect of reducing
218   // evacuation slack so that there is less memory available to be transferred to OLD.  The result is that we
219   // throttle promotion and it takes too long to move old objects out of the young generation.
220 
221   double avg_alloc_rate = _allocation_rate.upper_bound(_margin_of_error_sd);
222   size_t evac_slack_avg;
223   if (anticipated_available > avg_cycle_time * avg_alloc_rate + penalties + spike_headroom) {
224     evac_slack_avg = anticipated_available - (avg_cycle_time * avg_alloc_rate + penalties + spike_headroom);
225   } else {
226     // we have no slack because it's already time to trigger
227     evac_slack_avg = 0;
228   }
229 
230   bool is_spiking = _allocation_rate.is_spiking(rate, _spike_threshold_sd);
231   size_t evac_slack_spiking;
232   if (is_spiking) {
233     if (anticipated_available > avg_cycle_time * rate + penalties + spike_headroom) {
234       evac_slack_spiking = anticipated_available - (avg_cycle_time * rate + penalties + spike_headroom);
235     } else {
236       // we have no slack because it's already time to trigger
237       evac_slack_spiking = 0;
238     }
239   } else {
240     evac_slack_spiking = evac_slack_avg;
241   }
242 
243   size_t threshold = min_free_threshold();
244   size_t evac_min_threshold = (anticipated_available > threshold)? anticipated_available - threshold: 0;
245   return MIN3(evac_slack_spiking, evac_slack_avg, evac_min_threshold);
246 }
247