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/shenandoahGenerationalHeuristics.hpp"
 28 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
 29 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
 30 #include "gc/shenandoah/shenandoahGeneration.hpp"
 31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 32 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 33 
 34 #include "logging/log.hpp"
 35 
 36 ShenandoahGenerationalHeuristics::ShenandoahGenerationalHeuristics(ShenandoahGeneration* generation)
 37         : ShenandoahAdaptiveHeuristics(generation), _generation(generation) {
 38 }
 39 
 40 void ShenandoahGenerationalHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) {
 41   assert(collection_set->is_empty(), "Must be empty");
 42 
 43   ShenandoahHeap* heap = ShenandoahHeap::heap();
 44   size_t region_size_bytes = ShenandoahHeapRegion::region_size_bytes();
 45 
 46 
 47   // Check all pinned regions have updated status before choosing the collection set.
 48   heap->assert_pinned_region_status();
 49 
 50   // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.
 51 
 52   size_t num_regions = heap->num_regions();
 53 
 54   RegionData* candidates = _region_data;
 55 
 56   size_t cand_idx = 0;
 57   size_t preselected_candidates = 0;
 58 
 59   size_t total_garbage = 0;
 60 
 61   size_t immediate_garbage = 0;
 62   size_t immediate_regions = 0;
 63 
 64   size_t free = 0;
 65   size_t free_regions = 0;
 66 
 67   const uint tenuring_threshold = heap->age_census()->tenuring_threshold();
 68 
 69   // This counts number of humongous regions that we intend to promote in this cycle.
 70   size_t humongous_regions_promoted = 0;
 71   // This counts bytes of memory used by humongous regions to be promoted in place.
 72   size_t humongous_bytes_promoted = 0;
 73   // This counts number of regular regions that will be promoted in place.
 74   size_t regular_regions_promoted_in_place = 0;
 75   // This counts bytes of memory used by regular regions to be promoted in place.
 76   size_t regular_regions_promoted_usage = 0;
 77 
 78   for (size_t i = 0; i < num_regions; i++) {
 79     ShenandoahHeapRegion* region = heap->get_region(i);
 80     if (!_generation->contains(region)) {
 81       continue;
 82     }
 83     size_t garbage = region->garbage();
 84     total_garbage += garbage;
 85     if (region->is_empty()) {
 86       free_regions++;
 87       free += region_size_bytes;
 88     } else if (region->is_regular()) {
 89       if (!region->has_live()) {
 90         // We can recycle it right away and put it in the free set.
 91         immediate_regions++;
 92         immediate_garbage += garbage;
 93         region->make_trash_immediate();
 94       } else {
 95         bool is_candidate;
 96         // This is our candidate for later consideration.
 97         if (collection_set->is_preselected(i)) {
 98           assert(region->age() >= tenuring_threshold, "Preselection filter");
 99           is_candidate = true;
100           preselected_candidates++;
101           // Set garbage value to maximum value to force this into the sorted collection set.
102           garbage = region_size_bytes;
103         } else if (region->is_young() && (region->age() >= tenuring_threshold)) {
104           // Note that for GLOBAL GC, region may be OLD, and OLD regions do not qualify for pre-selection
105 
106           // This region is old enough to be promoted but it was not preselected, either because its garbage is below
107           // ShenandoahOldGarbageThreshold so it will be promoted in place, or because there is not sufficient room
108           // in old gen to hold the evacuated copies of this region's live data.  In both cases, we choose not to
109           // place this region into the collection set.
110           if (region->get_top_before_promote() != nullptr) {
111             regular_regions_promoted_in_place++;
112             regular_regions_promoted_usage += region->used_before_promote();
113           }
114           is_candidate = false;
115         } else {
116           is_candidate = true;
117         }
118         if (is_candidate) {
119           candidates[cand_idx]._region = region;
120           candidates[cand_idx]._u._garbage = garbage;
121           cand_idx++;
122         }
123       }
124     } else if (region->is_humongous_start()) {
125       // Reclaim humongous regions here, and count them as the immediate garbage
126 #ifdef ASSERT
127       bool reg_live = region->has_live();
128       bool bm_live = heap->complete_marking_context()->is_marked(cast_to_oop(region->bottom()));
129       assert(reg_live == bm_live,
130              "Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT,
131              BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words());
132 #endif
133       if (!region->has_live()) {
134         heap->trash_humongous_region_at(region);
135 
136         // Count only the start. Continuations would be counted on "trash" path
137         immediate_regions++;
138         immediate_garbage += garbage;
139       } else {
140         if (region->is_young() && region->age() >= tenuring_threshold) {
141           oop obj = cast_to_oop(region->bottom());
142           size_t humongous_regions = ShenandoahHeapRegion::required_regions(obj->size() * HeapWordSize);
143           humongous_regions_promoted += humongous_regions;
144           humongous_bytes_promoted += obj->size() * HeapWordSize;
145         }
146       }
147     } else if (region->is_trash()) {
148       // Count in just trashed collection set, during coalesced CM-with-UR
149       immediate_regions++;
150       immediate_garbage += garbage;
151     }
152   }
153   heap->reserve_promotable_humongous_regions(humongous_regions_promoted);
154   heap->reserve_promotable_humongous_usage(humongous_bytes_promoted);
155   heap->reserve_promotable_regular_regions(regular_regions_promoted_in_place);
156   heap->reserve_promotable_regular_usage(regular_regions_promoted_usage);
157   log_info(gc, ergo)("Planning to promote in place " SIZE_FORMAT " humongous regions and " SIZE_FORMAT
158                      " regular regions, spanning a total of " SIZE_FORMAT " used bytes",
159                      humongous_regions_promoted, regular_regions_promoted_in_place,
160                      humongous_regions_promoted * ShenandoahHeapRegion::region_size_bytes() +
161                      regular_regions_promoted_usage);
162 
163   // Step 2. Look back at garbage statistics, and decide if we want to collect anything,
164   // given the amount of immediately reclaimable garbage. If we do, figure out the collection set.
165 
166   assert (immediate_garbage <= total_garbage,
167           "Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s",
168           byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage),
169           byte_size_in_proper_unit(total_garbage), proper_unit_for_byte_size(total_garbage));
170 
171   size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage);
172 
173   bool doing_promote_in_place = (humongous_regions_promoted + regular_regions_promoted_in_place > 0);
174   if (doing_promote_in_place || (preselected_candidates > 0) || (immediate_percent <= ShenandoahImmediateThreshold)) {
175     // Only young collections need to prime the collection set.
176     if (_generation->is_young()) {
177       heap->old_heuristics()->prime_collection_set(collection_set);
178     }
179 
180     // Call the subclasses to add young-gen regions into the collection set.
181     choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free);
182   } else {
183     // We are going to skip evacuation and update refs because we reclaimed
184     // sufficient amounts of immediate garbage.
185     heap->shenandoah_policy()->record_abbreviated_cycle();
186   }
187 
188   if (collection_set->has_old_regions()) {
189     heap->shenandoah_policy()->record_mixed_cycle();
190   }
191 
192   size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage);
193   size_t collectable_garbage = collection_set->garbage() + immediate_garbage;
194   size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage);
195 
196   log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "
197                      "Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions, "
198                      "CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions",
199 
200                      byte_size_in_proper_unit(collectable_garbage),
201                      proper_unit_for_byte_size(collectable_garbage),
202                      collectable_garbage_percent,
203 
204                      byte_size_in_proper_unit(immediate_garbage),
205                      proper_unit_for_byte_size(immediate_garbage),
206                      immediate_percent,
207                      immediate_regions,
208 
209                      byte_size_in_proper_unit(collection_set->garbage()),
210                      proper_unit_for_byte_size(collection_set->garbage()),
211                      cset_percent,
212                      collection_set->count());
213 
214   if (collection_set->garbage() > 0) {
215     size_t young_evac_bytes = collection_set->get_young_bytes_reserved_for_evacuation();
216     size_t promote_evac_bytes = collection_set->get_young_bytes_to_be_promoted();
217     size_t old_evac_bytes = collection_set->get_old_bytes_reserved_for_evacuation();
218     size_t total_evac_bytes = young_evac_bytes + promote_evac_bytes + old_evac_bytes;
219     log_info(gc, ergo)("Evacuation Targets: YOUNG: " SIZE_FORMAT "%s, "
220                        "PROMOTE: " SIZE_FORMAT "%s, "
221                        "OLD: " SIZE_FORMAT "%s, "
222                        "TOTAL: " SIZE_FORMAT "%s",
223                        byte_size_in_proper_unit(young_evac_bytes), proper_unit_for_byte_size(young_evac_bytes),
224                        byte_size_in_proper_unit(promote_evac_bytes), proper_unit_for_byte_size(promote_evac_bytes),
225                        byte_size_in_proper_unit(old_evac_bytes), proper_unit_for_byte_size(old_evac_bytes),
226                        byte_size_in_proper_unit(total_evac_bytes), proper_unit_for_byte_size(total_evac_bytes));
227   }
228 }
229 
230 
231 size_t ShenandoahGenerationalHeuristics::add_preselected_regions_to_collection_set(ShenandoahCollectionSet* cset,
232                                                                                    const RegionData* data,
233                                                                                    size_t size) const {
234 #ifdef ASSERT
235   const uint tenuring_threshold = ShenandoahHeap::heap()->age_census()->tenuring_threshold();
236 #endif
237 
238   // cur_young_garbage represents the amount of memory to be reclaimed from young-gen.  In the case that live objects
239   // are known to be promoted out of young-gen, we count this as cur_young_garbage because this memory is reclaimed
240   // from young-gen and becomes available to serve future young-gen allocation requests.
241   size_t cur_young_garbage = 0;
242   for (size_t idx = 0; idx < size; idx++) {
243     ShenandoahHeapRegion* r = data[idx]._region;
244     if (cset->is_preselected(r->index())) {
245       assert(r->age() >= tenuring_threshold, "Preselected regions must have tenure age");
246       // Entire region will be promoted, This region does not impact young-gen or old-gen evacuation reserve.
247       // This region has been pre-selected and its impact on promotion reserve is already accounted for.
248 
249       // r->used() is r->garbage() + r->get_live_data_bytes()
250       // Since all live data in this region is being evacuated from young-gen, it is as if this memory
251       // is garbage insofar as young-gen is concerned.  Counting this as garbage reduces the need to
252       // reclaim highly utilized young-gen regions just for the sake of finding min_garbage to reclaim
253       // within young-gen memory.
254 
255       cur_young_garbage += r->garbage();
256       cset->add_region(r);
257     }
258   }
259   return cur_young_garbage;
260 }
261 
262 void ShenandoahGenerationalHeuristics::log_cset_composition(ShenandoahCollectionSet* cset) const {
263   size_t collected_old = cset->get_old_bytes_reserved_for_evacuation();
264   size_t collected_promoted = cset->get_young_bytes_to_be_promoted();
265   size_t collected_young = cset->get_young_bytes_reserved_for_evacuation();
266 
267   log_info(gc, ergo)(
268           "Chosen CSet evacuates young: " SIZE_FORMAT "%s (of which at least: " SIZE_FORMAT "%s are to be promoted), "
269           "old: " SIZE_FORMAT "%s",
270           byte_size_in_proper_unit(collected_young), proper_unit_for_byte_size(collected_young),
271           byte_size_in_proper_unit(collected_promoted), proper_unit_for_byte_size(collected_promoted),
272           byte_size_in_proper_unit(collected_old), proper_unit_for_byte_size(collected_old));
273 }