< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahDegeneratedGC.cpp

Print this page

  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/shenandoahCollectorPolicy.hpp"
 29 #include "gc/shenandoah/shenandoahConcurrentMark.hpp"
 30 #include "gc/shenandoah/shenandoahDegeneratedGC.hpp"
 31 #include "gc/shenandoah/shenandoahFullGC.hpp"


 32 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 33 #include "gc/shenandoah/shenandoahMetrics.hpp"
 34 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"

 35 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
 36 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
 37 #include "gc/shenandoah/shenandoahSTWMark.hpp"
 38 #include "gc/shenandoah/shenandoahUtils.hpp"
 39 #include "gc/shenandoah/shenandoahVerifier.hpp"

 40 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
 41 #include "gc/shenandoah/shenandoahVMOperations.hpp"
 42 #include "runtime/vmThread.hpp"
 43 #include "utilities/events.hpp"
 44 
 45 ShenandoahDegenGC::ShenandoahDegenGC(ShenandoahDegenPoint degen_point) :
 46   ShenandoahGC(),
 47   _degen_point(degen_point),

 48   _abbreviated(false) {
 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(&degenerated_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 if
118       // concurrent mark has yet completed
119       if (_degen_point == ShenandoahDegenPoint::_degenerated_mark &&
120           heap->is_concurrent_mark_in_progress()) {
121         op_finish_mark();
122       }
123       assert(!heap->cancelled_gc(), "STW mark can not OOM");
124 
125       /* Degen select Collection Set. etc. */
126       op_prepare_evacuation();
127 
128       op_cleanup_early();
129 

153           }
154         }
155 
156         // Degeneration under oom-evac protocol might have left some objects in
157         // collection set un-evacuated. Restart evacuation from the beginning to
158         // capture all objects. For all the objects that are already evacuated,
159         // it would be a simple check, which is supposed to be fast. This is also
160         // safe to do even without degeneration, as CSet iterator is at beginning
161         // in preparation for evacuation anyway.
162         //
163         // Before doing that, we need to make sure we never had any cset-pinned
164         // regions. This may happen if allocation failure happened when evacuating
165         // the about-to-be-pinned object, oom-evac protocol left the object in
166         // the collection set, and then the pin reached the cset region. If we continue
167         // the cycle here, we would trash the cset and alive objects in it. To avoid
168         // it, we fail degeneration right away and slide into Full GC to recover.
169 
170         {
171           heap->sync_pinned_region_status();
172           heap->collection_set()->clear_current_index();
173 
174           ShenandoahHeapRegion* r;
175           while ((r = heap->collection_set()->next()) != nullptr) {
176             if (r->is_pinned()) {
177               heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);
178               op_degenerated_fail();
179               return;
180             }
181           }
182 
183           heap->collection_set()->clear_current_index();
184         }
185         op_evacuate();
186         if (heap->cancelled_gc()) {
187           op_degenerated_fail();
188           return;
189         }
190       }
191 





192       // If heuristics thinks we should do the cycle, this flag would be set,
193       // and we need to do update-refs. Otherwise, it would be the shortcut cycle.
194       if (heap->has_forwarded_objects()) {
195         op_init_updaterefs();
196         assert(!heap->cancelled_gc(), "STW reference update can not OOM");
197       } else {
198         _abbreviated = true;
199       }
200 
201     case _degenerated_updaterefs:
202       if (heap->has_forwarded_objects()) {
203         op_updaterefs();
204         op_update_roots();
205         assert(!heap->cancelled_gc(), "STW reference update can not OOM");
206       }
207 
208       // Disarm nmethods that armed in concurrent cycle.
209       // In above case, update roots should disarm them
210       ShenandoahCodeRoots::disarm_nmethods();
211 







212       op_cleanup_complete();









213       break;
214     default:
215       ShouldNotReachHere();
216   }
217 










218   if (ShenandoahVerify) {
219     heap->verifier()->verify_after_degenerated();
220   }
221 
222   if (VerifyAfterGC) {
223     Universe::verify();
224   }
225 
226   metrics.snap_after();
227 
228   // Check for futility and fail. There is no reason to do several back-to-back Degenerated cycles,
229   // because that probably means the heap is overloaded and/or fragmented.
230   if (!metrics.is_good_progress()) {
231     heap->notify_gc_no_progress();
232     heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);
233     op_degenerated_futile();
234   } else {
235     heap->notify_gc_progress();
236     heap->shenandoah_policy()->record_success_degenerated(_abbreviated);
237     heap->heuristics()->record_success_degenerated();
238   }
239 }
240 
241 void ShenandoahDegenGC::op_reset() {
242   ShenandoahHeap::heap()->prepare_gc();
243 }
244 
245 void ShenandoahDegenGC::op_mark() {
246   assert(!ShenandoahHeap::heap()->is_concurrent_mark_in_progress(), "Should be reset");
247   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_mark);
248   ShenandoahSTWMark mark(false /*full gc*/);
249   mark.clear();
250   mark.mark();
251 }
252 
253 void ShenandoahDegenGC::op_finish_mark() {
254   ShenandoahConcurrentMark mark;
255   mark.finish_mark();
256 }
257 
258 void ShenandoahDegenGC::op_prepare_evacuation() {
259   ShenandoahHeap* const heap = ShenandoahHeap::heap();
260   if (ShenandoahVerify) {
261     heap->verifier()->verify_roots_no_forwarded();
262   }
263 
264   // STW cleanup weak roots and unload classes
265   heap->parallel_cleaning(false /*full gc*/);

266   // Prepare regions and collection set
267   heap->prepare_regions_and_collection_set(false /*concurrent*/);
268 
269   // Retire the TLABs, which will force threads to reacquire their TLABs after the pause.
270   // This is needed for two reasons. Strong one: new allocations would be with new freeset,
271   // which would be outside the collection set, so no cset writes would happen there.
272   // Weaker one: new allocations would happen past update watermark, and so less work would
273   // be needed for reference updates (would update the large filler instead).
274   if (UseTLAB) {
275     ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_final_manage_labs);
276     heap->tlabs_retire(false);
277   }
278 
279   if (!heap->collection_set()->is_empty()) {







280     heap->set_evacuation_in_progress(true);
281     heap->set_has_forwarded_objects(true);
282 
283     if(ShenandoahVerify) {
284       heap->verifier()->verify_during_evacuation();
285     }


286   } else {
287     if (ShenandoahVerify) {
288       heap->verifier()->verify_after_concmark();
289     }
290 
291     if (VerifyAfterGC) {
292       Universe::verify();
293     }
294   }
295 }
296 
297 void ShenandoahDegenGC::op_cleanup_early() {
298   ShenandoahHeap::heap()->recycle_trash();
299 }
300 





301 void ShenandoahDegenGC::op_evacuate() {
302   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_evac);
303   ShenandoahHeap::heap()->evacuate_collection_set(false /* concurrent*/);
304 }
305 
306 void ShenandoahDegenGC::op_init_updaterefs() {
307   // Evacuation has completed
308   ShenandoahHeap* const heap = ShenandoahHeap::heap();
309   heap->set_evacuation_in_progress(false);
310   heap->set_concurrent_weak_root_in_progress(false);
311   heap->set_concurrent_strong_root_in_progress(false);
312 
313   heap->prepare_update_heap_references(false /*concurrent*/);
314   heap->set_update_refs_in_progress(true);
315 }
316 
317 void ShenandoahDegenGC::op_updaterefs() {
318   ShenandoahHeap* const heap = ShenandoahHeap::heap();
319   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_updaterefs);
320   // Handed over from concurrent update references phase
321   heap->update_heap_references(false /*concurrent*/);
322 
323   heap->set_update_refs_in_progress(false);
324   heap->set_has_forwarded_objects(false);
325 }
326 
327 void ShenandoahDegenGC::op_update_roots() {
328   ShenandoahHeap* const heap = ShenandoahHeap::heap();
329 
330   update_roots(false /*full_gc*/);
331 
332   heap->update_heap_region_states(false /*concurrent*/);

341 
342   heap->rebuild_free_set(false /*concurrent*/);
343 }
344 
345 void ShenandoahDegenGC::op_cleanup_complete() {
346   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_cleanup_complete);
347   ShenandoahHeap::heap()->recycle_trash();
348 }
349 
350 void ShenandoahDegenGC::op_degenerated_fail() {
351   upgrade_to_full();
352 }
353 
354 void ShenandoahDegenGC::op_degenerated_futile() {
355   upgrade_to_full();
356 }
357 
358 const char* ShenandoahDegenGC::degen_event_message(ShenandoahDegenPoint point) const {
359   switch (point) {
360     case _degenerated_unset:
361       return "Pause Degenerated GC (<UNSET>)";
362     case _degenerated_outside_cycle:
363       return "Pause Degenerated GC (Outside of Cycle)";


364     case _degenerated_mark:
365       return "Pause Degenerated GC (Mark)";
366     case _degenerated_evac:
367       return "Pause Degenerated GC (Evacuation)";
368     case _degenerated_updaterefs:
369       return "Pause Degenerated GC (Update Refs)";
370     default:
371       ShouldNotReachHere();
372       return "ERROR";
373   }
374 }
375 
376 void ShenandoahDegenGC::upgrade_to_full() {
377   log_info(gc)("Degenerated GC upgrading to Full GC");
378   ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full();
379   ShenandoahFullGC full_gc;
380   full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc);
381 }

  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(&degenerated_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() &&
137             (_generation->is_young() || (_generation->is_global() && ShenandoahVerify))) {
138         // Swap remembered sets for young, or if the verifier will run during a global collect
139         // TODO: This path should not depend on ShenandoahVerify
140         _generation->swap_remembered_set();
141       }
142 
143     case _degenerated_roots:
144       // Degenerated from concurrent root mark, reset the flag for STW mark
145       if (!heap->mode()->is_generational()) {
146         if (heap->is_concurrent_mark_in_progress()) {
147           heap->cancel_concurrent_mark();
148         }
149       } else {
150         if (_generation->is_concurrent_mark_in_progress()) {
151           // We want to allow old generation marking to be punctuated by young collections
152           // (even if they have degenerated). If this is a global cycle, we'd have cancelled
153           // the entire old gc before coming into this switch. Note that cancel_marking on
154           // the generation does NOT abandon incomplete SATB buffers as cancel_concurrent_mark does.
155           // We need to separate out the old pointers which is done below.
156           _generation->cancel_marking();
157         }
158 
159         if (heap->is_concurrent_mark_in_progress()) {
160           // If either old or young marking is in progress, the SATB barrier will be enabled.
161           // The SATB buffer may hold a mix of old and young pointers. The old pointers need to be
162           // transferred to the old generation mark queues and the young pointers are NOT part
163           // of this snapshot, so they must be dropped here. It is safe to drop them here because
164           // we will rescan the roots on this safepoint.
165           heap->transfer_old_pointers_from_satb();
166         }
167       }
168 
169       if (_degen_point == ShenandoahDegenPoint::_degenerated_roots) {
170         // We only need this if the concurrent cycle has already swapped the card tables.
171         // Marking will use the 'read' table, but interesting pointers may have been
172         // recorded in the 'write' table in the time between the cancelled concurrent cycle
173         // and this degenerated cycle. These pointers need to be included the 'read' table
174         // used to scan the remembered set during the STW mark which follows here.
175         _generation->merge_write_table();
176       }
177 
178       op_reset();
179 
180       // STW mark
181       op_mark();
182 
183     case _degenerated_mark:
184       // No fallthrough. Continue mark, handed over from concurrent mark if
185       // concurrent mark has yet completed
186       if (_degen_point == ShenandoahDegenPoint::_degenerated_mark &&
187           heap->is_concurrent_mark_in_progress()) {
188         op_finish_mark();
189       }
190       assert(!heap->cancelled_gc(), "STW mark can not OOM");
191 
192       /* Degen select Collection Set. etc. */
193       op_prepare_evacuation();
194 
195       op_cleanup_early();
196 

220           }
221         }
222 
223         // Degeneration under oom-evac protocol might have left some objects in
224         // collection set un-evacuated. Restart evacuation from the beginning to
225         // capture all objects. For all the objects that are already evacuated,
226         // it would be a simple check, which is supposed to be fast. This is also
227         // safe to do even without degeneration, as CSet iterator is at beginning
228         // in preparation for evacuation anyway.
229         //
230         // Before doing that, we need to make sure we never had any cset-pinned
231         // regions. This may happen if allocation failure happened when evacuating
232         // the about-to-be-pinned object, oom-evac protocol left the object in
233         // the collection set, and then the pin reached the cset region. If we continue
234         // the cycle here, we would trash the cset and alive objects in it. To avoid
235         // it, we fail degeneration right away and slide into Full GC to recover.
236 
237         {
238           heap->sync_pinned_region_status();
239           heap->collection_set()->clear_current_index();

240           ShenandoahHeapRegion* r;
241           while ((r = heap->collection_set()->next()) != nullptr) {
242             if (r->is_pinned()) {
243               heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);
244               op_degenerated_fail();
245               return;
246             }
247           }
248 
249           heap->collection_set()->clear_current_index();
250         }
251         op_evacuate();
252         if (heap->cancelled_gc()) {
253           op_degenerated_fail();
254           return;
255         }
256       }
257 
258       // Update collector state regardless of whether or not there are forwarded objects
259       heap->set_evacuation_in_progress(false);
260       heap->set_concurrent_weak_root_in_progress(false);
261       heap->set_concurrent_strong_root_in_progress(false);
262 
263       // If heuristics thinks we should do the cycle, this flag would be set,
264       // and we need to do update-refs. Otherwise, it would be the shortcut cycle.
265       if (heap->has_forwarded_objects()) {
266         op_init_updaterefs();
267         assert(!heap->cancelled_gc(), "STW reference update can not OOM");
268       } else {
269         _abbreviated = true;
270       }
271 
272     case _degenerated_updaterefs:
273       if (heap->has_forwarded_objects()) {
274         op_updaterefs();
275         op_update_roots();
276         assert(!heap->cancelled_gc(), "STW reference update can not OOM");
277       }
278 
279       // Disarm nmethods that armed in concurrent cycle.
280       // In above case, update roots should disarm them
281       ShenandoahCodeRoots::disarm_nmethods();
282 
283       if (heap->mode()->is_generational() && heap->is_concurrent_old_mark_in_progress()) {
284         // This is still necessary for degenerated cycles because the degeneration point may occur
285         // after final mark of the young generation. See ShenandoahConcurrentGC::op_final_updaterefs for
286         // a more detailed explanation.
287         heap->transfer_old_pointers_from_satb();
288       }
289 
290       op_cleanup_complete();
291       // We defer generation resizing actions until after cset regions have been recycled.
292       if (heap->mode()->is_generational()) {
293         auto result = ShenandoahGenerationalHeap::heap()->balance_generations();
294         LogTarget(Info, gc, ergo) lt;
295         if (lt.is_enabled()) {
296           LogStream ls(lt);
297           result.print_on("Degenerated GC", &ls);
298         }
299       }
300       break;
301     default:
302       ShouldNotReachHere();
303   }
304 
305   if (heap->mode()->is_generational()) {
306     // In case degeneration interrupted concurrent evacuation or update references, we need to clean up transient state.
307     // Otherwise, these actions have no effect.
308     ShenandoahGenerationalHeap::heap()->reset_generation_reserves();
309 
310     if (!ShenandoahGenerationalHeap::heap()->old_generation()->is_parseable()) {
311       op_global_coalesce_and_fill();
312     }
313   }
314 
315   if (ShenandoahVerify) {
316     heap->verifier()->verify_after_degenerated();
317   }
318 
319   if (VerifyAfterGC) {
320     Universe::verify();
321   }
322 
323   metrics.snap_after();
324 
325   // Check for futility and fail. There is no reason to do several back-to-back Degenerated cycles,
326   // because that probably means the heap is overloaded and/or fragmented.
327   if (!metrics.is_good_progress()) {
328     heap->notify_gc_no_progress();
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() || heap->old_generation()->has_in_place_promotions()) {
377     // Even if the collection set is empty, we need to do evacuation if there are regions to be promoted in place.
378     // Degenerated evacuation takes responsibility for registering objects and setting the remembered set cards to dirty.
379 
380     if (ShenandoahVerify) {
381       heap->verifier()->verify_before_evacuation();
382     }
383 
384     heap->set_evacuation_in_progress(true);

385 
386     if(ShenandoahVerify) {
387       heap->verifier()->verify_during_evacuation();
388     }
389 
390     heap->set_has_forwarded_objects(!heap->collection_set()->is_empty());
391   } else {
392     if (ShenandoahVerify) {
393       heap->verifier()->verify_after_concmark();
394     }
395 
396     if (VerifyAfterGC) {
397       Universe::verify();
398     }
399   }
400 }
401 
402 void ShenandoahDegenGC::op_cleanup_early() {
403   ShenandoahHeap::heap()->recycle_trash();
404 }
405 
406 void ShenandoahDegenGC::op_global_coalesce_and_fill() {
407   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_coalesce_and_fill);
408   ShenandoahGenerationalHeap::heap()->coalesce_and_fill_old_regions(false);
409 }
410 
411 void ShenandoahDegenGC::op_evacuate() {
412   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_evac);
413   ShenandoahHeap::heap()->evacuate_collection_set(false /* concurrent*/);
414 }
415 
416 void ShenandoahDegenGC::op_init_updaterefs() {
417   // Evacuation has completed
418   ShenandoahHeap* const heap = ShenandoahHeap::heap();




419   heap->prepare_update_heap_references(false /*concurrent*/);
420   heap->set_update_refs_in_progress(true);
421 }
422 
423 void ShenandoahDegenGC::op_updaterefs() {
424   ShenandoahHeap* const heap = ShenandoahHeap::heap();
425   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_updaterefs);
426   // Handed over from concurrent update references phase
427   heap->update_heap_references(false /*concurrent*/);
428 
429   heap->set_update_refs_in_progress(false);
430   heap->set_has_forwarded_objects(false);
431 }
432 
433 void ShenandoahDegenGC::op_update_roots() {
434   ShenandoahHeap* const heap = ShenandoahHeap::heap();
435 
436   update_roots(false /*full_gc*/);
437 
438   heap->update_heap_region_states(false /*concurrent*/);

447 
448   heap->rebuild_free_set(false /*concurrent*/);
449 }
450 
451 void ShenandoahDegenGC::op_cleanup_complete() {
452   ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_cleanup_complete);
453   ShenandoahHeap::heap()->recycle_trash();
454 }
455 
456 void ShenandoahDegenGC::op_degenerated_fail() {
457   upgrade_to_full();
458 }
459 
460 void ShenandoahDegenGC::op_degenerated_futile() {
461   upgrade_to_full();
462 }
463 
464 const char* ShenandoahDegenGC::degen_event_message(ShenandoahDegenPoint point) const {
465   switch (point) {
466     case _degenerated_unset:
467       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (<UNSET>)");
468     case _degenerated_outside_cycle:
469       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Outside of Cycle)");
470     case _degenerated_roots:
471       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Roots)");
472     case _degenerated_mark:
473       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Mark)");
474     case _degenerated_evac:
475       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Evacuation)");
476     case _degenerated_updaterefs:
477       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (Update Refs)");
478     default:
479       ShouldNotReachHere();
480       SHENANDOAH_RETURN_EVENT_MESSAGE(_generation->type(), "Pause Degenerated GC", " (?)");
481   }
482 }
483 
484 void ShenandoahDegenGC::upgrade_to_full() {
485   log_info(gc)("Degenerated GC upgrading to Full GC");
486   ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full();
487   ShenandoahFullGC full_gc;
488   full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc);
489 }
< prev index next >