1 /* 2 * Copyright (c) 2018, 2019, Red Hat, Inc. 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/shenandoahAdaptiveHeuristics.hpp" 28 #include "gc/shenandoah/shenandoahCollectionSet.hpp" 29 #include "gc/shenandoah/shenandoahFreeSet.hpp" 30 #include "gc/shenandoah/shenandoahHeapRegion.hpp" 31 #include "logging/log.hpp" 32 #include "logging/logTag.hpp" 33 #include "utilities/quickSort.hpp" 34 35 ShenandoahAdaptiveHeuristics::ShenandoahAdaptiveHeuristics() : 36 ShenandoahHeuristics() {} 37 38 ShenandoahAdaptiveHeuristics::~ShenandoahAdaptiveHeuristics() {} 39 40 void ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset, 41 RegionData* data, size_t size, 42 size_t actual_free) { 43 size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100; 44 45 // The logic for cset selection in adaptive is as follows: 46 // 47 // 1. We cannot get cset larger than available free space. Otherwise we guarantee OOME 48 // during evacuation, and thus guarantee full GC. In practice, we also want to let 49 // application to allocate something. This is why we limit CSet to some fraction of 50 // available space. In non-overloaded heap, max_cset would contain all plausible candidates 51 // over garbage threshold. 52 // 53 // 2. We should not get cset too low so that free threshold would not be met right 54 // after the cycle. Otherwise we get back-to-back cycles for no reason if heap is 55 // too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero. 56 // 57 // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates 58 // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before 59 // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme, 60 // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit. 61 62 size_t capacity = ShenandoahHeap::heap()->max_capacity(); 63 size_t max_cset = (size_t)((1.0 * capacity / 100 * ShenandoahEvacReserve) / ShenandoahEvacWaste); 64 size_t free_target = (capacity / 100 * ShenandoahMinFreeThreshold) + max_cset; 65 size_t min_garbage = (free_target > actual_free ? (free_target - actual_free) : 0); 66 67 log_info(gc, ergo)("Adaptive CSet Selection. Target Free: " SIZE_FORMAT "%s, Actual Free: " 68 SIZE_FORMAT "%s, Max CSet: " SIZE_FORMAT "%s, Min Garbage: " SIZE_FORMAT "%s", 69 byte_size_in_proper_unit(free_target), proper_unit_for_byte_size(free_target), 70 byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free), 71 byte_size_in_proper_unit(max_cset), proper_unit_for_byte_size(max_cset), 72 byte_size_in_proper_unit(min_garbage), proper_unit_for_byte_size(min_garbage)); 73 74 // Better select garbage-first regions 75 QuickSort::sort<RegionData>(data, (int)size, compare_by_garbage, false); 76 77 size_t cur_cset = 0; 78 size_t cur_garbage = 0; 79 _bytes_in_cset = 0; 80 81 for (size_t idx = 0; idx < size; idx++) { 82 ShenandoahHeapRegion* r = data[idx]._region; 83 84 size_t new_cset = cur_cset + r->get_live_data_bytes(); 85 size_t new_garbage = cur_garbage + r->garbage(); 86 87 if (new_cset > max_cset) { 88 break; 89 } 90 91 if ((new_garbage < min_garbage) || (r->garbage() > garbage_threshold)) { 92 cset->add_region(r); 93 _bytes_in_cset += r->used(); 94 cur_cset = new_cset; 95 cur_garbage = new_garbage; 96 } 97 } 98 } 99 100 void ShenandoahAdaptiveHeuristics::record_cycle_start() { 101 ShenandoahHeuristics::record_cycle_start(); 102 } 103 104 bool ShenandoahAdaptiveHeuristics::should_start_gc() const { 105 ShenandoahHeap* heap = ShenandoahHeap::heap(); 106 size_t capacity = heap->max_capacity(); 107 size_t available = heap->free_set()->available(); 108 109 // Check if we are falling below the worst limit, time to trigger the GC, regardless of 110 // anything else. 111 size_t min_threshold = capacity / 100 * ShenandoahMinFreeThreshold; 112 if (available < min_threshold) { 113 log_info(gc)("Trigger: Free (" SIZE_FORMAT "%s) is below minimum threshold (" SIZE_FORMAT "%s)", 114 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available), 115 byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold)); 116 return true; 117 } 118 119 // Check if are need to learn a bit about the application 120 const size_t max_learn = ShenandoahLearningSteps; 121 if (_gc_times_learned < max_learn) { 122 size_t init_threshold = capacity / 100 * ShenandoahInitFreeThreshold; 123 if (available < init_threshold) { 124 log_info(gc)("Trigger: Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "%s) is below initial threshold (" SIZE_FORMAT "%s)", 125 _gc_times_learned + 1, max_learn, 126 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available), 127 byte_size_in_proper_unit(init_threshold), proper_unit_for_byte_size(init_threshold)); 128 return true; 129 } 130 } 131 132 // Check if allocation headroom is still okay. This also factors in: 133 // 1. Some space to absorb allocation spikes 134 // 2. Accumulated penalties from Degenerated and Full GC 135 136 size_t allocation_headroom = available; 137 138 size_t spike_headroom = capacity / 100 * ShenandoahAllocSpikeFactor; 139 size_t penalties = capacity / 100 * _gc_time_penalties; 140 141 allocation_headroom -= MIN2(allocation_headroom, spike_headroom); 142 allocation_headroom -= MIN2(allocation_headroom, penalties); 143 144 // TODO: Allocation rate is way too averaged to be useful during state changes 145 146 double average_gc = _gc_time_history->avg(); 147 double time_since_last = time_since_last_gc(); 148 double allocation_rate = heap->bytes_allocated_since_gc_start() / time_since_last; 149 150 if (average_gc > allocation_headroom / allocation_rate) { 151 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)", 152 average_gc * 1000, 153 byte_size_in_proper_unit(allocation_rate), proper_unit_for_byte_size(allocation_rate), 154 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom)); 155 log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "%s (free) - " SIZE_FORMAT "%s (spike) - " SIZE_FORMAT "%s (penalties) = " SIZE_FORMAT "%s", 156 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available), 157 byte_size_in_proper_unit(spike_headroom), proper_unit_for_byte_size(spike_headroom), 158 byte_size_in_proper_unit(penalties), proper_unit_for_byte_size(penalties), 159 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom)); 160 return true; 161 } 162 163 return ShenandoahHeuristics::should_start_gc(); 164 } 165 166 const char* ShenandoahAdaptiveHeuristics::name() { 167 return "adaptive"; 168 } 169 170 bool ShenandoahAdaptiveHeuristics::is_diagnostic() { 171 return false; 172 } 173 174 bool ShenandoahAdaptiveHeuristics::is_experimental() { 175 return false; 176 }