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