1 /* 2 * Copyright (c) 2018, Red Hat, Inc. All rights reserved. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 #include "precompiled.hpp" 25 26 #include "gc_implementation/shenandoah/heuristics/shenandoahAdaptiveHeuristics.hpp" 27 #include "gc_implementation/shenandoah/shenandoahCollectionSet.hpp" 28 #include "gc_implementation/shenandoah/shenandoahFreeSet.hpp" 29 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp" 30 #include "gc_implementation/shenandoah/shenandoahHeapRegion.inline.hpp" 31 #include "gc_implementation/shenandoah/shenandoahLogging.hpp" 32 #include "utilities/quickSort.hpp" 33 34 ShenandoahAdaptiveHeuristics::ShenandoahAdaptiveHeuristics() : 35 ShenandoahHeuristics() {} 36 37 ShenandoahAdaptiveHeuristics::~ShenandoahAdaptiveHeuristics() {} 38 39 void ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset, 40 RegionData* data, size_t size, 41 size_t actual_free) { 42 size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100; 43 44 // The logic for cset selection in adaptive is as follows: 45 // 46 // 1. We cannot get cset larger than available free space. Otherwise we guarantee OOME 47 // during evacuation, and thus guarantee full GC. In practice, we also want to let 48 // application to allocate something. This is why we limit CSet to some fraction of 49 // available space. In non-overloaded heap, max_cset would contain all plausible candidates 50 // over garbage threshold. 51 // 52 // 2. We should not get cset too low so that free threshold would not be met right 53 // after the cycle. Otherwise we get back-to-back cycles for no reason if heap is 54 // too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero. 55 // 56 // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates 57 // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before 58 // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme, 59 // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit. 60 61 size_t capacity = ShenandoahHeap::heap()->soft_max_capacity(); 62 size_t max_cset = (size_t)((1.0 * capacity / 100 * ShenandoahEvacReserve) / ShenandoahEvacWaste); 63 size_t free_target = (capacity / 100 * ShenandoahMinFreeThreshold) + max_cset; 64 size_t min_garbage = (free_target > actual_free ? (free_target - actual_free) : 0); 65 66 log_info(gc, ergo)("Adaptive CSet Selection. Target Free: " SIZE_FORMAT "%s, Actual Free: " 67 SIZE_FORMAT "%s, Max CSet: " SIZE_FORMAT "%s, Min Garbage: " SIZE_FORMAT "%s", 68 byte_size_in_proper_unit(free_target), proper_unit_for_byte_size(free_target), 69 byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free), 70 byte_size_in_proper_unit(max_cset), proper_unit_for_byte_size(max_cset), 71 byte_size_in_proper_unit(min_garbage), proper_unit_for_byte_size(min_garbage)); 72 73 // Better select garbage-first regions 74 QuickSort::sort<RegionData>(data, (int)size, compare_by_garbage, false); 75 76 size_t cur_cset = 0; 77 size_t cur_garbage = 0; 78 79 for (size_t idx = 0; idx < size; idx++) { 80 ShenandoahHeapRegion* r = data[idx]._region; 81 82 size_t new_cset = cur_cset + r->get_live_data_bytes(); 83 size_t new_garbage = cur_garbage + r->garbage(); 84 85 if (new_cset > max_cset) { 86 break; 87 } 88 89 if ((new_garbage < min_garbage) || (r->garbage() > garbage_threshold)) { 90 cset->add_region(r); 91 cur_cset = new_cset; 92 cur_garbage = new_garbage; 93 } 94 } 95 } 96 97 void ShenandoahAdaptiveHeuristics::record_cycle_start() { 98 ShenandoahHeuristics::record_cycle_start(); 99 } 100 101 bool ShenandoahAdaptiveHeuristics::should_start_gc() const { 102 ShenandoahHeap* heap = ShenandoahHeap::heap(); 103 size_t max_capacity = heap->max_capacity(); 104 size_t capacity = heap->soft_max_capacity(); 105 size_t available = heap->free_set()->available(); 106 107 // Make sure the code below treats available without the soft tail. 108 size_t soft_tail = max_capacity - capacity; 109 available = (available > soft_tail) ? (available - soft_tail) : 0; 110 111 // Check if we are falling below the worst limit, time to trigger the GC, regardless of 112 // anything else. 113 size_t min_threshold = capacity / 100 * ShenandoahMinFreeThreshold; 114 if (available < min_threshold) { 115 log_info(gc)("Trigger: Free (" SIZE_FORMAT "%s) is below minimum threshold (" SIZE_FORMAT "%s)", 116 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available), 117 byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold)); 118 return true; 119 } 120 121 // Check if are need to learn a bit about the application 122 const size_t max_learn = ShenandoahLearningSteps; 123 if (_gc_times_learned < max_learn) { 124 size_t init_threshold = capacity / 100 * ShenandoahInitFreeThreshold; 125 if (available < init_threshold) { 126 log_info(gc)("Trigger: Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "%s) is below initial threshold (" SIZE_FORMAT "%s)", 127 _gc_times_learned + 1, max_learn, 128 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available), 129 byte_size_in_proper_unit(init_threshold), proper_unit_for_byte_size(init_threshold)); 130 return true; 131 } 132 } 133 134 // Check if allocation headroom is still okay. This also factors in: 135 // 1. Some space to absorb allocation spikes 136 // 2. Accumulated penalties from Degenerated and Full GC 137 138 size_t allocation_headroom = available; 139 140 size_t spike_headroom = capacity / 100 * ShenandoahAllocSpikeFactor; 141 size_t penalties = capacity / 100 * _gc_time_penalties; 142 143 allocation_headroom -= MIN2(allocation_headroom, spike_headroom); 144 allocation_headroom -= MIN2(allocation_headroom, penalties); 145 146 // TODO: Allocation rate is way too averaged to be useful during state changes 147 148 double average_gc = _gc_time_history->avg(); 149 double time_since_last = time_since_last_gc(); 150 double allocation_rate = heap->bytes_allocated_since_gc_start() / time_since_last; 151 152 if (average_gc > allocation_headroom / allocation_rate) { 153 log_info(gc)("Trigger: Average GC time (%.2f ms) is above the time for allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s)", 154 average_gc * 1000, 155 byte_size_in_proper_unit(allocation_rate), proper_unit_for_byte_size(allocation_rate), 156 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom)); 157 log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "%s (free) - " SIZE_FORMAT "%s (spike) - " SIZE_FORMAT "%s (penalties) = " SIZE_FORMAT "%s", 158 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available), 159 byte_size_in_proper_unit(spike_headroom), proper_unit_for_byte_size(spike_headroom), 160 byte_size_in_proper_unit(penalties), proper_unit_for_byte_size(penalties), 161 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom)); 162 return true; 163 } 164 165 return ShenandoahHeuristics::should_start_gc(); 166 }