|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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/shared/collectorCounters.hpp" |
| 28 | +#include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp" |
| 29 | +#include "gc/shenandoah/shenandoahCollectorPolicy.hpp" |
| 30 | +#include "gc/shenandoah/shenandoahConcurrentMark.hpp" |
| 31 | +#include "gc/shenandoah/shenandoahDegeneratedGC.hpp" |
| 32 | +#include "gc/shenandoah/shenandoahHeap.inline.hpp" |
| 33 | +#include "gc/shenandoah/shenandoahMarkCompact.hpp" |
| 34 | +#include "gc/shenandoah/shenandoahMetrics.hpp" |
| 35 | +#include "gc/shenandoah/shenandoahMonitoringSupport.hpp" |
| 36 | +#include "gc/shenandoah/shenandoahOopClosures.inline.hpp" |
| 37 | +#include "gc/shenandoah/shenandoahRootProcessor.inline.hpp" |
| 38 | +#include "gc/shenandoah/shenandoahSTWMark.hpp" |
| 39 | +#include "gc/shenandoah/shenandoahUtils.hpp" |
| 40 | +#include "gc/shenandoah/shenandoahVerifier.hpp" |
| 41 | +#include "gc/shenandoah/shenandoahWorkerPolicy.hpp" |
| 42 | +#include "gc/shenandoah/shenandoahVMOperations.hpp" |
| 43 | +#include "runtime/vmThread.hpp" |
| 44 | +#include "utilities/events.hpp" |
| 45 | + |
| 46 | +ShenandoahDegenGC::ShenandoahDegenGC(ShenandoahDegenPoint degen_point) : |
| 47 | + ShenandoahGC(), |
| 48 | + _degen_point(degen_point) { |
| 49 | +} |
| 50 | + |
| 51 | +bool ShenandoahDegenGC::collect(GCCause::Cause cause) { |
| 52 | + vmop_degenerated(); |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +void ShenandoahDegenGC::vmop_degenerated() { |
| 57 | + TraceCollectorStats tcs(ShenandoahHeap::heap()->monitoring_support()->full_stw_collection_counters()); |
| 58 | + ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::degen_gc_gross); |
| 59 | + VM_ShenandoahDegeneratedGC degenerated_gc(this); |
| 60 | + VMThread::execute(°enerated_gc); |
| 61 | +} |
| 62 | + |
| 63 | +void ShenandoahDegenGC::entry_degenerated() { |
| 64 | + const char* msg = degen_event_message(_degen_point); |
| 65 | + ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::degen_gc, true /* log_heap_usage */); |
| 66 | + EventMark em("%s", msg); |
| 67 | + ShenandoahHeap* const heap = ShenandoahHeap::heap(); |
| 68 | + |
| 69 | + ShenandoahWorkerScope scope(heap->workers(), |
| 70 | + ShenandoahWorkerPolicy::calc_workers_for_stw_degenerated(), |
| 71 | + "stw degenerated gc"); |
| 72 | + |
| 73 | + heap->set_degenerated_gc_in_progress(true); |
| 74 | + op_degenerated(); |
| 75 | + heap->set_degenerated_gc_in_progress(false); |
| 76 | +} |
| 77 | + |
| 78 | +void ShenandoahDegenGC::op_degenerated() { |
| 79 | + ShenandoahHeap* const heap = ShenandoahHeap::heap(); |
| 80 | + // Degenerated GC is STW, but it can also fail. Current mechanics communicates |
| 81 | + // GC failure via cancelled_concgc() flag. So, if we detect the failure after |
| 82 | + // some phase, we have to upgrade the Degenerate GC to Full GC. |
| 83 | + heap->clear_cancelled_gc(); |
| 84 | + |
| 85 | + ShenandoahMetricsSnapshot metrics; |
| 86 | + metrics.snap_before(); |
| 87 | + |
| 88 | + switch (_degen_point) { |
| 89 | + // The cases below form the Duff's-like device: it describes the actual GC cycle, |
| 90 | + // but enters it at different points, depending on which concurrent phase had |
| 91 | + // degenerated. |
| 92 | + |
| 93 | + case _degenerated_outside_cycle: |
| 94 | + // We have degenerated from outside the cycle, which means something is bad with |
| 95 | + // the heap, most probably heavy humongous fragmentation, or we are very low on free |
| 96 | + // space. It makes little sense to wait for Full GC to reclaim as much as it can, when |
| 97 | + // we can do the most aggressive degen cycle, which includes processing references and |
| 98 | + // class unloading, unless those features are explicitly disabled. |
| 99 | + // |
| 100 | + |
| 101 | + // Degenerated from concurrent root mark, reset the flag for STW mark |
| 102 | + if (heap->is_concurrent_mark_in_progress()) { |
| 103 | + ShenandoahConcurrentMark::cancel(); |
| 104 | + heap->set_concurrent_mark_in_progress(false); |
| 105 | + } |
| 106 | + |
| 107 | + // Note that we can only do this for "outside-cycle" degens, otherwise we would risk |
| 108 | + // changing the cycle parameters mid-cycle during concurrent -> degenerated handover. |
| 109 | + heap->set_unload_classes(heap->heuristics()->can_unload_classes()); |
| 110 | + |
| 111 | + op_reset(); |
| 112 | + |
| 113 | + // STW mark |
| 114 | + op_mark(); |
| 115 | + |
| 116 | + case _degenerated_mark: |
| 117 | + // No fallthrough. Continue mark, handed over from concurrent mark |
| 118 | + if (_degen_point == ShenandoahDegenPoint::_degenerated_mark) { |
| 119 | + op_finish_mark(); |
| 120 | + } |
| 121 | + assert(!heap->cancelled_gc(), "STW mark can not OOM"); |
| 122 | + |
| 123 | + /* Degen select Collection Set. etc. */ |
| 124 | + op_prepare_evacuation(); |
| 125 | + |
| 126 | + op_cleanup_early(); |
| 127 | + |
| 128 | + case _degenerated_evac: |
| 129 | + // If heuristics thinks we should do the cycle, this flag would be set, |
| 130 | + // and we can do evacuation. Otherwise, it would be the shortcut cycle. |
| 131 | + if (heap->is_evacuation_in_progress()) { |
| 132 | + |
| 133 | + // Degeneration under oom-evac protocol might have left some objects in |
| 134 | + // collection set un-evacuated. Restart evacuation from the beginning to |
| 135 | + // capture all objects. For all the objects that are already evacuated, |
| 136 | + // it would be a simple check, which is supposed to be fast. This is also |
| 137 | + // safe to do even without degeneration, as CSet iterator is at beginning |
| 138 | + // in preparation for evacuation anyway. |
| 139 | + // |
| 140 | + // Before doing that, we need to make sure we never had any cset-pinned |
| 141 | + // regions. This may happen if allocation failure happened when evacuating |
| 142 | + // the about-to-be-pinned object, oom-evac protocol left the object in |
| 143 | + // the collection set, and then the pin reached the cset region. If we continue |
| 144 | + // the cycle here, we would trash the cset and alive objects in it. To avoid |
| 145 | + // it, we fail degeneration right away and slide into Full GC to recover. |
| 146 | + |
| 147 | + { |
| 148 | + heap->sync_pinned_region_status(); |
| 149 | + heap->collection_set()->clear_current_index(); |
| 150 | + |
| 151 | + ShenandoahHeapRegion* r; |
| 152 | + while ((r = heap->collection_set()->next()) != NULL) { |
| 153 | + if (r->is_pinned()) { |
| 154 | + heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc); |
| 155 | + op_degenerated_fail(); |
| 156 | + return; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + heap->collection_set()->clear_current_index(); |
| 161 | + } |
| 162 | + op_evacuate(); |
| 163 | + if (heap->cancelled_gc()) { |
| 164 | + op_degenerated_fail(); |
| 165 | + return; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // If heuristics thinks we should do the cycle, this flag would be set, |
| 170 | + // and we need to do update-refs. Otherwise, it would be the shortcut cycle. |
| 171 | + if (heap->has_forwarded_objects()) { |
| 172 | + op_init_updaterefs(); |
| 173 | + assert(!heap->cancelled_gc(), "STW reference update can not OOM"); |
| 174 | + } |
| 175 | + |
| 176 | + case _degenerated_updaterefs: |
| 177 | + if (heap->has_forwarded_objects()) { |
| 178 | + op_updaterefs(); |
| 179 | + op_update_roots(); |
| 180 | + assert(!heap->cancelled_gc(), "STW reference update can not OOM"); |
| 181 | + } |
| 182 | + |
| 183 | + if (ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) { |
| 184 | + // Disarm nmethods that armed in concurrent cycle. |
| 185 | + // In above case, update roots should disarm them |
| 186 | + ShenandoahCodeRoots::disarm_nmethods(); |
| 187 | + } |
| 188 | + |
| 189 | + op_cleanup_complete(); |
| 190 | + break; |
| 191 | + default: |
| 192 | + ShouldNotReachHere(); |
| 193 | + } |
| 194 | + |
| 195 | + if (ShenandoahVerify) { |
| 196 | + heap->verifier()->verify_after_degenerated(); |
| 197 | + } |
| 198 | + |
| 199 | + if (VerifyAfterGC) { |
| 200 | + Universe::verify(); |
| 201 | + } |
| 202 | + |
| 203 | + metrics.snap_after(); |
| 204 | + |
| 205 | + // Check for futility and fail. There is no reason to do several back-to-back Degenerated cycles, |
| 206 | + // because that probably means the heap is overloaded and/or fragmented. |
| 207 | + if (!metrics.is_good_progress()) { |
| 208 | + heap->notify_gc_no_progress(); |
| 209 | + heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc); |
| 210 | + op_degenerated_futile(); |
| 211 | + } else { |
| 212 | + heap->notify_gc_progress(); |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +void ShenandoahDegenGC::op_reset() { |
| 217 | + ShenandoahHeap::heap()->prepare_gc(); |
| 218 | +} |
| 219 | + |
| 220 | +void ShenandoahDegenGC::op_mark() { |
| 221 | + assert(!ShenandoahHeap::heap()->is_concurrent_mark_in_progress(), "Should be reset"); |
| 222 | + ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_mark); |
| 223 | + ShenandoahSTWMark mark(false /*full gc*/); |
| 224 | + mark.clear(); |
| 225 | + mark.mark(); |
| 226 | +} |
| 227 | + |
| 228 | +void ShenandoahDegenGC::op_finish_mark() { |
| 229 | + ShenandoahConcurrentMark mark; |
| 230 | + mark.finish_mark(); |
| 231 | +} |
| 232 | + |
| 233 | +void ShenandoahDegenGC::op_prepare_evacuation() { |
| 234 | + ShenandoahHeap* const heap = ShenandoahHeap::heap(); |
| 235 | + if (ShenandoahVerify) { |
| 236 | + heap->verifier()->verify_roots_no_forwarded(); |
| 237 | + } |
| 238 | + |
| 239 | + // STW cleanup weak roots and unload classes |
| 240 | + heap->parallel_cleaning(false /*full gc*/); |
| 241 | + // Prepare regions and collection set |
| 242 | + heap->prepare_regions_and_collection_set(false /*concurrent*/); |
| 243 | + |
| 244 | + if (!heap->collection_set()->is_empty()) { |
| 245 | + heap->set_evacuation_in_progress(true); |
| 246 | + heap->set_has_forwarded_objects(true); |
| 247 | + |
| 248 | + if(ShenandoahVerify) { |
| 249 | + heap->verifier()->verify_during_evacuation(); |
| 250 | + } |
| 251 | + } else { |
| 252 | + if (ShenandoahVerify) { |
| 253 | + heap->verifier()->verify_after_concmark(); |
| 254 | + } |
| 255 | + |
| 256 | + if (VerifyAfterGC) { |
| 257 | + Universe::verify(); |
| 258 | + } |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +void ShenandoahDegenGC::op_cleanup_early() { |
| 263 | + ShenandoahHeap::heap()->recycle_trash(); |
| 264 | +} |
| 265 | + |
| 266 | +void ShenandoahDegenGC::op_evacuate() { |
| 267 | + ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_evac); |
| 268 | + ShenandoahHeap::heap()->evacuate_collection_set(false /* concurrent*/); |
| 269 | +} |
| 270 | + |
| 271 | +void ShenandoahDegenGC::op_init_updaterefs() { |
| 272 | + // Evacuation has completed |
| 273 | + ShenandoahHeap* const heap = ShenandoahHeap::heap(); |
| 274 | + heap->set_evacuation_in_progress(false); |
| 275 | + heap->set_concurrent_weak_root_in_progress(false); |
| 276 | + heap->set_concurrent_strong_root_in_progress(false); |
| 277 | + |
| 278 | + heap->prepare_update_heap_references(false /*concurrent*/); |
| 279 | + heap->set_update_refs_in_progress(true); |
| 280 | +} |
| 281 | + |
| 282 | +void ShenandoahDegenGC::op_updaterefs() { |
| 283 | + ShenandoahHeap* const heap = ShenandoahHeap::heap(); |
| 284 | + ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_updaterefs); |
| 285 | + // Handed over from concurrent update references phase |
| 286 | + heap->update_heap_references(false /*concurrent*/); |
| 287 | + |
| 288 | + heap->set_update_refs_in_progress(false); |
| 289 | + heap->set_has_forwarded_objects(false); |
| 290 | +} |
| 291 | + |
| 292 | +void ShenandoahDegenGC::op_update_roots() { |
| 293 | + ShenandoahHeap* const heap = ShenandoahHeap::heap(); |
| 294 | + |
| 295 | + update_roots(false /*full_gc*/); |
| 296 | + |
| 297 | + heap->update_heap_region_states(false /*concurrent*/); |
| 298 | + |
| 299 | + if (ShenandoahVerify) { |
| 300 | + heap->verifier()->verify_after_updaterefs(); |
| 301 | + } |
| 302 | + |
| 303 | + if (VerifyAfterGC) { |
| 304 | + Universe::verify(); |
| 305 | + } |
| 306 | + |
| 307 | + heap->rebuild_free_set(false /*concurrent*/); |
| 308 | +} |
| 309 | + |
| 310 | +void ShenandoahDegenGC::op_cleanup_complete() { |
| 311 | + ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_cleanup_complete); |
| 312 | + ShenandoahHeap::heap()->recycle_trash(); |
| 313 | +} |
| 314 | + |
| 315 | +void ShenandoahDegenGC::op_degenerated_fail() { |
| 316 | + log_info(gc)("Cannot finish degeneration, upgrading to Full GC"); |
| 317 | + ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full(); |
| 318 | + |
| 319 | + ShenandoahMarkCompact full_gc; |
| 320 | + full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc); |
| 321 | +} |
| 322 | + |
| 323 | +void ShenandoahDegenGC::op_degenerated_futile() { |
| 324 | + ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full(); |
| 325 | + ShenandoahMarkCompact full_gc; |
| 326 | + full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc); |
| 327 | +} |
| 328 | + |
| 329 | +const char* ShenandoahDegenGC::degen_event_message(ShenandoahDegenPoint point) const { |
| 330 | + switch (point) { |
| 331 | + case _degenerated_unset: |
| 332 | + return "Pause Degenerated GC (<UNSET>)"; |
| 333 | + case _degenerated_outside_cycle: |
| 334 | + return "Pause Degenerated GC (Outside of Cycle)"; |
| 335 | + case _degenerated_mark: |
| 336 | + return "Pause Degenerated GC (Mark)"; |
| 337 | + case _degenerated_evac: |
| 338 | + return "Pause Degenerated GC (Evacuation)"; |
| 339 | + case _degenerated_updaterefs: |
| 340 | + return "Pause Degenerated GC (Update Refs)"; |
| 341 | + default: |
| 342 | + ShouldNotReachHere(); |
| 343 | + return "ERROR"; |
| 344 | + } |
| 345 | +} |
0 commit comments