1 /* 2 * Copyright (c) 2021, Red Hat, Inc. All rights reserved. 3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 28 #include "gc/shared/collectorCounters.hpp" 29 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" 30 #include "gc/shenandoah/shenandoahConcurrentMark.hpp" 31 #include "gc/shenandoah/shenandoahDegeneratedGC.hpp" 32 #include "gc/shenandoah/shenandoahFullGC.hpp" 33 #include "gc/shenandoah/shenandoahGeneration.hpp" 34 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp" 35 #include "gc/shenandoah/shenandoahHeap.inline.hpp" 36 #include "gc/shenandoah/shenandoahMetrics.hpp" 37 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp" 38 #include "gc/shenandoah/shenandoahOldGeneration.hpp" 39 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp" 40 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp" 41 #include "gc/shenandoah/shenandoahSTWMark.hpp" 42 #include "gc/shenandoah/shenandoahUtils.hpp" 43 #include "gc/shenandoah/shenandoahVerifier.hpp" 44 #include "gc/shenandoah/shenandoahYoungGeneration.hpp" 45 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp" 46 #include "gc/shenandoah/shenandoahVMOperations.hpp" 47 #include "runtime/vmThread.hpp" 48 #include "utilities/events.hpp" 49 50 ShenandoahDegenGC::ShenandoahDegenGC(ShenandoahDegenPoint degen_point, ShenandoahGeneration* generation) : 51 ShenandoahGC(), 52 _degen_point(degen_point), 53 _generation(generation), 54 _abbreviated(false) { 55 } 56 57 bool ShenandoahDegenGC::collect(GCCause::Cause cause) { 58 vmop_degenerated(); 59 ShenandoahHeap* heap = ShenandoahHeap::heap(); 60 if (heap->mode()->is_generational()) { 61 bool is_bootstrap_gc = heap->old_generation()->is_bootstrapping(); 62 heap->mmu_tracker()->record_degenerated(GCId::current(), is_bootstrap_gc); 63 const char* msg = is_bootstrap_gc? "At end of Degenerated Bootstrap Old GC": "At end of Degenerated Young GC"; 64 heap->log_heap_status(msg); 65 } 66 return true; 67 } 68 69 void ShenandoahDegenGC::vmop_degenerated() { 70 TraceCollectorStats tcs(ShenandoahHeap::heap()->monitoring_support()->full_stw_collection_counters()); 71 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::degen_gc_gross); 72 VM_ShenandoahDegeneratedGC degenerated_gc(this); 73 VMThread::execute(°enerated_gc); 74 } 75 76 void ShenandoahDegenGC::entry_degenerated() { 77 const char* msg = degen_event_message(_degen_point); 78 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::degen_gc, true /* log_heap_usage */); 79 EventMark em("%s", msg); 80 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 81 ShenandoahWorkerScope scope(heap->workers(), 82 ShenandoahWorkerPolicy::calc_workers_for_stw_degenerated(), 83 "stw degenerated gc"); 84 85 heap->set_degenerated_gc_in_progress(true); 86 op_degenerated(); 87 heap->set_degenerated_gc_in_progress(false); 88 } 89 90 void ShenandoahDegenGC::op_degenerated() { 91 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 92 // Degenerated GC is STW, but it can also fail. Current mechanics communicates 93 // GC failure via cancelled_concgc() flag. So, if we detect the failure after 94 // some phase, we have to upgrade the Degenerate GC to Full GC. 95 heap->clear_cancelled_gc(true /* clear oom handler */); 96 97 #ifdef ASSERT 98 if (heap->mode()->is_generational()) { 99 ShenandoahOldGeneration* old_generation = heap->old_generation(); 100 if (!heap->is_concurrent_old_mark_in_progress()) { 101 // If we are not marking the old generation, there should be nothing in the old mark queues 102 assert(old_generation->task_queues()->is_empty(), "Old gen task queues should be empty"); 103 } 104 105 if (_generation->is_global()) { 106 // If we are in a global cycle, the old generation should not be marking. It is, however, 107 // allowed to be holding regions for evacuation or coalescing. 108 assert(old_generation->is_idle() 109 || old_generation->is_doing_mixed_evacuations() 110 || old_generation->is_preparing_for_mark(), 111 "Old generation cannot be in state: %s", old_generation->state_name()); 112 } 113 } 114 #endif 115 116 ShenandoahMetricsSnapshot metrics; 117 metrics.snap_before(); 118 119 switch (_degen_point) { 120 // The cases below form the Duff's-like device: it describes the actual GC cycle, 121 // but enters it at different points, depending on which concurrent phase had 122 // degenerated. 123 124 case _degenerated_outside_cycle: 125 // We have degenerated from outside the cycle, which means something is bad with 126 // the heap, most probably heavy humongous fragmentation, or we are very low on free 127 // space. It makes little sense to wait for Full GC to reclaim as much as it can, when 128 // we can do the most aggressive degen cycle, which includes processing references and 129 // class unloading, unless those features are explicitly disabled. 130 131 // Note that we can only do this for "outside-cycle" degens, otherwise we would risk 132 // changing the cycle parameters mid-cycle during concurrent -> degenerated handover. 133 heap->set_unload_classes(_generation->heuristics()->can_unload_classes() && 134 (!heap->mode()->is_generational() || _generation->is_global())); 135 136 if (heap->mode()->is_generational() && _generation->is_young()) { 137 // Swap remembered sets for young 138 _generation->swap_remembered_set(); 139 } 140 141 case _degenerated_roots: 142 // Degenerated from concurrent root mark, reset the flag for STW mark 143 if (!heap->mode()->is_generational()) { 144 if (heap->is_concurrent_mark_in_progress()) { 145 heap->cancel_concurrent_mark(); 146 } 147 } else { 148 if (_generation->is_concurrent_mark_in_progress()) { 149 // We want to allow old generation marking to be punctuated by young collections 150 // (even if they have degenerated). If this is a global cycle, we'd have cancelled 151 // the entire old gc before coming into this switch. Note that cancel_marking on 152 // the generation does NOT abandon incomplete SATB buffers as cancel_concurrent_mark does. 153 // We need to separate out the old pointers which is done below. 154 _generation->cancel_marking(); 155 } 156 157 if (heap->is_concurrent_mark_in_progress()) { 158 // If either old or young marking is in progress, the SATB barrier will be enabled. 159 // The SATB buffer may hold a mix of old and young pointers. The old pointers need to be 160 // transferred to the old generation mark queues and the young pointers are NOT part 161 // of this snapshot, so they must be dropped here. It is safe to drop them here because 162 // we will rescan the roots on this safepoint. 163 heap->old_generation()->transfer_pointers_from_satb(); 164 } 165 166 if (_degen_point == ShenandoahDegenPoint::_degenerated_roots) { 167 // We only need this if the concurrent cycle has already swapped the card tables. 168 // Marking will use the 'read' table, but interesting pointers may have been 169 // recorded in the 'write' table in the time between the cancelled concurrent cycle 170 // and this degenerated cycle. These pointers need to be included the 'read' table 171 // used to scan the remembered set during the STW mark which follows here. 172 _generation->merge_write_table(); 173 } 174 } 175 176 op_reset(); 177 178 // STW mark 179 op_mark(); 180 181 case _degenerated_mark: 182 // No fallthrough. Continue mark, handed over from concurrent mark if 183 // concurrent mark has yet completed 184 if (_degen_point == ShenandoahDegenPoint::_degenerated_mark && 185 heap->is_concurrent_mark_in_progress()) { 186 op_finish_mark(); 187 } 188 assert(!heap->cancelled_gc(), "STW mark can not OOM"); 189 190 /* Degen select Collection Set. etc. */ 191 op_prepare_evacuation(); 192 193 op_cleanup_early(); 194 195 case _degenerated_evac: 196 // If heuristics thinks we should do the cycle, this flag would be set, 197 // and we can do evacuation. Otherwise, it would be the shortcut cycle. 198 if (heap->is_evacuation_in_progress()) { 199 200 if (_degen_point == _degenerated_evac) { 201 // Degeneration under oom-evac protocol allows the mutator LRB to expose 202 // references to from-space objects. This is okay, in theory, because we 203 // will come to the safepoint here to complete the evacuations and update 204 // the references. However, if the from-space reference is written to a 205 // region that was EC during final mark or was recycled after final mark 206 // it will not have TAMS or UWM updated. Such a region is effectively 207 // skipped during update references which can lead to crashes and corruption 208 // if the from-space reference is accessed. 209 if (UseTLAB) { 210 heap->labs_make_parsable(); 211 } 212 213 for (size_t i = 0; i < heap->num_regions(); i++) { 214 ShenandoahHeapRegion* r = heap->get_region(i); 215 if (r->is_active() && r->top() > r->get_update_watermark()) { 216 r->set_update_watermark_at_safepoint(r->top()); 217 } 218 } 219 } 220 221 // Degeneration under oom-evac protocol might have left some objects in 222 // collection set un-evacuated. Restart evacuation from the beginning to 223 // capture all objects. For all the objects that are already evacuated, 224 // it would be a simple check, which is supposed to be fast. This is also 225 // safe to do even without degeneration, as CSet iterator is at beginning 226 // in preparation for evacuation anyway. 227 // 228 // Before doing that, we need to make sure we never had any cset-pinned 229 // regions. This may happen if allocation failure happened when evacuating 230 // the about-to-be-pinned object, oom-evac protocol left the object in 231 // the collection set, and then the pin reached the cset region. If we continue 232 // the cycle here, we would trash the cset and alive objects in it. To avoid 233 // it, we fail degeneration right away and slide into Full GC to recover. 234 235 { 236 heap->sync_pinned_region_status(); 237 heap->collection_set()->clear_current_index(); 238 ShenandoahHeapRegion* r; 239 while ((r = heap->collection_set()->next()) != nullptr) { 240 if (r->is_pinned()) { 241 heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc); 242 op_degenerated_fail(); 243 return; 244 } 245 } 246 247 heap->collection_set()->clear_current_index(); 248 } 249 op_evacuate(); 250 if (heap->cancelled_gc()) { 251 op_degenerated_fail(); 252 return; 253 } 254 } else if (has_in_place_promotions(heap)) { 255 // We have nothing to evacuate, but there are still regions to promote in place. 256 ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_promote_regions); 257 ShenandoahGenerationalHeap::heap()->promote_regions_in_place(false /* concurrent*/); 258 } 259 260 // Update collector state regardless of whether there are forwarded objects 261 heap->set_evacuation_in_progress(false); 262 heap->set_concurrent_weak_root_in_progress(false); 263 heap->set_concurrent_strong_root_in_progress(false); 264 265 // If heuristics thinks we should do the cycle, this flag would be set, 266 // and we need to do update-refs. Otherwise, it would be the shortcut cycle. 267 if (heap->has_forwarded_objects()) { 268 op_init_updaterefs(); 269 assert(!heap->cancelled_gc(), "STW reference update can not OOM"); 270 } else { 271 _abbreviated = true; 272 } 273 274 case _degenerated_updaterefs: 275 if (heap->has_forwarded_objects()) { 276 op_updaterefs(); 277 op_update_roots(); 278 assert(!heap->cancelled_gc(), "STW reference update can not OOM"); 279 } 280 281 // Disarm nmethods that armed in concurrent cycle. 282 // In above case, update roots should disarm them 283 ShenandoahCodeRoots::disarm_nmethods(); 284 285 op_cleanup_complete(); 286 287 if (heap->mode()->is_generational()) { 288 ShenandoahGenerationalHeap::heap()->complete_degenerated_cycle(); 289 } 290 291 break; 292 default: 293 ShouldNotReachHere(); 294 } 295 296 if (ShenandoahVerify) { 297 heap->verifier()->verify_after_degenerated(); 298 } 299 300 if (VerifyAfterGC) { 301 Universe::verify(); 302 } 303 304 metrics.snap_after(); 305 306 // Check for futility and fail. There is no reason to do several back-to-back Degenerated cycles, 307 // because that probably means the heap is overloaded and/or fragmented. 308 if (!metrics.is_good_progress()) { 309 heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc); 310 op_degenerated_futile(); 311 } else { 312 heap->notify_gc_progress(); 313 heap->shenandoah_policy()->record_success_degenerated(_generation->is_young(), _abbreviated); 314 _generation->heuristics()->record_success_degenerated(); 315 } 316 } 317 318 void ShenandoahDegenGC::op_reset() { 319 _generation->prepare_gc(); 320 } 321 322 void ShenandoahDegenGC::op_mark() { 323 assert(!_generation->is_concurrent_mark_in_progress(), "Should be reset"); 324 ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_mark); 325 ShenandoahSTWMark mark(_generation, false /*full gc*/); 326 mark.mark(); 327 } 328 329 void ShenandoahDegenGC::op_finish_mark() { 330 ShenandoahConcurrentMark mark(_generation); 331 mark.finish_mark(); 332 } 333 334 void ShenandoahDegenGC::op_prepare_evacuation() { 335 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 336 if (ShenandoahVerify) { 337 heap->verifier()->verify_roots_no_forwarded(); 338 } 339 340 // STW cleanup weak roots and unload classes 341 heap->parallel_cleaning(false /*full gc*/); 342 343 // Prepare regions and collection set 344 _generation->prepare_regions_and_collection_set(false /*concurrent*/); 345 346 // Retire the TLABs, which will force threads to reacquire their TLABs after the pause. 347 // This is needed for two reasons. Strong one: new allocations would be with new freeset, 348 // which would be outside the collection set, so no cset writes would happen there. 349 // Weaker one: new allocations would happen past update watermark, and so less work would 350 // be needed for reference updates (would update the large filler instead). 351 if (UseTLAB) { 352 ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_final_manage_labs); 353 heap->tlabs_retire(false); 354 } 355 356 if (!heap->collection_set()->is_empty()) { 357 if (ShenandoahVerify) { 358 heap->verifier()->verify_before_evacuation(); 359 } 360 361 heap->set_evacuation_in_progress(true); 362 363 heap->set_has_forwarded_objects(true); 364 } else { 365 if (ShenandoahVerify) { 366 if (has_in_place_promotions(heap)) { 367 heap->verifier()->verify_after_concmark_with_promotions(); 368 } else { 369 heap->verifier()->verify_after_concmark(); 370 } 371 } 372 373 if (VerifyAfterGC) { 374 Universe::verify(); 375 } 376 } 377 } 378 379 bool ShenandoahDegenGC::has_in_place_promotions(const ShenandoahHeap* heap) const { 380 return heap->mode()->is_generational() && heap->old_generation()->has_in_place_promotions(); 381 } 382 383 void ShenandoahDegenGC::op_cleanup_early() { 384 ShenandoahHeap::heap()->recycle_trash(); 385 } 386 387 void ShenandoahDegenGC::op_evacuate() { 388 ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_evac); 389 ShenandoahHeap::heap()->evacuate_collection_set(false /* concurrent*/); 390 } 391 392 void ShenandoahDegenGC::op_init_updaterefs() { 393 // Evacuation has completed 394 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 395 heap->prepare_update_heap_references(false /*concurrent*/); 396 heap->set_update_refs_in_progress(true); 397 } 398 399 void ShenandoahDegenGC::op_updaterefs() { 400 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 401 ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_updaterefs); 402 // Handed over from concurrent update references phase 403 heap->update_heap_references(false /*concurrent*/); 404 405 heap->set_update_refs_in_progress(false); 406 heap->set_has_forwarded_objects(false); 407 } 408 409 void ShenandoahDegenGC::op_update_roots() { 410 ShenandoahHeap* const heap = ShenandoahHeap::heap(); 411 412 update_roots(false /*full_gc*/); 413 414 heap->update_heap_region_states(false /*concurrent*/); 415 416 if (ShenandoahVerify) { 417 heap->verifier()->verify_after_updaterefs(); 418 } 419 420 if (VerifyAfterGC) { 421 Universe::verify(); 422 } 423 424 heap->rebuild_free_set(false /*concurrent*/); 425 } 426 427 void ShenandoahDegenGC::op_cleanup_complete() { 428 ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_cleanup_complete); 429 ShenandoahHeap::heap()->recycle_trash(); 430 } 431 432 void ShenandoahDegenGC::op_degenerated_fail() { 433 upgrade_to_full(); 434 } 435 436 void ShenandoahDegenGC::op_degenerated_futile() { 437 upgrade_to_full(); 438 } 439 440 const char* ShenandoahDegenGC::degen_event_message(ShenandoahDegenPoint point) const { 441 switch (point) { 442 case _degenerated_unset: 443 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (<UNSET>)"); 444 case _degenerated_outside_cycle: 445 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Outside of Cycle)"); 446 case _degenerated_roots: 447 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Roots)"); 448 case _degenerated_mark: 449 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Mark)"); 450 case _degenerated_evac: 451 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Evacuation)"); 452 case _degenerated_updaterefs: 453 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Update Refs)"); 454 default: 455 ShouldNotReachHere(); 456 SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (?)"); 457 } 458 } 459 460 void ShenandoahDegenGC::upgrade_to_full() { 461 log_info(gc)("Degenerated GC upgrading to Full GC"); 462 ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full(); 463 ShenandoahFullGC full_gc; 464 full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc); 465 }