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/shenandoahGenerationalHeap.inline.hpp" 30 #include "gc/shenandoah/shenandoahGlobalGeneration.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 // Better select garbage-first regions 44 QuickSort::sort<RegionData>(data, (int) size, compare_by_garbage, false); 45 46 choose_global_collection_set(cset, data, size, actual_free, 0 /* cur_young_garbage */); 47 48 log_cset_composition(cset); 49 } 50 51 52 void ShenandoahGlobalHeuristics::choose_global_collection_set(ShenandoahCollectionSet* cset, 53 const ShenandoahHeuristics::RegionData* data, 54 size_t size, size_t actual_free, 55 size_t cur_young_garbage) const { 56 auto heap = ShenandoahGenerationalHeap::heap(); 57 size_t region_size_bytes = ShenandoahHeapRegion::region_size_bytes(); 58 size_t capacity = heap->soft_max_capacity(); 59 size_t garbage_threshold = region_size_bytes * ShenandoahGarbageThreshold / 100; 60 size_t ignore_threshold = region_size_bytes * ShenandoahIgnoreGarbageThreshold / 100; 61 62 size_t young_evac_reserve = heap->young_generation()->get_evacuation_reserve(); 63 size_t old_evac_reserve = heap->old_generation()->get_evacuation_reserve(); 64 size_t max_young_cset = (size_t) (young_evac_reserve / ShenandoahEvacWaste); 65 size_t young_cur_cset = 0; 66 size_t max_old_cset = (size_t) (old_evac_reserve / ShenandoahOldEvacWaste); 67 size_t old_cur_cset = 0; 68 69 // Figure out how many unaffiliated young regions are dedicated to mutator and to evacuator. Allow the young 70 // collector's unaffiliated regions to be transferred to old-gen if old-gen has more easily reclaimed garbage 71 // than young-gen. At the end of this cycle, any excess regions remaining in old-gen will be transferred back 72 // to young. Do not transfer the mutator's unaffiliated regions to old-gen. Those must remain available 73 // to the mutator as it needs to be able to consume this memory during concurrent GC. 74 75 size_t unaffiliated_young_regions = heap->young_generation()->free_unaffiliated_regions(); 76 size_t unaffiliated_young_memory = unaffiliated_young_regions * region_size_bytes; 77 78 if (unaffiliated_young_memory > max_young_cset) { 79 size_t unaffiliated_mutator_memory = unaffiliated_young_memory - max_young_cset; 80 unaffiliated_young_memory -= unaffiliated_mutator_memory; 81 unaffiliated_young_regions = unaffiliated_young_memory / region_size_bytes; // round down 82 unaffiliated_young_memory = unaffiliated_young_regions * region_size_bytes; 83 } 84 85 // We'll affiliate these unaffiliated regions with either old or young, depending on need. 86 max_young_cset -= unaffiliated_young_memory; 87 88 // Keep track of how many regions we plan to transfer from young to old. 89 size_t regions_transferred_to_old = 0; 90 91 size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_young_cset; 92 size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0; 93 94 log_info(gc, ergo)("Adaptive CSet Selection for GLOBAL. Max Young Evacuation: " SIZE_FORMAT 95 "%s, Max Old Evacuation: " SIZE_FORMAT "%s, Actual Free: " SIZE_FORMAT "%s.", 96 byte_size_in_proper_unit(max_young_cset), proper_unit_for_byte_size(max_young_cset), 97 byte_size_in_proper_unit(max_old_cset), proper_unit_for_byte_size(max_old_cset), 98 byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free)); 99 100 for (size_t idx = 0; idx < size; idx++) { 101 ShenandoahHeapRegion* r = data[idx].get_region(); 102 assert(!cset->is_preselected(r->index()), "There should be no preselected regions during GLOBAL GC"); 103 bool add_region = false; 104 if (r->is_old() || heap->is_tenurable(r)) { 105 size_t new_cset = old_cur_cset + r->get_live_data_bytes(); 106 if ((r->garbage() > garbage_threshold)) { 107 while ((new_cset > max_old_cset) && (unaffiliated_young_regions > 0)) { 108 unaffiliated_young_regions--; 109 regions_transferred_to_old++; 110 max_old_cset += region_size_bytes / ShenandoahOldEvacWaste; 111 } 112 } 113 if ((new_cset <= max_old_cset) && (r->garbage() > garbage_threshold)) { 114 add_region = true; 115 old_cur_cset = new_cset; 116 } 117 } else { 118 assert(r->is_young() && !heap->is_tenurable(r), "DeMorgan's law (assuming r->is_affiliated)"); 119 size_t new_cset = young_cur_cset + r->get_live_data_bytes(); 120 size_t region_garbage = r->garbage(); 121 size_t new_garbage = cur_young_garbage + region_garbage; 122 bool add_regardless = (region_garbage > ignore_threshold) && (new_garbage < min_garbage); 123 124 if (add_regardless || (r->garbage() > garbage_threshold)) { 125 while ((new_cset > max_young_cset) && (unaffiliated_young_regions > 0)) { 126 unaffiliated_young_regions--; 127 max_young_cset += region_size_bytes / ShenandoahEvacWaste; 128 } 129 } 130 if ((new_cset <= max_young_cset) && (add_regardless || (region_garbage > garbage_threshold))) { 131 add_region = true; 132 young_cur_cset = new_cset; 133 cur_young_garbage = new_garbage; 134 } 135 } 136 if (add_region) { 137 cset->add_region(r); 138 } 139 } 140 141 if (regions_transferred_to_old > 0) { 142 heap->generation_sizer()->force_transfer_to_old(regions_transferred_to_old); 143 heap->young_generation()->set_evacuation_reserve(young_evac_reserve - regions_transferred_to_old * region_size_bytes); 144 heap->old_generation()->set_evacuation_reserve(old_evac_reserve + regions_transferred_to_old * region_size_bytes); 145 } 146 }