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/shenandoahGlobalHeuristics.hpp"
 28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
 29 #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
 30 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
 31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
 32 
 33 #include "utilities/quickSort.hpp"
 34 
 35 ShenandoahGlobalHeuristics::ShenandoahGlobalHeuristics(ShenandoahGlobalGeneration* generation)
 36         : ShenandoahGenerationalHeuristics(generation) {
 37 }
 38 
 39 
 40 void ShenandoahGlobalHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
 41                                                                        RegionData* data, size_t size,
 42                                                                        size_t actual_free) {
 43   // The logic for cset selection in adaptive is as follows:
 44   //
 45   //   1. We cannot get cset larger than available free space. Otherwise we guarantee OOME
 46   //      during evacuation, and thus guarantee full GC. In practice, we also want to let
 47   //      application to allocate something. This is why we limit CSet to some fraction of
 48   //      available space. In non-overloaded heap, max_cset would contain all plausible candidates
 49   //      over garbage threshold.
 50   //
 51   //   2. We should not get cset too low so that free threshold would not be met right
 52   //      after the cycle. Otherwise we get back-to-back cycles for no reason if heap is
 53   //      too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero.
 54   //
 55   // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates
 56   // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before
 57   // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme,
 58   // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit.
 59 
 60   // In generational mode, the sort order within the data array is not strictly descending amounts of garbage.  In
 61   // particular, regions that have reached tenure age will be sorted into this array before younger regions that contain
 62   // more garbage.  This represents one of the reasons why we keep looking at regions even after we decide, for example,
 63   // to exclude one of the regions because it might require evacuation of too much live data.
 64 
 65 
 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_global_collection_set(cset, data, size, actual_free, cur_young_garbage);
 73 
 74   log_cset_composition(cset);
 75 }
 76 
 77 
 78 void ShenandoahGlobalHeuristics::choose_global_collection_set(ShenandoahCollectionSet* cset,
 79                                                               const ShenandoahHeuristics::RegionData* data,
 80                                                               size_t size, size_t actual_free,
 81                                                               size_t cur_young_garbage) const {
 82   auto heap = ShenandoahGenerationalHeap::heap();
 83   size_t region_size_bytes = ShenandoahHeapRegion::region_size_bytes();
 84   size_t capacity = heap->young_generation()->max_capacity();
 85   size_t garbage_threshold = region_size_bytes * ShenandoahGarbageThreshold / 100;
 86   size_t ignore_threshold = region_size_bytes * ShenandoahIgnoreGarbageThreshold / 100;
 87   const uint tenuring_threshold = heap->age_census()->tenuring_threshold();
 88 
 89   size_t young_evac_reserve = heap->young_generation()->get_evacuation_reserve();
 90   size_t old_evac_reserve = heap->old_generation()->get_evacuation_reserve();
 91   size_t max_young_cset = (size_t) (young_evac_reserve / ShenandoahEvacWaste);
 92   size_t young_cur_cset = 0;
 93   size_t max_old_cset = (size_t) (old_evac_reserve / ShenandoahOldEvacWaste);
 94   size_t old_cur_cset = 0;
 95 
 96   // Figure out how many unaffiliated young regions are dedicated to mutator and to evacuator.  Allow the young
 97   // collector's unaffiliated regions to be transferred to old-gen if old-gen has more easily reclaimed garbage
 98   // than young-gen.  At the end of this cycle, any excess regions remaining in old-gen will be transferred back
 99   // to young.  Do not transfer the mutator's unaffiliated regions to old-gen.  Those must remain available
100   // to the mutator as it needs to be able to consume this memory during concurrent GC.
101 
102   size_t unaffiliated_young_regions = heap->young_generation()->free_unaffiliated_regions();
103   size_t unaffiliated_young_memory = unaffiliated_young_regions * region_size_bytes;
104 
105   if (unaffiliated_young_memory > max_young_cset) {
106     size_t unaffiliated_mutator_memory = unaffiliated_young_memory - max_young_cset;
107     unaffiliated_young_memory -= unaffiliated_mutator_memory;
108     unaffiliated_young_regions = unaffiliated_young_memory / region_size_bytes; // round down
109     unaffiliated_young_memory = unaffiliated_young_regions * region_size_bytes;
110   }
111 
112   // We'll affiliate these unaffiliated regions with either old or young, depending on need.
113   max_young_cset -= unaffiliated_young_memory;
114 
115   // Keep track of how many regions we plan to transfer from young to old.
116   size_t regions_transferred_to_old = 0;
117 
118   size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_young_cset;
119   size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0;
120 
121   log_info(gc, ergo)("Adaptive CSet Selection for GLOBAL. Max Young Evacuation: " SIZE_FORMAT
122                      "%s, Max Old Evacuation: " SIZE_FORMAT "%s, Actual Free: " SIZE_FORMAT "%s.",
123                      byte_size_in_proper_unit(max_young_cset), proper_unit_for_byte_size(max_young_cset),
124                      byte_size_in_proper_unit(max_old_cset), proper_unit_for_byte_size(max_old_cset),
125                      byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free));
126 
127   for (size_t idx = 0; idx < size; idx++) {
128     ShenandoahHeapRegion* r = data[idx]._region;
129     if (cset->is_preselected(r->index())) {
130       fatal("There should be no preselected regions during GLOBAL GC");
131       continue;
132     }
133     bool add_region = false;
134     if (r->is_old() || (r->age() >= tenuring_threshold)) {
135       size_t new_cset = old_cur_cset + r->get_live_data_bytes();
136       if ((r->garbage() > garbage_threshold)) {
137         while ((new_cset > max_old_cset) && (unaffiliated_young_regions > 0)) {
138           unaffiliated_young_regions--;
139           regions_transferred_to_old++;
140           max_old_cset += region_size_bytes / ShenandoahOldEvacWaste;
141         }
142       }
143       if ((new_cset <= max_old_cset) && (r->garbage() > garbage_threshold)) {
144         add_region = true;
145         old_cur_cset = new_cset;
146       }
147     } else {
148       assert(r->is_young() && (r->age() < tenuring_threshold), "DeMorgan's law (assuming r->is_affiliated)");
149       size_t new_cset = young_cur_cset + r->get_live_data_bytes();
150       size_t region_garbage = r->garbage();
151       size_t new_garbage = cur_young_garbage + region_garbage;
152       bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage);
153 
154       if (add_regardless || (r->garbage() > garbage_threshold)) {
155         while ((new_cset > max_young_cset) && (unaffiliated_young_regions > 0)) {
156           unaffiliated_young_regions--;
157           max_young_cset += region_size_bytes / ShenandoahEvacWaste;
158         }
159       }
160       if ((new_cset <= max_young_cset) && (add_regardless || (region_garbage > garbage_threshold))) {
161         add_region = true;
162         young_cur_cset = new_cset;
163         cur_young_garbage = new_garbage;
164       }
165     }
166     if (add_region) {
167       cset->add_region(r);
168     }
169   }
170 
171   if (regions_transferred_to_old > 0) {
172     heap->generation_sizer()->force_transfer_to_old(regions_transferred_to_old);
173     heap->young_generation()->set_evacuation_reserve(young_evac_reserve - regions_transferred_to_old * region_size_bytes);
174     heap->old_generation()->set_evacuation_reserve(old_evac_reserve + regions_transferred_to_old * region_size_bytes);
175   }
176 }