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