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