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   _consecutive_degen_with_bad_progress(0) {
 56 }
 57 
 58 bool ShenandoahDegenGC::collect(GCCause::Cause cause) {
 59   vmop_degenerated();
 60   ShenandoahHeap* heap = ShenandoahHeap::heap();
 61   if (heap->mode()->is_generational()) {
 62     bool is_bootstrap_gc = heap->old_generation()->is_bootstrapping();
 63     heap->mmu_tracker()->record_degenerated(GCId::current(), is_bootstrap_gc);
 64     const char* msg = is_bootstrap_gc? "At end of Degenerated Bootstrap Old GC": "At end of Degenerated Young GC";
 65     heap->log_heap_status(msg);
 66   }
 67   return true;
 68 }
 69 
 70 void ShenandoahDegenGC::vmop_degenerated() {
 71   TraceCollectorStats tcs(ShenandoahHeap::heap()->monitoring_support()->full_stw_collection_counters());
 72   ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::degen_gc_gross);
 73   VM_ShenandoahDegeneratedGC degenerated_gc(this);
 74   VMThread::execute(&degenerated_gc);
 75 }
 76 
 77 void ShenandoahDegenGC::entry_degenerated() {
 78   const char* msg = degen_event_message(_degen_point);
 79   ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::degen_gc, true /* log_heap_usage */);
 80   EventMark em("%s", msg);
 81   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 82   ShenandoahWorkerScope scope(heap->workers(),
 83                               ShenandoahWorkerPolicy::calc_workers_for_stw_degenerated(),
 84                               "stw degenerated gc");
 85 
 86   heap->set_degenerated_gc_in_progress(true);
 87   op_degenerated();
 88   heap->set_degenerated_gc_in_progress(false);
 89   {
 90     ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::degen_gc_propagate_gc_state);
 91     heap->propagate_gc_state_to_all_threads();
 92   }
 93 }
 94 
 95 void ShenandoahDegenGC::op_degenerated() {
 96   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 97   // Degenerated GC is STW, but it can also fail. Current mechanics communicates
 98   // GC failure via cancelled_concgc() flag. So, if we detect the failure after
 99   // some phase, we have to upgrade the Degenerate GC to Full GC.
100   heap->clear_cancelled_gc(true /* clear oom handler */);
101 
102 #ifdef ASSERT
103   if (heap->mode()->is_generational()) {
104     ShenandoahOldGeneration* old_generation = heap->old_generation();
105     if (!heap->is_concurrent_old_mark_in_progress()) {
106       // If we are not marking the old generation, there should be nothing in the old mark queues
107       assert(old_generation->task_queues()->is_empty(), "Old gen task queues should be empty");
108     }
109 
110     if (_generation->is_global()) {
111       // If we are in a global cycle, the old generation should not be marking. It is, however,
112       // allowed to be holding regions for evacuation or coalescing.
113       assert(old_generation->is_idle()
114              || old_generation->is_doing_mixed_evacuations()
115              || old_generation->is_preparing_for_mark(),
116              "Old generation cannot be in state: %s", old_generation->state_name());
117     }
118   }
119 #endif
120 
121   ShenandoahMetricsSnapshot metrics;
122   metrics.snap_before();
123 
124   switch (_degen_point) {
125     // The cases below form the Duff's-like device: it describes the actual GC cycle,
126     // but enters it at different points, depending on which concurrent phase had
127     // degenerated.
128 
129     case _degenerated_outside_cycle:
130       // We have degenerated from outside the cycle, which means something is bad with
131       // the heap, most probably heavy humongous fragmentation, or we are very low on free
132       // space. It makes little sense to wait for Full GC to reclaim as much as it can, when
133       // we can do the most aggressive degen cycle, which includes processing references and
134       // class unloading, unless those features are explicitly disabled.
135 
136       // Note that we can only do this for "outside-cycle" degens, otherwise we would risk
137       // changing the cycle parameters mid-cycle during concurrent -> degenerated handover.
138       heap->set_unload_classes(_generation->heuristics()->can_unload_classes() &&
139                                 (!heap->mode()->is_generational() || _generation->is_global()));
140 
141       if (heap->mode()->is_generational() && _generation->is_young()) {
142         // Swap remembered sets for young
143         _generation->swap_remembered_set();
144       }
145 
146     case _degenerated_roots:
147       // Degenerated from concurrent root mark, reset the flag for STW mark
148       if (!heap->mode()->is_generational()) {
149         if (heap->is_concurrent_mark_in_progress()) {
150           heap->cancel_concurrent_mark();
151         }
152       } else {
153         if (_generation->is_concurrent_mark_in_progress()) {
154           // We want to allow old generation marking to be punctuated by young collections
155           // (even if they have degenerated). If this is a global cycle, we'd have cancelled
156           // the entire old gc before coming into this switch. Note that cancel_marking on
157           // the generation does NOT abandon incomplete SATB buffers as cancel_concurrent_mark does.
158           // We need to separate out the old pointers which is done below.
159           _generation->cancel_marking();
160         }
161 
162         if (heap->is_concurrent_mark_in_progress()) {
163           // If either old or young marking is in progress, the SATB barrier will be enabled.
164           // The SATB buffer may hold a mix of old and young pointers. The old pointers need to be
165           // transferred to the old generation mark queues and the young pointers are NOT part
166           // of this snapshot, so they must be dropped here. It is safe to drop them here because
167           // we will rescan the roots on this safepoint.
168           heap->old_generation()->transfer_pointers_from_satb();
169         }
170 
171         if (_degen_point == ShenandoahDegenPoint::_degenerated_roots) {
172           // We only need this if the concurrent cycle has already swapped the card tables.
173           // Marking will use the 'read' table, but interesting pointers may have been
174           // recorded in the 'write' table in the time between the cancelled concurrent cycle
175           // and this degenerated cycle. These pointers need to be included the 'read' table
176           // used to scan the remembered set during the STW mark which follows here.
177           _generation->merge_write_table();
178         }
179       }
180 
181       op_reset();
182 
183       // STW mark
184       op_mark();
185 
186     case _degenerated_mark:
187       // No fallthrough. Continue mark, handed over from concurrent mark if
188       // concurrent mark has yet completed
189       if (_degen_point == ShenandoahDegenPoint::_degenerated_mark &&
190           heap->is_concurrent_mark_in_progress()) {
191         op_finish_mark();
192       }
193       assert(!heap->cancelled_gc(), "STW mark can not OOM");
194 
195       /* Degen select Collection Set. etc. */
196       op_prepare_evacuation();
197 
198       op_cleanup_early();
199 
200     case _degenerated_evac:
201       // If heuristics thinks we should do the cycle, this flag would be set,
202       // and we can do evacuation. Otherwise, it would be the shortcut cycle.
203       if (heap->is_evacuation_in_progress()) {
204 
205         if (_degen_point == _degenerated_evac) {
206           // Degeneration under oom-evac protocol allows the mutator LRB to expose
207           // references to from-space objects. This is okay, in theory, because we
208           // will come to the safepoint here to complete the evacuations and update
209           // the references. However, if the from-space reference is written to a
210           // region that was EC during final mark or was recycled after final mark
211           // it will not have TAMS or UWM updated. Such a region is effectively
212           // skipped during update references which can lead to crashes and corruption
213           // if the from-space reference is accessed.
214           if (UseTLAB) {
215             heap->labs_make_parsable();
216           }
217 
218           for (size_t i = 0; i < heap->num_regions(); i++) {
219             ShenandoahHeapRegion* r = heap->get_region(i);
220             if (r->is_active() && r->top() > r->get_update_watermark()) {
221               r->set_update_watermark_at_safepoint(r->top());
222             }
223           }
224         }
225 
226         // Degeneration under oom-evac protocol might have left some objects in
227         // collection set un-evacuated. Restart evacuation from the beginning to
228         // capture all objects. For all the objects that are already evacuated,
229         // it would be a simple check, which is supposed to be fast. This is also
230         // safe to do even without degeneration, as CSet iterator is at beginning
231         // in preparation for evacuation anyway.
232         //
233         // Before doing that, we need to make sure we never had any cset-pinned
234         // regions. This may happen if allocation failure happened when evacuating
235         // the about-to-be-pinned object, oom-evac protocol left the object in
236         // the collection set, and then the pin reached the cset region. If we continue
237         // the cycle here, we would trash the cset and alive objects in it. To avoid
238         // it, we fail degeneration right away and slide into Full GC to recover.
239 
240         {
241           heap->sync_pinned_region_status();
242           heap->collection_set()->clear_current_index();
243           ShenandoahHeapRegion* r;
244           while ((r = heap->collection_set()->next()) != nullptr) {
245             if (r->is_pinned()) {
246               heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);
247               op_degenerated_fail();
248               return;
249             }
250           }
251 
252           heap->collection_set()->clear_current_index();
253         }
254         op_evacuate();
255         if (heap->cancelled_gc()) {
256           op_degenerated_fail();
257           return;
258         }
259       } else if (has_in_place_promotions(heap)) {
260         // We have nothing to evacuate, but there are still regions to promote in place.
261         ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_promote_regions);
262         ShenandoahGenerationalHeap::heap()->promote_regions_in_place(false /* concurrent*/);
263       }
264 
265       // Update collector state regardless of whether there are forwarded objects
266       heap->set_evacuation_in_progress(false);
267       heap->set_concurrent_weak_root_in_progress(false);
268       heap->set_concurrent_strong_root_in_progress(false);
269 
270       // If heuristics thinks we should do the cycle, this flag would be set,
271       // and we need to do update-refs. Otherwise, it would be the shortcut cycle.
272       if (heap->has_forwarded_objects()) {
273         op_init_update_refs();
274         assert(!heap->cancelled_gc(), "STW reference update can not OOM");
275       } else {
276         _abbreviated = true;
277       }
278 
279     case _degenerated_update_refs:
280       if (heap->has_forwarded_objects()) {
281         op_update_refs();
282         op_update_roots();
283         assert(!heap->cancelled_gc(), "STW reference update can not OOM");
284       }
285 
286       // Disarm nmethods that armed in concurrent cycle.
287       // In above case, update roots should disarm them
288       ShenandoahCodeRoots::disarm_nmethods();
289 
290       op_cleanup_complete();
291 
292       if (heap->mode()->is_generational()) {
293         ShenandoahGenerationalHeap::heap()->complete_degenerated_cycle();
294       }
295 
296       break;
297     default:
298       ShouldNotReachHere();
299   }
300 
301   if (ShenandoahVerify) {
302     heap->verifier()->verify_after_degenerated();
303   }
304 
305   if (VerifyAfterGC) {
306     Universe::verify();
307   }
308 
309   metrics.snap_after();
310 
311   // The most common scenario for lack of good progress following a degenerated GC is an accumulation of floating
312   // garbage during the most recently aborted concurrent GC effort.  With generational GC, it is far more effective to
313   // reclaim this floating garbage with another degenerated cycle (which focuses on young generation and might require
314   // a pause of 200 ms) rather than a full GC cycle (which may require over 2 seconds with a 10 GB old generation).
315   //
316   // In generational mode, we'll only upgrade to full GC if we've done two degen cycles in a row and both indicated
317   // bad progress.  In non-generational mode, we'll preserve the original behavior, which is to upgrade to full
318   // immediately following a degenerated cycle with bad progress.  This preserves original behavior of non-generational
319   // Shenandoah so as to avoid introducing "surprising new behavior."  It also makes less sense with non-generational
320   // Shenandoah to replace a full GC with a degenerated GC, because both have similar pause times in non-generational
321   // mode.
322   if (!metrics.is_good_progress(_generation)) {
323     _consecutive_degen_with_bad_progress++;
324   } else {
325     _consecutive_degen_with_bad_progress = 0;
326   }
327   if (!heap->mode()->is_generational() ||
328       ((heap->shenandoah_policy()->consecutive_degenerated_gc_count() > 1) && (_consecutive_degen_with_bad_progress >= 2))) {
329     heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);
330     op_degenerated_futile();
331   } else {
332     heap->notify_gc_progress();
333     heap->shenandoah_policy()->record_success_degenerated(_generation->is_young(), _abbreviated);
334     _generation->heuristics()->record_success_degenerated();
335   }
336 }
337 
338 void ShenandoahDegenGC::op_reset() {
339   _generation->prepare_gc();
340 }
341 
342 void ShenandoahDegenGC::op_mark() {
343   assert(!_generation->is_concurrent_mark_in_progress(), "Should be reset");
344   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_mark);
345   ShenandoahSTWMark mark(_generation, false /*full gc*/);
346   mark.mark();
347 }
348 
349 void ShenandoahDegenGC::op_finish_mark() {
350   ShenandoahConcurrentMark mark(_generation);
351   mark.finish_mark();
352 }
353 
354 void ShenandoahDegenGC::op_prepare_evacuation() {
355   ShenandoahHeap* const heap = ShenandoahHeap::heap();
356   if (ShenandoahVerify) {
357     heap->verifier()->verify_roots_no_forwarded();
358   }
359 
360   // STW cleanup weak roots and unload classes
361   heap->parallel_cleaning(false /*full gc*/);
362 
363   // Prepare regions and collection set
364   _generation->prepare_regions_and_collection_set(false /*concurrent*/);
365 
366   // Retire the TLABs, which will force threads to reacquire their TLABs after the pause.
367   // This is needed for two reasons. Strong one: new allocations would be with new freeset,
368   // which would be outside the collection set, so no cset writes would happen there.
369   // Weaker one: new allocations would happen past update watermark, and so less work would
370   // be needed for reference updates (would update the large filler instead).
371   if (UseTLAB) {
372     ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_final_manage_labs);
373     heap->tlabs_retire(false);
374   }
375 
376   if (!heap->collection_set()->is_empty()) {
377     if (ShenandoahVerify) {
378       heap->verifier()->verify_before_evacuation();
379     }
380 
381     heap->set_evacuation_in_progress(true);
382 
383     heap->set_has_forwarded_objects(true);
384   } else {
385     if (ShenandoahVerify) {
386       if (has_in_place_promotions(heap)) {
387         heap->verifier()->verify_after_concmark_with_promotions();
388       } else {
389         heap->verifier()->verify_after_concmark();
390       }
391     }
392 
393     if (VerifyAfterGC) {
394       Universe::verify();
395     }
396   }
397 }
398 
399 bool ShenandoahDegenGC::has_in_place_promotions(const ShenandoahHeap* heap) const {
400   return heap->mode()->is_generational() && heap->old_generation()->has_in_place_promotions();
401 }
402 
403 void ShenandoahDegenGC::op_cleanup_early() {
404   ShenandoahHeap::heap()->recycle_trash();
405 }
406 
407 void ShenandoahDegenGC::op_evacuate() {
408   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_evac);
409   ShenandoahHeap::heap()->evacuate_collection_set(false /* concurrent*/);
410 }
411 
412 void ShenandoahDegenGC::op_init_update_refs() {
413   // Evacuation has completed
414   ShenandoahHeap* const heap = ShenandoahHeap::heap();
415   heap->prepare_update_heap_references();
416   heap->set_update_refs_in_progress(true);
417 }
418 
419 void ShenandoahDegenGC::op_update_refs() {
420   ShenandoahHeap* const heap = ShenandoahHeap::heap();
421   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_update_refs);
422   // Handed over from concurrent update references phase
423   heap->update_heap_references(false /*concurrent*/);
424 
425   heap->set_update_refs_in_progress(false);
426   heap->set_has_forwarded_objects(false);
427 }
428 
429 void ShenandoahDegenGC::op_update_roots() {
430   ShenandoahHeap* const heap = ShenandoahHeap::heap();
431 
432   update_roots(false /*full_gc*/);
433 
434   heap->update_heap_region_states(false /*concurrent*/);
435 
436   if (ShenandoahVerify) {
437     heap->verifier()->verify_after_update_refs();
438   }
439 
440   if (VerifyAfterGC) {
441     Universe::verify();
442   }
443 
444   heap->rebuild_free_set(false /*concurrent*/);
445 }
446 
447 void ShenandoahDegenGC::op_cleanup_complete() {
448   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_cleanup_complete);
449   ShenandoahHeap::heap()->recycle_trash();
450 }
451 
452 void ShenandoahDegenGC::op_degenerated_fail() {
453   upgrade_to_full();
454 }
455 
456 void ShenandoahDegenGC::op_degenerated_futile() {
457   upgrade_to_full();
458 }
459 
460 const char* ShenandoahDegenGC::degen_event_message(ShenandoahDegenPoint point) const {
461   switch (point) {
462     case _degenerated_unset:
463       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (<UNSET>)");
464     case _degenerated_outside_cycle:
465       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Outside of Cycle)");
466     case _degenerated_roots:
467       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Roots)");
468     case _degenerated_mark:
469       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Mark)");
470     case _degenerated_evac:
471       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Evacuation)");
472     case _degenerated_update_refs:
473       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Update Refs)");
474     default:
475       ShouldNotReachHere();
476       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (?)");
477   }
478 }
479 
480 void ShenandoahDegenGC::upgrade_to_full() {
481   log_info(gc)("Degenerated GC upgrading to Full GC");
482   ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full();
483   ShenandoahFullGC full_gc;
484   full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc);
485 }