1 /*
2 * Copyright (c) 2014, 2021, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 *
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 */
26
27
28 #include "compiler/oopMap.hpp"
29 #include "gc/shared/continuationGCSupport.hpp"
30 #include "gc/shared/fullGCForwarding.inline.hpp"
31 #include "gc/shared/gcTraceTime.inline.hpp"
32 #include "gc/shared/preservedMarks.inline.hpp"
33 #include "gc/shared/tlab_globals.hpp"
34 #include "gc/shared/workerThread.hpp"
35 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
36 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
37 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
38 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
39 #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
40 #include "gc/shenandoah/shenandoahFreeSet.hpp"
41 #include "gc/shenandoah/shenandoahFullGC.hpp"
42 #include "gc/shenandoah/shenandoahGenerationalFullGC.hpp"
43 #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
44 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
45 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
46 #include "gc/shenandoah/shenandoahHeapRegionClosures.hpp"
47 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
48 #include "gc/shenandoah/shenandoahMark.inline.hpp"
49 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
50 #include "gc/shenandoah/shenandoahMetrics.hpp"
51 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
52 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
53 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
54 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
55 #include "gc/shenandoah/shenandoahStackWatermark.hpp"
56 #include "gc/shenandoah/shenandoahSTWMark.hpp"
57 #include "gc/shenandoah/shenandoahUtils.hpp"
58 #include "gc/shenandoah/shenandoahVerifier.hpp"
59 #include "gc/shenandoah/shenandoahVMOperations.hpp"
60 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
61 #include "memory/metaspaceUtils.hpp"
62 #include "memory/universe.hpp"
63 #include "oops/compressedOops.inline.hpp"
64 #include "oops/oop.inline.hpp"
65 #include "runtime/orderAccess.hpp"
66 #include "runtime/vmThread.hpp"
67 #include "utilities/copy.hpp"
68 #include "utilities/events.hpp"
69 #include "utilities/growableArray.hpp"
70
71 ShenandoahFullGC::ShenandoahFullGC() :
72 ShenandoahGC(ShenandoahHeap::heap()->global_generation()),
73 _gc_timer(ShenandoahHeap::heap()->gc_timer()),
74 _preserved_marks(new PreservedMarksSet(true)) {}
75
76 ShenandoahFullGC::~ShenandoahFullGC() {
77 delete _preserved_marks;
78 }
79
80 bool ShenandoahFullGC::collect(GCCause::Cause cause) {
81 vmop_entry_full(cause);
82 // Always success
83 return true;
84 }
85
86 void ShenandoahFullGC::vmop_entry_full(GCCause::Cause cause) {
87 ShenandoahHeap* const heap = ShenandoahHeap::heap();
88 TraceCollectorStats tcs(heap->monitoring_support()->full_stw_collection_counters());
89 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::full_gc_gross);
90
91 heap->try_inject_alloc_failure();
92 VM_ShenandoahFullGC op(cause, this);
93 VMThread::execute(&op);
94 }
95
96 void ShenandoahFullGC::entry_full(GCCause::Cause cause) {
97 static const char* msg = "Pause Full";
98 ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::full_gc, true /* log_heap_usage */);
99 EventMark em("%s", msg);
100
101 ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
102 ShenandoahWorkerPolicy::calc_workers_for_fullgc(),
103 "full gc");
104
105 op_full(cause);
106 }
107
108 void ShenandoahFullGC::op_full(GCCause::Cause cause) {
109 ShenandoahHeap* const heap = ShenandoahHeap::heap();
110
111 ShenandoahMetricsSnapshot metrics(heap->free_set());
112
113 // Perform full GC
114 do_it(cause);
115
116 if (heap->mode()->is_generational()) {
117 ShenandoahGenerationalFullGC::handle_completion(heap);
118 }
119
120 if (metrics.is_good_progress()) {
121 heap->notify_gc_progress();
122 } else {
123 // Nothing to do. Tell the allocation path that we have failed to make
124 // progress, and it can finally fail.
125 heap->notify_gc_no_progress();
126 }
127
128 // Regardless if progress was made, we record that we completed a "successful" full GC.
129 _generation->heuristics()->record_success_full();
130 heap->shenandoah_policy()->record_success_full();
131
132 // Leaving full GC, we need to flip barriers back to idle.
133 ShenandoahCodeRoots::arm_nmethods();
134
135 // There is no operation that follows Full GC, so run all nmethod barriers now.
136 // We will take additional time during the safepoint, but we would also incur no
137 // additional latency on mutators once we return from here.
138 ShenandoahCodeRoots::disarm_nmethods();
139
140 {
141 ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::full_gc_propagate_gc_state);
142 heap->propagate_gc_state_to_all_threads();
143 }
144 }
145
146 void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
147 ShenandoahHeap* heap = ShenandoahHeap::heap();
148
149 // A full GC may be entered directly, or as an upgrade from a failed
150 // degenerated GC. In the latter case, self-forwarded objects may be
151 // present from the failed evacuation. Drain those marks before any phase
152 // (verify, update_roots, phase1_mark_heap) walks headers.
153 {
154 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_un_self_forward);
155 heap->un_self_forward_cset_regions();
156 }
157
158 if (heap->mode()->is_generational()) {
159 ShenandoahGenerationalFullGC::prepare();
160 }
161
162 if (ShenandoahVerify) {
163 heap->verifier()->verify_before_fullgc(_generation);
164 }
165
166 if (VerifyBeforeGC) {
167 Universe::verify();
168 }
169
170 // Degenerated GC may carry concurrent root flags when upgrading to
171 // full GC. We need to reset it before mutators resume.
172 heap->set_concurrent_strong_root_in_progress(false);
173 heap->set_concurrent_weak_root_in_progress(false);
174
175 heap->set_full_gc_in_progress(true);
176
177 assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
178 assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped");
179
180 {
181 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_pre);
182 heap->pre_full_gc_dump(_gc_timer);
183 }
184
185 {
186 ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare);
187 // Full GC is supposed to recover from any GC state:
188
189 // a0. Remember if we have forwarded objects
190 bool has_forwarded_objects = heap->has_forwarded_objects();
191
192 // a1. Cancel evacuation, if in progress
193 if (heap->is_evacuation_in_progress()) {
194 heap->set_evacuation_in_progress(false);
195 }
196 assert(!heap->is_evacuation_in_progress(), "sanity");
197
198 // a2. Cancel update-refs, if in progress
199 if (heap->is_update_refs_in_progress()) {
200 heap->set_update_refs_in_progress(false);
201 }
202 assert(!heap->is_update_refs_in_progress(), "sanity");
203
204 // b. Cancel all concurrent marks, if in progress
205 if (heap->is_concurrent_mark_in_progress()) {
206 heap->cancel_concurrent_mark();
207 }
208 assert(!heap->is_concurrent_mark_in_progress(), "sanity");
209
210 // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
211 if (has_forwarded_objects) {
212 update_roots(true /*full_gc*/);
213 }
214
215 // d. Abandon reference discovery and clear all discovered references.
216 ShenandoahReferenceProcessor* rp = _generation->ref_processor();
217 rp->abandon_partial_discovery();
218
219 // e. Sync pinned region status from the CP marks
220 heap->sync_pinned_region_status();
221
222 if (heap->mode()->is_generational()) {
223 ShenandoahGenerationalFullGC::restore_top_before_promote(heap);
224 }
225
226 // The rest of prologue:
227 _preserved_marks->init(heap->workers()->active_workers());
228
229 assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
230 }
231
232 if (UseTLAB) {
233 // Note: PLABs are also retired with GCLABs in generational mode.
234 heap->gclabs_retire(ResizeTLAB);
235 heap->tlabs_retire(ResizeTLAB);
236 }
237
238 OrderAccess::fence();
239
240 phase1_mark_heap();
241
242 // Once marking is done, which may have fixed up forwarded objects, we can drop it.
243 // Coming out of Full GC, we would not have any forwarded objects.
244 // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
245 heap->set_has_forwarded_objects(false);
246
247 heap->set_full_gc_move_in_progress(true);
248
249 // Setup workers for the rest
250 OrderAccess::fence();
251
252 // Initialize worker slices
253 ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
254 for (uint i = 0; i < heap->max_workers(); i++) {
255 worker_slices[i] = new ShenandoahHeapRegionSet();
256 }
257
258 {
259 // The rest of code performs region moves, where region status is undefined
260 // until all phases run together.
261 ShenandoahHeapLocker lock(heap->lock());
262
263 phase2_calculate_target_addresses(worker_slices);
264
265 OrderAccess::fence();
266
267 phase3_update_references();
268
269 phase4_compact_objects(worker_slices);
270
271 phase5_epilog();
272 }
273 heap->start_idle_span();
274
275 // Resize metaspace
276 MetaspaceGC::compute_new_size();
277
278 // Free worker slices
279 for (uint i = 0; i < heap->max_workers(); i++) {
280 delete worker_slices[i];
281 }
282 FREE_C_HEAP_ARRAY(worker_slices);
283
284 heap->set_full_gc_move_in_progress(false);
285 heap->set_full_gc_in_progress(false);
286
287 DEBUG_ONLY(heap->assert_no_self_forwards());
288
289 if (ShenandoahVerify) {
290 heap->verifier()->verify_after_fullgc(_generation);
291 }
292
293 if (VerifyAfterGC) {
294 Universe::verify();
295 }
296
297 {
298 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_post);
299 heap->post_full_gc_dump(_gc_timer);
300 }
301 }
302
303 void ShenandoahFullGC::phase1_mark_heap() {
304 GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
305 ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
306
307 ShenandoahHeap* heap = ShenandoahHeap::heap();
308
309 _generation->reset_mark_bitmap<true, true>();
310 assert(heap->marking_context()->is_bitmap_clear(), "sanity");
311 assert(!_generation->is_mark_complete(), "sanity");
312
313 heap->set_unload_classes(_generation->heuristics()->can_unload_classes());
314
315 ShenandoahReferenceProcessor* rp = _generation->ref_processor();
316 // enable ("weak") refs discovery
317 rp->set_soft_reference_policy(true); // forcefully purge all soft references
318
319 ShenandoahSTWMark mark(_generation, true /*full_gc*/);
320 mark.mark();
321 heap->parallel_cleaning(_generation, true /* full_gc */);
322
323 if (ShenandoahHeap::heap()->mode()->is_generational()) {
324 ShenandoahGenerationalFullGC::log_live_in_old(heap);
325 }
326 }
327
328 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
329 private:
330 PreservedMarks* const _preserved_marks;
331 ShenandoahHeap* const _heap;
332 GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
333 int _empty_regions_pos;
334 ShenandoahHeapRegion* _to_region;
335 ShenandoahHeapRegion* _from_region;
336 HeapWord* _compact_point;
337
338 public:
339 ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks,
340 GrowableArray<ShenandoahHeapRegion*>& empty_regions,
341 ShenandoahHeapRegion* to_region) :
342 _preserved_marks(preserved_marks),
343 _heap(ShenandoahHeap::heap()),
344 _empty_regions(empty_regions),
345 _empty_regions_pos(0),
346 _to_region(to_region),
347 _from_region(nullptr),
348 _compact_point(to_region->bottom()) {}
349
350 void set_from_region(ShenandoahHeapRegion* from_region) {
351 _from_region = from_region;
352 }
353
354 void finish() {
355 assert(_to_region != nullptr, "should not happen");
356 _to_region->set_new_top(_compact_point);
357 }
358
359 bool is_compact_same_region() {
360 return _from_region == _to_region;
361 }
362
363 int empty_regions_pos() {
364 return _empty_regions_pos;
365 }
366
367 void do_object(oop p) override {
368 shenandoah_assert_mark_complete(cast_from_oop<HeapWord*>(p));
369 assert(_from_region != nullptr, "must set before work");
370 assert(_heap->global_generation()->is_mark_complete(), "marking must be finished");
371 assert(_heap->marking_context()->is_marked(p), "must be marked");
372 assert(!_heap->marking_context()->allocated_after_mark_start(p), "must be truly marked");
373
374 size_t obj_size = p->size();
375 if (_compact_point + obj_size > _to_region->end()) {
376 finish();
377
378 // Object doesn't fit. Pick next empty region and start compacting there.
379 ShenandoahHeapRegion* new_to_region;
380 if (_empty_regions_pos < _empty_regions.length()) {
381 new_to_region = _empty_regions.at(_empty_regions_pos);
382 _empty_regions_pos++;
383 } else {
384 // Out of empty region? Compact within the same region.
385 new_to_region = _from_region;
386 }
387
388 assert(new_to_region != _to_region, "must not reuse same to-region");
389 assert(new_to_region != nullptr, "must not be null");
390 _to_region = new_to_region;
391 _compact_point = _to_region->bottom();
392 }
393
394 // Object fits into current region, record new location, if object does not move:
395 assert(_compact_point + obj_size <= _to_region->end(), "must fit");
396 shenandoah_assert_not_forwarded(nullptr, p);
397 if (_compact_point != cast_from_oop<HeapWord*>(p)) {
398 _preserved_marks->push_if_necessary(p, p->mark());
399 FullGCForwarding::forward_to(p, cast_to_oop(_compact_point));
400 }
401 _compact_point += obj_size;
402 }
403 };
404
405 class ShenandoahPrepareForCompactionTask : public WorkerTask {
406 private:
407 PreservedMarksSet* const _preserved_marks;
408 ShenandoahHeap* const _heap;
409 ShenandoahHeapRegionSet** const _worker_slices;
410
411 public:
412 ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :
413 WorkerTask("Shenandoah Prepare For Compaction"),
414 _preserved_marks(preserved_marks),
415 _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
416 }
417
418 static bool is_candidate_region(ShenandoahHeapRegion* r) {
419 // Empty region: get it into the slice to defragment the slice itself.
420 // We could have skipped this without violating correctness, but we really
421 // want to compact all live regions to the start of the heap, which sometimes
422 // means moving them into the fully empty regions.
423 if (r->is_empty()) return true;
424
425 // Can move the region, and this is not the humongous region. Humongous
426 // moves are special cased here, because their moves are handled separately.
427 return r->is_stw_move_allowed() && !r->is_humongous();
428 }
429
430 void work(uint worker_id) override;
431 private:
432 template<typename ClosureType>
433 void prepare_for_compaction(ClosureType& cl,
434 GrowableArray<ShenandoahHeapRegion*>& empty_regions,
435 ShenandoahHeapRegionSetIterator& it,
436 ShenandoahHeapRegion* from_region);
437 };
438
439 void ShenandoahPrepareForCompactionTask::work(uint worker_id) {
440 ShenandoahParallelWorkerSession worker_session(worker_id);
441 ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
442 ShenandoahHeapRegionSetIterator it(slice);
443 ShenandoahHeapRegion* from_region = it.next();
444 // No work?
445 if (from_region == nullptr) {
446 return;
447 }
448
449 // Sliding compaction. Walk all regions in the slice, and compact them.
450 // Remember empty regions and reuse them as needed.
451 ResourceMark rm;
452
453 GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
454
455 if (_heap->mode()->is_generational()) {
456 ShenandoahPrepareForGenerationalCompactionObjectClosure cl(_preserved_marks->get(worker_id),
457 empty_regions, from_region, worker_id);
458 prepare_for_compaction(cl, empty_regions, it, from_region);
459 } else {
460 ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
461 prepare_for_compaction(cl, empty_regions, it, from_region);
462 }
463 }
464
465 template<typename ClosureType>
466 void ShenandoahPrepareForCompactionTask::prepare_for_compaction(ClosureType& cl,
467 GrowableArray<ShenandoahHeapRegion*>& empty_regions,
468 ShenandoahHeapRegionSetIterator& it,
469 ShenandoahHeapRegion* from_region) {
470 while (from_region != nullptr) {
471 assert(is_candidate_region(from_region), "Sanity");
472 cl.set_from_region(from_region);
473 if (from_region->has_live()) {
474 _heap->marked_object_iterate(from_region, &cl);
475 }
476
477 // Compacted the region to somewhere else? From-region is empty then.
478 if (!cl.is_compact_same_region()) {
479 empty_regions.append(from_region);
480 }
481 from_region = it.next();
482 }
483 cl.finish();
484
485 // Mark all remaining regions as empty
486 for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
487 ShenandoahHeapRegion* r = empty_regions.at(pos);
488 r->set_new_top(r->bottom());
489 }
490 }
491
492 void ShenandoahFullGC::calculate_target_humongous_objects() {
493 ShenandoahHeap* heap = ShenandoahHeap::heap();
494
495 // Compute the new addresses for humongous objects. We need to do this after addresses
496 // for regular objects are calculated, and we know what regions in heap suffix are
497 // available for humongous moves.
498 //
499 // Scan the heap backwards, because we are compacting humongous regions towards the end.
500 // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
501 // humongous start there.
502 //
503 // The complication is potential non-movable regions during the scan. If such region is
504 // detected, then sliding restarts towards that non-movable region.
505
506 size_t to_begin = heap->num_regions();
507 size_t to_end = heap->num_regions();
508
509 log_debug(gc)("Full GC calculating target humongous objects from end %zu", to_end);
510 for (size_t c = heap->num_regions(); c > 0; c--) {
511 ShenandoahHeapRegion *r = heap->get_region(c - 1);
512 if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
513 // To-region candidate: record this, and continue scan
514 to_begin = r->index();
515 continue;
516 }
517
518 if (r->is_humongous_start() && r->is_stw_move_allowed()) {
519 // From-region candidate: movable humongous region
520 oop old_obj = cast_to_oop(r->bottom());
521 size_t words_size = old_obj->size();
522 size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
523
524 size_t start = to_end - num_regions;
525
526 if (start >= to_begin && start != r->index()) {
527 // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
528 _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark());
529 FullGCForwarding::forward_to(old_obj, cast_to_oop(heap->get_region(start)->bottom()));
530 to_end = start;
531 continue;
532 }
533 }
534
535 // Failed to fit. Scan starting from current region.
536 to_begin = r->index();
537 to_end = r->index();
538 }
539 }
540
541 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
542 public:
543 void heap_region_do(ShenandoahHeapRegion* r) override {
544 if (r->is_trash()) {
545 r->try_recycle_under_lock();
546 // No need to adjust_interval_for_recycled_old_region. That will be taken care of during freeset rebuild.
547 }
548 if (r->is_cset()) {
549 // Leave affiliation unchanged
550 r->make_regular_bypass();
551 }
552 if (r->is_empty_uncommitted()) {
553 r->make_committed_bypass();
554 }
555 assert (r->is_committed(), "only committed regions in heap now, see region %zu", r->index());
556
557 // Record current region occupancy: this communicates empty regions are free
558 // to the rest of Full GC code.
559 r->set_new_top(r->top());
560 }
561 };
562
563 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure {
564 private:
565 ShenandoahHeap* const _heap;
566 ShenandoahMarkingContext* const _ctx;
567
568 public:
569 ShenandoahTrashImmediateGarbageClosure() :
570 _heap(ShenandoahHeap::heap()),
571 _ctx(ShenandoahHeap::heap()->global_generation()->complete_marking_context()) {}
572
573 void heap_region_do(ShenandoahHeapRegion* r) override {
574 if (r->is_humongous_start()) {
575 oop humongous_obj = cast_to_oop(r->bottom());
576 if (!_ctx->is_marked(humongous_obj)) {
577 assert(!r->has_live(), "Region %zu is not marked, should not have live", r->index());
578 _heap->trash_humongous_region_at(r);
579 } else {
580 assert(r->has_live(), "Region %zu should have live", r->index());
581 }
582 } else if (r->is_humongous_continuation()) {
583 // If we hit continuation, the non-live humongous starts should have been trashed already
584 assert(r->humongous_start_region()->has_live(), "Region %zu should have live", r->index());
585 } else if (r->is_regular()) {
586 if (!r->has_live()) {
587 r->make_trash_immediate();
588 }
589 }
590 }
591 };
592
593 void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
594 ShenandoahHeap* heap = ShenandoahHeap::heap();
595
596 uint n_workers = heap->workers()->active_workers();
597 size_t n_regions = heap->num_regions();
598
599 // What we want to accomplish: have the dense prefix of data, while still balancing
600 // out the parallel work.
601 //
602 // Assuming the amount of work is driven by the live data that needs moving, we can slice
603 // the entire heap into equal-live-sized prefix slices, and compact into them. So, each
604 // thread takes all regions in its prefix subset, and then it takes some regions from
605 // the tail.
606 //
607 // Tail region selection becomes interesting.
608 //
609 // First, we want to distribute the regions fairly between the workers, and those regions
610 // might have different amount of live data. So, until we sure no workers need live data,
611 // we need to only take what the worker needs.
612 //
613 // Second, since we slide everything to the left in each slice, the most busy regions
614 // would be the ones on the left. Which means we want to have all workers have their after-tail
615 // regions as close to the left as possible.
616 //
617 // The easiest way to do this is to distribute after-tail regions in round-robin between
618 // workers that still need live data.
619 //
620 // Consider parallel workers A, B, C, then the target slice layout would be:
621 //
622 // AAAAAAAABBBBBBBBCCCCCCCC|ABCABCABCABCABCABCABCABABABABABABABABABABAAAAA
623 //
624 // (.....dense-prefix.....) (.....................tail...................)
625 // [all regions fully live] [left-most regions are fuller that right-most]
626 //
627
628 // Compute how much live data is there. This would approximate the size of dense prefix
629 // we target to create.
630 size_t total_live = 0;
631 for (size_t idx = 0; idx < n_regions; idx++) {
632 ShenandoahHeapRegion *r = heap->get_region(idx);
633 if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
634 total_live += r->get_live_data_words();
635 }
636 }
637
638 // Estimate the size for the dense prefix. Note that we specifically count only the
639 // "full" regions, so there would be some non-full regions in the slice tail.
640 size_t live_per_worker = total_live / n_workers;
641 size_t prefix_regions_per_worker = live_per_worker / ShenandoahHeapRegion::region_size_words();
642 size_t prefix_regions_total = prefix_regions_per_worker * n_workers;
643 prefix_regions_total = MIN2(prefix_regions_total, n_regions);
644 assert(prefix_regions_total <= n_regions, "Sanity");
645
646 // There might be non-candidate regions in the prefix. To compute where the tail actually
647 // ends up being, we need to account those as well.
648 size_t prefix_end = prefix_regions_total;
649 for (size_t idx = 0; idx < prefix_regions_total; idx++) {
650 ShenandoahHeapRegion *r = heap->get_region(idx);
651 if (!ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
652 prefix_end++;
653 }
654 }
655 prefix_end = MIN2(prefix_end, n_regions);
656 assert(prefix_end <= n_regions, "Sanity");
657
658 // Distribute prefix regions per worker: each thread definitely gets its own same-sized
659 // subset of dense prefix.
660 size_t prefix_idx = 0;
661
662 size_t* live = NEW_C_HEAP_ARRAY(size_t, n_workers, mtGC);
663
664 for (size_t wid = 0; wid < n_workers; wid++) {
665 ShenandoahHeapRegionSet* slice = worker_slices[wid];
666
667 live[wid] = 0;
668 size_t regs = 0;
669
670 // Add all prefix regions for this worker
671 while (prefix_idx < prefix_end && regs < prefix_regions_per_worker) {
672 ShenandoahHeapRegion *r = heap->get_region(prefix_idx);
673 if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
674 slice->add_region(r);
675 live[wid] += r->get_live_data_words();
676 regs++;
677 }
678 prefix_idx++;
679 }
680 }
681
682 // Distribute the tail among workers in round-robin fashion.
683 size_t wid = n_workers - 1;
684
685 for (size_t tail_idx = prefix_end; tail_idx < n_regions; tail_idx++) {
686 ShenandoahHeapRegion *r = heap->get_region(tail_idx);
687 if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
688 assert(wid < n_workers, "Sanity");
689
690 size_t live_region = r->get_live_data_words();
691
692 // Select next worker that still needs live data.
693 size_t old_wid = wid;
694 do {
695 wid++;
696 if (wid == n_workers) wid = 0;
697 } while (live[wid] + live_region >= live_per_worker && old_wid != wid);
698
699 if (old_wid == wid) {
700 // Circled back to the same worker? This means liveness data was
701 // miscalculated. Bump the live_per_worker limit so that
702 // everyone gets a piece of the leftover work.
703 live_per_worker += ShenandoahHeapRegion::region_size_words();
704 }
705
706 worker_slices[wid]->add_region(r);
707 live[wid] += live_region;
708 }
709 }
710
711 FREE_C_HEAP_ARRAY(live);
712
713 #ifdef ASSERT
714 ResourceBitMap map(n_regions);
715 for (size_t wid = 0; wid < n_workers; wid++) {
716 ShenandoahHeapRegionSetIterator it(worker_slices[wid]);
717 ShenandoahHeapRegion* r = it.next();
718 while (r != nullptr) {
719 size_t idx = r->index();
720 assert(ShenandoahPrepareForCompactionTask::is_candidate_region(r), "Sanity: %zu", idx);
721 assert(!map.at(idx), "No region distributed twice: %zu", idx);
722 map.at_put(idx, true);
723 r = it.next();
724 }
725 }
726
727 for (size_t rid = 0; rid < n_regions; rid++) {
728 bool is_candidate = ShenandoahPrepareForCompactionTask::is_candidate_region(heap->get_region(rid));
729 bool is_distributed = map.at(rid);
730 assert(is_distributed || !is_candidate, "All candidates are distributed: %zu", rid);
731 }
732 #endif
733 }
734
735 void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
736 GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
737 ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
738
739 ShenandoahHeap* heap = ShenandoahHeap::heap();
740
741 // About to figure out which regions can be compacted, make sure pinning status
742 // had been updated in GC prologue.
743 heap->assert_pinned_region_status();
744
745 {
746 // Trash the immediately collectible regions before computing addresses
747 ShenandoahTrashImmediateGarbageClosure trash_immediate_garbage;
748 ShenandoahExcludeRegionClosure<FREE> cl(&trash_immediate_garbage);
749 heap->heap_region_iterate(&cl);
750
751 // Make sure regions are in good state: committed, active, clean.
752 // This is needed because we are potentially sliding the data through them.
753 ShenandoahEnsureHeapActiveClosure ecl;
754 heap->heap_region_iterate(&ecl);
755 }
756
757 // Compute the new addresses for regular objects
758 {
759 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
760
761 distribute_slices(worker_slices);
762
763 ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
764 heap->workers()->run_task(&task);
765 }
766
767 // Compute the new addresses for humongous objects
768 {
769 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);
770 calculate_target_humongous_objects();
771 }
772 }
773
774 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure {
775 private:
776 ShenandoahMarkingContext* const _ctx;
777
778 template <class T>
779 inline void do_oop_work(T* p) {
780 T o = RawAccess<>::oop_load(p);
781 if (!CompressedOops::is_null(o)) {
782 oop obj = CompressedOops::decode_not_null(o);
783 assert(_ctx->is_marked(obj), "must be marked");
784 if (FullGCForwarding::is_forwarded(obj)) {
785 oop forw = FullGCForwarding::forwardee(obj);
786 RawAccess<IS_NOT_NULL>::oop_store(p, forw);
787 }
788 }
789 }
790
791 public:
792 ShenandoahAdjustPointersClosure() :
793 _ctx(ShenandoahHeap::heap()->global_generation()->complete_marking_context()) {}
794
795 void do_oop(oop* p) { do_oop_work(p); }
796 void do_oop(narrowOop* p) { do_oop_work(p); }
797 void do_method(Method* m) {}
798 void do_nmethod(nmethod* nm) {}
799 };
800
801 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure {
802 private:
803 ShenandoahAdjustPointersClosure _cl;
804
805 public:
806 void do_object(oop p) override {
807 assert(ShenandoahHeap::heap()->global_generation()->is_mark_complete(), "marking must be complete");
808 assert(ShenandoahHeap::heap()->marking_context()->is_marked(p), "must be marked");
809 p->oop_iterate(&_cl);
810 }
811 };
812
813 class ShenandoahAdjustPointersTask : public WorkerTask {
814 private:
815 ShenandoahHeap* const _heap;
816 ShenandoahRegionIterator _regions;
817
818 public:
819 ShenandoahAdjustPointersTask() :
820 WorkerTask("Shenandoah Adjust Pointers"),
821 _heap(ShenandoahHeap::heap()) {
822 }
823
824 void work(uint worker_id) override {
825 ShenandoahParallelWorkerSession worker_session(worker_id);
826 ShenandoahAdjustPointersObjectClosure obj_cl;
827 ShenandoahHeapRegion* r = _regions.next();
828 while (r != nullptr) {
829 if (!r->is_humongous_continuation() && r->has_live()) {
830 _heap->marked_object_iterate(r, &obj_cl);
831 }
832 if (_heap->mode()->is_generational()) {
833 ShenandoahGenerationalFullGC::maybe_coalesce_and_fill_region(r);
834 }
835 r = _regions.next();
836 }
837 }
838 };
839
840 class ShenandoahAdjustRootPointersTask : public WorkerTask {
841 private:
842 ShenandoahRootAdjuster* _rp;
843 PreservedMarksSet* _preserved_marks;
844 public:
845 ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) :
846 WorkerTask("Shenandoah Adjust Root Pointers"),
847 _rp(rp),
848 _preserved_marks(preserved_marks) {}
849
850 void work(uint worker_id) override {
851 ShenandoahParallelWorkerSession worker_session(worker_id);
852 ShenandoahAdjustPointersClosure cl;
853 _rp->roots_do(worker_id, &cl);
854 _preserved_marks->get(worker_id)->adjust_during_full_gc();
855 }
856 };
857
858 void ShenandoahFullGC::phase3_update_references() {
859 GCTraceTime(Info, gc, phases) time("Phase 3: Adjust pointers", _gc_timer);
860 ShenandoahGCPhase adjust_pointer_phase(ShenandoahPhaseTimings::full_gc_adjust_pointers);
861
862 ShenandoahHeap* heap = ShenandoahHeap::heap();
863
864 WorkerThreads* workers = heap->workers();
865 uint nworkers = workers->active_workers();
866 {
867 #ifdef COMPILER2
868 DerivedPointerTable::clear();
869 #endif // COMPILER2
870 ShenandoahRootAdjuster rp(nworkers, ShenandoahPhaseTimings::full_gc_adjust_roots);
871 ShenandoahAdjustRootPointersTask task(&rp, _preserved_marks);
872 workers->run_task(&task);
873 #ifdef COMPILER2
874 DerivedPointerTable::update_pointers();
875 #endif // COMPILER2
876 }
877
878 ShenandoahAdjustPointersTask adjust_pointers_task;
879 workers->run_task(&adjust_pointers_task);
880 }
881
882 class ShenandoahCompactObjectsClosure : public ObjectClosure {
883 private:
884 uint const _worker_id;
885
886 public:
887 explicit ShenandoahCompactObjectsClosure(uint worker_id) :
888 _worker_id(worker_id) {}
889
890 void do_object(oop p) override {
891 assert(ShenandoahHeap::heap()->global_generation()->is_mark_complete(), "marking must be finished");
892 assert(ShenandoahHeap::heap()->marking_context()->is_marked(p), "must be marked");
893 size_t size = p->size();
894 if (FullGCForwarding::is_forwarded(p)) {
895 HeapWord* compact_from = cast_from_oop<HeapWord*>(p);
896 HeapWord* compact_to = cast_from_oop<HeapWord*>(FullGCForwarding::forwardee(p));
897 assert(compact_from != compact_to, "Forwarded object should move");
898 Copy::aligned_conjoint_words(compact_from, compact_to, size);
899 oop new_obj = cast_to_oop(compact_to);
900
901 // Restore the mark word before relativizing the stack chunk. The copy's
902 // mark word contains the full GC forwarding encoding, which would cause
903 // is_stackChunk() to read garbage (especially with compact headers).
904 new_obj->init_mark();
905 ContinuationGCSupport::relativize_stack_chunk(new_obj);
906 }
907 }
908 };
909
910 class ShenandoahCompactObjectsTask : public WorkerTask {
911 private:
912 ShenandoahHeap* const _heap;
913 ShenandoahHeapRegionSet** const _worker_slices;
914
915 public:
916 ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** worker_slices) :
917 WorkerTask("Shenandoah Compact Objects"),
918 _heap(ShenandoahHeap::heap()),
919 _worker_slices(worker_slices) {
920 }
921
922 void work(uint worker_id) override {
923 ShenandoahParallelWorkerSession worker_session(worker_id);
924 ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
925
926 ShenandoahCompactObjectsClosure cl(worker_id);
927 ShenandoahHeapRegion* r = slice.next();
928 while (r != nullptr) {
929 assert(!r->is_humongous(), "must not get humongous regions here");
930 if (r->has_live()) {
931 _heap->marked_object_iterate(r, &cl);
932 }
933 r->set_top(r->new_top());
934 r = slice.next();
935 }
936 }
937 };
938
939 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
940 private:
941 ShenandoahHeap* const _heap;
942 bool _is_generational;
943 size_t _young_regions, _young_usage, _young_humongous_waste;
944 size_t _old_regions, _old_usage, _old_humongous_waste;
945
946 public:
947 ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()),
948 _is_generational(_heap->mode()->is_generational()),
949 _young_regions(0),
950 _young_usage(0),
951 _young_humongous_waste(0),
952 _old_regions(0),
953 _old_usage(0),
954 _old_humongous_waste(0)
955 {
956 _heap->free_set()->clear();
957 }
958
959 void heap_region_do(ShenandoahHeapRegion* r) override {
960 assert (!r->is_cset(), "cset regions should have been demoted already");
961
962 // Need to reset the complete-top-at-mark-start pointer here because
963 // the complete marking bitmap is no longer valid. This ensures
964 // size-based iteration in marked_object_iterate().
965 // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
966 // pinned regions.
967 if (!r->is_pinned()) {
968 _heap->marking_context()->reset_top_at_mark_start(r);
969 }
970
971 size_t live = r->used();
972
973 // Make empty regions that have been allocated into regular
974 if (r->is_empty() && live > 0) {
975 if (!_is_generational) {
976 r->make_affiliated_maybe();
977 }
978 // else, generational mode compaction has already established affiliation.
979 r->make_regular_bypass();
980 if (ZapUnusedHeapArea) {
981 SpaceMangler::mangle_region(MemRegion(r->top(), r->end()));
982 }
983 }
984
985 // Reclaim regular regions that became empty
986 if (r->is_regular() && live == 0) {
987 r->make_trash();
988 }
989
990 // Recycle all trash regions
991 if (r->is_trash()) {
992 live = 0;
993 r->try_recycle_under_lock();
994 // No need to adjust_interval_for_recycled_old_region. That will be taken care of during freeset rebuild.
995 } else {
996 if (r->is_old()) {
997 ShenandoahGenerationalFullGC::account_for_region(r, _old_regions, _old_usage, _old_humongous_waste);
998 } else if (r->is_young()) {
999 ShenandoahGenerationalFullGC::account_for_region(r, _young_regions, _young_usage, _young_humongous_waste);
1000 }
1001 }
1002 r->set_live_data(live);
1003 r->reset_alloc_metadata();
1004 }
1005 };
1006
1007 void ShenandoahFullGC::compact_humongous_objects() {
1008 // Compact humongous regions, based on their fwdptr objects.
1009 //
1010 // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
1011 // humongous regions are already compacted, and do not require further moves, which alleviates
1012 // sliding costs. We may consider doing this in parallel in the future.
1013
1014 ShenandoahHeap* heap = ShenandoahHeap::heap();
1015
1016 for (size_t c = heap->num_regions(); c > 0; c--) {
1017 ShenandoahHeapRegion* r = heap->get_region(c - 1);
1018 if (r->is_humongous_start()) {
1019 oop old_obj = cast_to_oop(r->bottom());
1020 if (!FullGCForwarding::is_forwarded(old_obj)) {
1021 // No need to move the object, it stays at the same slot
1022 continue;
1023 }
1024 size_t words_size = old_obj->size();
1025 size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
1026
1027 size_t old_start = r->index();
1028 size_t old_end = old_start + num_regions - 1;
1029 size_t new_start = heap->heap_region_index_containing(FullGCForwarding::forwardee(old_obj));
1030 size_t new_end = new_start + num_regions - 1;
1031 assert(old_start != new_start, "must be real move");
1032 assert(r->is_stw_move_allowed(), "Region %zu should be movable", r->index());
1033
1034 log_debug(gc)("Full GC compaction moves humongous object from region %zu to region %zu", old_start, new_start);
1035 Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), words_size);
1036 ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom()));
1037
1038 oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
1039 new_obj->init_mark();
1040
1041 {
1042 ShenandoahAffiliation original_affiliation = r->affiliation();
1043 for (size_t c = old_start; c <= old_end; c++) {
1044 ShenandoahHeapRegion* r = heap->get_region(c);
1045 // Leave humongous region affiliation unchanged.
1046 r->make_regular_bypass();
1047 r->set_top(r->bottom());
1048 }
1049
1050 for (size_t c = new_start; c <= new_end; c++) {
1051 ShenandoahHeapRegion* r = heap->get_region(c);
1052 if (c == new_start) {
1053 r->make_humongous_start_bypass(original_affiliation);
1054 } else {
1055 r->make_humongous_cont_bypass(original_affiliation);
1056 }
1057
1058 // Trailing region may be non-full, record the remainder there
1059 size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
1060 if ((c == new_end) && (remainder != 0)) {
1061 r->set_top(r->bottom() + remainder);
1062 } else {
1063 r->set_top(r->end());
1064 }
1065
1066 r->reset_alloc_metadata();
1067 }
1068 }
1069 }
1070 }
1071 }
1072
1073 // This is slightly different to ShHeap::reset_next_mark_bitmap:
1074 // we need to remain able to walk pinned regions.
1075 // Since pinned region do not move and don't get compacted, we will get holes with
1076 // unreachable objects in them (which may have pointers to unloaded Klasses and thus
1077 // cannot be iterated over using oop->size()). The only way to safely iterate over those is using
1078 // a valid marking bitmap and valid TAMS pointer. This class only resets marking
1079 // bitmaps for un-pinned regions, and later we only reset TAMS for unpinned regions.
1080 class ShenandoahMCResetCompleteBitmapTask : public WorkerTask {
1081 private:
1082 ShenandoahRegionIterator _regions;
1083
1084 public:
1085 ShenandoahMCResetCompleteBitmapTask() :
1086 WorkerTask("Shenandoah Reset Bitmap") {
1087 }
1088
1089 void work(uint worker_id) override {
1090 ShenandoahParallelWorkerSession worker_session(worker_id);
1091 ShenandoahHeapRegion* region = _regions.next();
1092 ShenandoahHeap* heap = ShenandoahHeap::heap();
1093 ShenandoahMarkingContext* const ctx = heap->marking_context();
1094 assert(heap->global_generation()->is_mark_complete(), "Marking must be complete");
1095 while (region != nullptr) {
1096 if (heap->is_bitmap_slice_committed(region) && !region->is_pinned() && region->has_live()) {
1097 ctx->clear_bitmap(region);
1098 }
1099 region = _regions.next();
1100 }
1101 }
1102 };
1103
1104 void ShenandoahFullGC::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) {
1105 GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer);
1106 ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects);
1107
1108 ShenandoahHeap* heap = ShenandoahHeap::heap();
1109
1110 // Compact regular objects first
1111 {
1112 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular);
1113 ShenandoahCompactObjectsTask compact_task(worker_slices);
1114 heap->workers()->run_task(&compact_task);
1115 }
1116
1117 // Compact humongous objects after regular object moves
1118 {
1119 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
1120 compact_humongous_objects();
1121 }
1122 }
1123
1124 void ShenandoahFullGC::phase5_epilog() {
1125 GCTraceTime(Info, gc, phases) time("Phase 5: Full GC epilog", _gc_timer);
1126 ShenandoahHeap* heap = ShenandoahHeap::heap();
1127
1128 // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
1129 // and must ensure the bitmap is in sync.
1130 {
1131 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
1132 ShenandoahMCResetCompleteBitmapTask task;
1133 heap->workers()->run_task(&task);
1134 }
1135
1136 // Bring regions in proper states after the collection, and set heap properties.
1137 {
1138 ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
1139 ShenandoahPostCompactClosure post_compact;
1140 heap->heap_region_iterate(&post_compact);
1141 heap->collection_set()->clear();
1142 {
1143 ShenandoahFreeSet* free_set = heap->free_set();
1144 size_t young_trashed_regions, old_trashed_regions, first_old, last_old, num_old;
1145 free_set->prepare_to_rebuild(young_trashed_regions, old_trashed_regions, first_old, last_old, num_old);
1146 // We also do not expand old generation size following Full GC because we have scrambled age populations and
1147 // no longer have objects separated by age into distinct regions.
1148 if (heap->mode()->is_generational()) {
1149 ShenandoahGenerationalFullGC::compute_balances();
1150 }
1151 heap->free_set()->finish_rebuild(young_trashed_regions, old_trashed_regions, num_old);
1152 }
1153
1154 // Set mark incomplete because the marking bitmaps have been reset except pinned regions.
1155 _generation->set_mark_incomplete();
1156
1157 heap->clear_cancelled_gc();
1158 }
1159
1160 _preserved_marks->restore(heap->workers());
1161 _preserved_marks->reclaim();
1162
1163 if (heap->mode()->is_generational()) {
1164 ShenandoahGenerationalFullGC::rebuild_remembered_set(heap);
1165 }
1166 }