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/shenandoah/heuristics/shenandoahTraversalHeuristics.hpp" 27 #include "gc/shenandoah/shenandoahFreeSet.hpp" 28 #include "gc/shenandoah/shenandoahHeuristics.hpp" 29 #include "gc/shenandoah/shenandoahTraversalGC.hpp" 30 #include "logging/log.hpp" 31 #include "logging/logTag.hpp" 32 #include "utilities/quickSort.hpp" 33 34 ShenandoahTraversalHeuristics::ShenandoahTraversalHeuristics() : ShenandoahHeuristics(), 35 _last_cset_select(0) 36 { 37 FLAG_SET_DEFAULT(ShenandoahSATBBarrier, false); 38 FLAG_SET_DEFAULT(ShenandoahStoreValReadBarrier, false); 39 FLAG_SET_DEFAULT(ShenandoahStoreValEnqueueBarrier, true); 40 FLAG_SET_DEFAULT(ShenandoahKeepAliveBarrier, false); 41 FLAG_SET_DEFAULT(ShenandoahAllowMixedAllocs, false); 42 43 SHENANDOAH_ERGO_OVERRIDE_DEFAULT(ShenandoahRefProcFrequency, 1); 44 45 // Adjust class unloading settings only if globally enabled. 46 if (ClassUnloadingWithConcurrentMark) { 47 SHENANDOAH_ERGO_OVERRIDE_DEFAULT(ShenandoahUnloadClassesFrequency, 1); 48 } 49 50 SHENANDOAH_ERGO_ENABLE_FLAG(ExplicitGCInvokesConcurrent); 51 SHENANDOAH_ERGO_ENABLE_FLAG(ShenandoahImplicitGCInvokesConcurrent); 52 53 // Final configuration checks 54 SHENANDOAH_CHECK_FLAG_SET(ShenandoahReadBarrier); 55 SHENANDOAH_CHECK_FLAG_SET(ShenandoahWriteBarrier); 56 SHENANDOAH_CHECK_FLAG_SET(ShenandoahStoreValEnqueueBarrier); 57 SHENANDOAH_CHECK_FLAG_SET(ShenandoahCASBarrier); 58 SHENANDOAH_CHECK_FLAG_SET(ShenandoahAcmpBarrier); 59 SHENANDOAH_CHECK_FLAG_SET(ShenandoahCloneBarrier); 60 } 61 62 bool ShenandoahTraversalHeuristics::should_start_normal_gc() const { 63 return false; 64 } 65 66 bool ShenandoahTraversalHeuristics::is_experimental() { 67 return true; 68 } 69 70 bool ShenandoahTraversalHeuristics::is_diagnostic() { 71 return false; 72 } 73 74 bool ShenandoahTraversalHeuristics::can_do_traversal_gc() { 75 return true; 76 } 77 78 const char* ShenandoahTraversalHeuristics::name() { 79 return "traversal"; 80 } 81 82 void ShenandoahTraversalHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) { 83 ShenandoahHeap* heap = ShenandoahHeap::heap(); 84 85 ShenandoahTraversalGC* traversal_gc = heap->traversal_gc(); 86 87 ShenandoahHeapRegionSet* traversal_set = traversal_gc->traversal_set(); 88 traversal_set->clear(); 89 90 RegionData *data = get_region_data_cache(heap->num_regions()); 91 size_t cnt = 0; 92 93 // Step 0. Prepare all regions 94 95 for (size_t i = 0; i < heap->num_regions(); i++) { 96 ShenandoahHeapRegion* r = heap->get_region(i); 97 if (r->used() > 0) { 98 if (r->is_regular()) { 99 data[cnt]._region = r; 100 data[cnt]._garbage = r->garbage(); 101 data[cnt]._seqnum_last_alloc = r->seqnum_last_alloc_mutator(); 102 cnt++; 103 } 104 traversal_set->add_region(r); 105 } 106 } 107 108 // The logic for cset selection is similar to that of adaptive: 109 // 110 // 1. We cannot get cset larger than available free space. Otherwise we guarantee OOME 111 // during evacuation, and thus guarantee full GC. In practice, we also want to let 112 // application to allocate something. This is why we limit CSet to some fraction of 113 // available space. In non-overloaded heap, max_cset would contain all plausible candidates 114 // over garbage threshold. 115 // 116 // 2. We should not get cset too low so that free threshold would not be met right 117 // after the cycle. Otherwise we get back-to-back cycles for no reason if heap is 118 // too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero. 119 // 120 // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates 121 // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before 122 // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme, 123 // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit. 124 // 125 // The significant complication is that liveness data was collected at the previous cycle, and only 126 // for those regions that were allocated before previous cycle started. 127 128 size_t capacity = heap->capacity(); 129 size_t actual_free = heap->free_set()->available(); 130 size_t free_target = ShenandoahMinFreeThreshold * capacity / 100; 131 size_t min_garbage = free_target > actual_free ? (free_target - actual_free) : 0; 132 size_t max_cset = (size_t)(1.0 * ShenandoahEvacReserve * capacity / 100 / ShenandoahEvacWaste); 133 134 log_info(gc, ergo)("Adaptive CSet Selection. Target Free: " SIZE_FORMAT "M, Actual Free: " 135 SIZE_FORMAT "M, Max CSet: " SIZE_FORMAT "M, Min Garbage: " SIZE_FORMAT "M", 136 free_target / M, actual_free / M, max_cset / M, min_garbage / M); 137 138 // Better select garbage-first regions, and then older ones 139 QuickSort::sort<RegionData>(data, (int) cnt, compare_by_garbage_then_alloc_seq_ascending, false); 140 141 size_t cur_cset = 0; 142 size_t cur_garbage = 0; 143 144 size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() / 100 * ShenandoahGarbageThreshold; 145 146 // Step 1. Add trustworthy regions to collection set. 147 // 148 // We can trust live/garbage data from regions that were fully traversed during 149 // previous cycle. Even if actual liveness is different now, we can only have _less_ 150 // live objects, because dead objects are not resurrected. Which means we can undershoot 151 // the collection set, but not overshoot it. 152 153 for (size_t i = 0; i < cnt; i++) { 154 if (data[i]._seqnum_last_alloc > _last_cset_select) continue; 155 156 ShenandoahHeapRegion* r = data[i]._region; 157 assert (r->is_regular(), "should have been filtered before"); 158 159 size_t new_garbage = cur_garbage + r->garbage(); 160 size_t new_cset = cur_cset + r->get_live_data_bytes(); 161 162 if (new_cset > max_cset) { 163 break; 164 } 165 166 if ((new_garbage < min_garbage) || (r->garbage() > garbage_threshold)) { 167 assert(!collection_set->is_in(r), "must not yet be in cset"); 168 collection_set->add_region(r); 169 cur_cset = new_cset; 170 cur_garbage = new_garbage; 171 } 172 } 173 174 // Step 2. Try to catch some recently allocated regions for evacuation ride. 175 // 176 // Pessimistically assume we are going to evacuate the entire region. While this 177 // is very pessimistic and in most cases undershoots the collection set when regions 178 // are mostly dead, it also provides more safety against running into allocation 179 // failure when newly allocated regions are fully live. 180 181 for (size_t i = 0; i < cnt; i++) { 182 if (data[i]._seqnum_last_alloc <= _last_cset_select) continue; 183 184 ShenandoahHeapRegion* r = data[i]._region; 185 assert (r->is_regular(), "should have been filtered before"); 186 187 // size_t new_garbage = cur_garbage + 0; (implied) 188 size_t new_cset = cur_cset + r->used(); 189 190 if (new_cset > max_cset) { 191 break; 192 } 193 194 assert(!collection_set->is_in(r), "must not yet be in cset"); 195 collection_set->add_region(r); 196 cur_cset = new_cset; 197 } 198 199 // Step 3. Clear liveness data 200 // TODO: Merge it with step 0, but save live data in RegionData before. 201 for (size_t i = 0; i < heap->num_regions(); i++) { 202 ShenandoahHeapRegion* r = heap->get_region(i); 203 if (r->used() > 0) { 204 r->clear_live_data(); 205 } 206 } 207 208 collection_set->update_region_status(); 209 210 _last_cset_select = ShenandoahHeapRegion::seqnum_current_alloc(); 211 } 212 213 bool ShenandoahTraversalHeuristics::should_start_traversal_gc() { 214 ShenandoahHeap* heap = ShenandoahHeap::heap(); 215 assert(!heap->has_forwarded_objects(), "no forwarded objects here"); 216 217 size_t capacity = heap->capacity(); 218 size_t available = heap->free_set()->available(); 219 220 // Check if we are falling below the worst limit, time to trigger the GC, regardless of 221 // anything else. 222 size_t min_threshold = ShenandoahMinFreeThreshold * heap->capacity() / 100; 223 if (available < min_threshold) { 224 log_info(gc)("Trigger: Free (" SIZE_FORMAT "M) is below minimum threshold (" SIZE_FORMAT "M)", 225 available / M, min_threshold / M); 226 return true; 227 } 228 229 // Check if are need to learn a bit about the application 230 const size_t max_learn = ShenandoahLearningSteps; 231 if (_gc_times_learned < max_learn) { 232 size_t init_threshold = ShenandoahInitFreeThreshold * heap->capacity() / 100; 233 if (available < init_threshold) { 234 log_info(gc)("Trigger: Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "M) is below initial threshold (" SIZE_FORMAT "M)", 235 _gc_times_learned + 1, max_learn, available / M, init_threshold / M); 236 return true; 237 } 238 } 239 240 // Check if allocation headroom is still okay. This also factors in: 241 // 1. Some space to absorb allocation spikes 242 // 2. Accumulated penalties from Degenerated and Full GC 243 244 size_t allocation_headroom = available; 245 246 size_t spike_headroom = ShenandoahAllocSpikeFactor * capacity / 100; 247 size_t penalties = _gc_time_penalties * capacity / 100; 248 249 allocation_headroom -= MIN2(allocation_headroom, spike_headroom); 250 allocation_headroom -= MIN2(allocation_headroom, penalties); 251 252 double average_gc = _gc_time_history->avg(); 253 double time_since_last = time_since_last_gc(); 254 double allocation_rate = heap->bytes_allocated_since_gc_start() / time_since_last; 255 256 if (average_gc > allocation_headroom / allocation_rate) { 257 log_info(gc)("Trigger: Average GC time (%.2f ms) is above the time for allocation rate (%.2f MB/s) to deplete free headroom (" SIZE_FORMAT "M)", 258 average_gc * 1000, allocation_rate / M, allocation_headroom / M); 259 log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "M (free) - " SIZE_FORMAT "M (spike) - " SIZE_FORMAT "M (penalties) = " SIZE_FORMAT "M", 260 available / M, spike_headroom / M, penalties / M, allocation_headroom / M); 261 return true; 262 } else if (ShenandoahHeuristics::should_start_normal_gc()) { 263 return true; 264 } 265 266 return false; 267 } 268 269 void ShenandoahTraversalHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* set, 270 RegionData* data, size_t data_size, 271 size_t free) { 272 ShouldNotReachHere(); 273 }