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