1 /*
2  * Copyright (c) 2014, 2020, 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 "code/codeCache.hpp"
28 #include "gc/shared/gcTraceTime.inline.hpp"
29 #include "gc/shared/preservedMarks.inline.hpp"
30 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
31 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
32 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp"
33 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
34 #include "gc/shenandoah/shenandoahFreeSet.hpp"
35 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
36 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
37 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
38 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
39 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
40 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
41 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
42 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
43 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
44 #include "gc/shenandoah/shenandoahUtils.hpp"
45 #include "gc/shenandoah/shenandoahVerifier.hpp"
46 #include "gc/shenandoah/shenandoahVMOperations.hpp"
47 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
48 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
49 #include "memory/metaspace.hpp"
50 #include "memory/universe.hpp"
51 #include "oops/compressedOops.inline.hpp"
52 #include "oops/oop.inline.hpp"
53 #include "runtime/biasedLocking.hpp"
54 #include "runtime/orderAccess.hpp"
55 #include "runtime/thread.hpp"
56 #include "utilities/copy.hpp"
57 #include "utilities/growableArray.hpp"
58 #include "gc/shared/workgroup.hpp"
59 
ShenandoahMarkCompact()60 ShenandoahMarkCompact::ShenandoahMarkCompact() :
61   _gc_timer(NULL),
62   _preserved_marks(new PreservedMarksSet(true)) {}
63 
initialize(GCTimer * gc_timer)64 void ShenandoahMarkCompact::initialize(GCTimer* gc_timer) {
65   _gc_timer = gc_timer;
66 }
67 
do_it(GCCause::Cause gc_cause)68 void ShenandoahMarkCompact::do_it(GCCause::Cause gc_cause) {
69   ShenandoahHeap* heap = ShenandoahHeap::heap();
70 
71   if (ShenandoahVerify) {
72     heap->verifier()->verify_before_fullgc();
73   }
74 
75   if (VerifyBeforeGC) {
76     Universe::verify();
77   }
78 
79   // Degenerated GC may carry concurrent root flags when upgrading to
80   // full GC. We need to reset it before mutators resume.
81   if (ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) {
82     heap->set_concurrent_strong_root_in_progress(false);
83     heap->set_concurrent_weak_root_in_progress(false);
84   }
85 
86   heap->set_full_gc_in_progress(true);
87 
88   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
89   assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped");
90 
91   {
92     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_pre);
93     heap->pre_full_gc_dump(_gc_timer);
94   }
95 
96   {
97     ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare);
98     // Full GC is supposed to recover from any GC state:
99 
100     // a0. Remember if we have forwarded objects
101     bool has_forwarded_objects = heap->has_forwarded_objects();
102 
103     // a1. Cancel evacuation, if in progress
104     if (heap->is_evacuation_in_progress()) {
105       heap->set_evacuation_in_progress(false);
106     }
107     assert(!heap->is_evacuation_in_progress(), "sanity");
108 
109     // a2. Cancel update-refs, if in progress
110     if (heap->is_update_refs_in_progress()) {
111       heap->set_update_refs_in_progress(false);
112     }
113     assert(!heap->is_update_refs_in_progress(), "sanity");
114 
115     // b. Cancel concurrent mark, if in progress
116     if (heap->is_concurrent_mark_in_progress()) {
117       heap->concurrent_mark()->cancel();
118       heap->set_concurrent_mark_in_progress(false);
119     }
120     assert(!heap->is_concurrent_mark_in_progress(), "sanity");
121 
122     // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
123     if (has_forwarded_objects) {
124       heap->concurrent_mark()->update_roots(ShenandoahPhaseTimings::full_gc_update_roots);
125     }
126 
127     // d. Reset the bitmaps for new marking
128     heap->reset_mark_bitmap();
129     assert(heap->marking_context()->is_bitmap_clear(), "sanity");
130     assert(!heap->marking_context()->is_complete(), "sanity");
131 
132     // e. Abandon reference discovery and clear all discovered references.
133     ShenandoahReferenceProcessor* rp = heap->ref_processor();
134     rp->abandon_partial_discovery();
135 
136     // f. Set back forwarded objects bit back, in case some steps above dropped it.
137     heap->set_has_forwarded_objects(has_forwarded_objects);
138 
139     // g. Sync pinned region status from the CP marks
140     heap->sync_pinned_region_status();
141 
142     // The rest of prologue:
143     BiasedLocking::preserve_marks();
144     _preserved_marks->init(heap->workers()->active_workers());
145   }
146 
147   if (UseTLAB) {
148     heap->gclabs_retire(ResizeTLAB);
149     heap->tlabs_retire(ResizeTLAB);
150   }
151 
152   OrderAccess::fence();
153 
154   phase1_mark_heap();
155 
156   // Once marking is done, which may have fixed up forwarded objects, we can drop it.
157   // Coming out of Full GC, we would not have any forwarded objects.
158   // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
159   heap->set_has_forwarded_objects(false);
160 
161   heap->set_full_gc_move_in_progress(true);
162 
163   // Setup workers for the rest
164   OrderAccess::fence();
165 
166   // Initialize worker slices
167   ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
168   for (uint i = 0; i < heap->max_workers(); i++) {
169     worker_slices[i] = new ShenandoahHeapRegionSet();
170   }
171 
172   {
173     // The rest of code performs region moves, where region status is undefined
174     // until all phases run together.
175     ShenandoahHeapLocker lock(heap->lock());
176 
177     phase2_calculate_target_addresses(worker_slices);
178 
179     OrderAccess::fence();
180 
181     phase3_update_references();
182 
183     phase4_compact_objects(worker_slices);
184   }
185 
186   {
187     // Epilogue
188     _preserved_marks->restore(heap->workers());
189     BiasedLocking::restore_marks();
190     _preserved_marks->reclaim();
191   }
192 
193   // Resize metaspace
194   MetaspaceGC::compute_new_size();
195 
196   // Free worker slices
197   for (uint i = 0; i < heap->max_workers(); i++) {
198     delete worker_slices[i];
199   }
200   FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
201 
202   heap->set_full_gc_move_in_progress(false);
203   heap->set_full_gc_in_progress(false);
204 
205   if (ShenandoahVerify) {
206     heap->verifier()->verify_after_fullgc();
207   }
208 
209   if (VerifyAfterGC) {
210     Universe::verify();
211   }
212 
213   {
214     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_post);
215     heap->post_full_gc_dump(_gc_timer);
216   }
217 }
218 
219 class ShenandoahPrepareForMarkClosure: public ShenandoahHeapRegionClosure {
220 private:
221   ShenandoahMarkingContext* const _ctx;
222 
223 public:
ShenandoahPrepareForMarkClosure()224   ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
225 
heap_region_do(ShenandoahHeapRegion * r)226   void heap_region_do(ShenandoahHeapRegion *r) {
227     _ctx->capture_top_at_mark_start(r);
228     r->clear_live_data();
229   }
230 };
231 
phase1_mark_heap()232 void ShenandoahMarkCompact::phase1_mark_heap() {
233   GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
234   ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
235 
236   ShenandoahHeap* heap = ShenandoahHeap::heap();
237 
238   ShenandoahPrepareForMarkClosure cl;
239   heap->heap_region_iterate(&cl);
240 
241   ShenandoahConcurrentMark* cm = heap->concurrent_mark();
242 
243   heap->set_unload_classes(heap->heuristics()->can_unload_classes());
244 
245   ShenandoahReferenceProcessor* rp = heap->ref_processor();
246   // enable ("weak") refs discovery
247   rp->set_soft_reference_policy(true); // forcefully purge all soft references
248 
249   cm->mark_roots(ShenandoahPhaseTimings::full_gc_scan_roots);
250   cm->finish_mark_from_roots(/* full_gc = */ true);
251   heap->mark_complete_marking_context();
252   heap->parallel_cleaning(true /* full_gc */);
253 }
254 
255 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
256 private:
257   PreservedMarks*          const _preserved_marks;
258   ShenandoahHeap*          const _heap;
259   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
260   int _empty_regions_pos;
261   ShenandoahHeapRegion*          _to_region;
262   ShenandoahHeapRegion*          _from_region;
263   HeapWord* _compact_point;
264 
265 public:
ShenandoahPrepareForCompactionObjectClosure(PreservedMarks * preserved_marks,GrowableArray<ShenandoahHeapRegion * > & empty_regions,ShenandoahHeapRegion * to_region)266   ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks,
267                                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
268                                               ShenandoahHeapRegion* to_region) :
269     _preserved_marks(preserved_marks),
270     _heap(ShenandoahHeap::heap()),
271     _empty_regions(empty_regions),
272     _empty_regions_pos(0),
273     _to_region(to_region),
274     _from_region(NULL),
275     _compact_point(to_region->bottom()) {}
276 
set_from_region(ShenandoahHeapRegion * from_region)277   void set_from_region(ShenandoahHeapRegion* from_region) {
278     _from_region = from_region;
279   }
280 
finish_region()281   void finish_region() {
282     assert(_to_region != NULL, "should not happen");
283     _to_region->set_new_top(_compact_point);
284   }
285 
is_compact_same_region()286   bool is_compact_same_region() {
287     return _from_region == _to_region;
288   }
289 
empty_regions_pos()290   int empty_regions_pos() {
291     return _empty_regions_pos;
292   }
293 
do_object(oop p)294   void do_object(oop p) {
295     assert(_from_region != NULL, "must set before work");
296     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
297     assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
298 
299     size_t obj_size = p->size();
300     if (_compact_point + obj_size > _to_region->end()) {
301       finish_region();
302 
303       // Object doesn't fit. Pick next empty region and start compacting there.
304       ShenandoahHeapRegion* new_to_region;
305       if (_empty_regions_pos < _empty_regions.length()) {
306         new_to_region = _empty_regions.at(_empty_regions_pos);
307         _empty_regions_pos++;
308       } else {
309         // Out of empty region? Compact within the same region.
310         new_to_region = _from_region;
311       }
312 
313       assert(new_to_region != _to_region, "must not reuse same to-region");
314       assert(new_to_region != NULL, "must not be NULL");
315       _to_region = new_to_region;
316       _compact_point = _to_region->bottom();
317     }
318 
319     // Object fits into current region, record new location:
320     assert(_compact_point + obj_size <= _to_region->end(), "must fit");
321     shenandoah_assert_not_forwarded(NULL, p);
322     _preserved_marks->push_if_necessary(p, p->mark());
323     p->forward_to(oop(_compact_point));
324     _compact_point += obj_size;
325   }
326 };
327 
328 class ShenandoahPrepareForCompactionTask : public AbstractGangTask {
329 private:
330   PreservedMarksSet*        const _preserved_marks;
331   ShenandoahHeap*           const _heap;
332   ShenandoahHeapRegionSet** const _worker_slices;
333 
334 public:
ShenandoahPrepareForCompactionTask(PreservedMarksSet * preserved_marks,ShenandoahHeapRegionSet ** worker_slices)335   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :
336     AbstractGangTask("Shenandoah Prepare For Compaction"),
337     _preserved_marks(preserved_marks),
338     _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
339   }
340 
is_candidate_region(ShenandoahHeapRegion * r)341   static bool is_candidate_region(ShenandoahHeapRegion* r) {
342     // Empty region: get it into the slice to defragment the slice itself.
343     // We could have skipped this without violating correctness, but we really
344     // want to compact all live regions to the start of the heap, which sometimes
345     // means moving them into the fully empty regions.
346     if (r->is_empty()) return true;
347 
348     // Can move the region, and this is not the humongous region. Humongous
349     // moves are special cased here, because their moves are handled separately.
350     return r->is_stw_move_allowed() && !r->is_humongous();
351   }
352 
work(uint worker_id)353   void work(uint worker_id) {
354     ShenandoahParallelWorkerSession worker_session(worker_id);
355     ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
356     ShenandoahHeapRegionSetIterator it(slice);
357     ShenandoahHeapRegion* from_region = it.next();
358     // No work?
359     if (from_region == NULL) {
360        return;
361     }
362 
363     // Sliding compaction. Walk all regions in the slice, and compact them.
364     // Remember empty regions and reuse them as needed.
365     ResourceMark rm;
366 
367     GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
368 
369     ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
370 
371     while (from_region != NULL) {
372       assert(is_candidate_region(from_region), "Sanity");
373 
374       cl.set_from_region(from_region);
375       if (from_region->has_live()) {
376         _heap->marked_object_iterate(from_region, &cl);
377       }
378 
379       // Compacted the region to somewhere else? From-region is empty then.
380       if (!cl.is_compact_same_region()) {
381         empty_regions.append(from_region);
382       }
383       from_region = it.next();
384     }
385     cl.finish_region();
386 
387     // Mark all remaining regions as empty
388     for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
389       ShenandoahHeapRegion* r = empty_regions.at(pos);
390       r->set_new_top(r->bottom());
391     }
392   }
393 };
394 
calculate_target_humongous_objects()395 void ShenandoahMarkCompact::calculate_target_humongous_objects() {
396   ShenandoahHeap* heap = ShenandoahHeap::heap();
397 
398   // Compute the new addresses for humongous objects. We need to do this after addresses
399   // for regular objects are calculated, and we know what regions in heap suffix are
400   // available for humongous moves.
401   //
402   // Scan the heap backwards, because we are compacting humongous regions towards the end.
403   // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
404   // humongous start there.
405   //
406   // The complication is potential non-movable regions during the scan. If such region is
407   // detected, then sliding restarts towards that non-movable region.
408 
409   size_t to_begin = heap->num_regions();
410   size_t to_end = heap->num_regions();
411 
412   for (size_t c = heap->num_regions(); c > 0; c--) {
413     ShenandoahHeapRegion *r = heap->get_region(c - 1);
414     if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
415       // To-region candidate: record this, and continue scan
416       to_begin = r->index();
417       continue;
418     }
419 
420     if (r->is_humongous_start() && r->is_stw_move_allowed()) {
421       // From-region candidate: movable humongous region
422       oop old_obj = oop(r->bottom());
423       size_t words_size = old_obj->size();
424       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
425 
426       size_t start = to_end - num_regions;
427 
428       if (start >= to_begin && start != r->index()) {
429         // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
430         _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark());
431         old_obj->forward_to(oop(heap->get_region(start)->bottom()));
432         to_end = start;
433         continue;
434       }
435     }
436 
437     // Failed to fit. Scan starting from current region.
438     to_begin = r->index();
439     to_end = r->index();
440   }
441 }
442 
443 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
444 private:
445   ShenandoahHeap* const _heap;
446 
447 public:
ShenandoahEnsureHeapActiveClosure()448   ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {}
heap_region_do(ShenandoahHeapRegion * r)449   void heap_region_do(ShenandoahHeapRegion* r) {
450     if (r->is_trash()) {
451       r->recycle();
452     }
453     if (r->is_cset()) {
454       r->make_regular_bypass();
455     }
456     if (r->is_empty_uncommitted()) {
457       r->make_committed_bypass();
458     }
459     assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->index());
460 
461     // Record current region occupancy: this communicates empty regions are free
462     // to the rest of Full GC code.
463     r->set_new_top(r->top());
464   }
465 };
466 
467 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure {
468 private:
469   ShenandoahHeap* const _heap;
470   ShenandoahMarkingContext* const _ctx;
471 
472 public:
ShenandoahTrashImmediateGarbageClosure()473   ShenandoahTrashImmediateGarbageClosure() :
474     _heap(ShenandoahHeap::heap()),
475     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
476 
heap_region_do(ShenandoahHeapRegion * r)477   void heap_region_do(ShenandoahHeapRegion* r) {
478     if (r->is_humongous_start()) {
479       oop humongous_obj = oop(r->bottom());
480       if (!_ctx->is_marked(humongous_obj)) {
481         assert(!r->has_live(),
482                "Region " SIZE_FORMAT " is not marked, should not have live", r->index());
483         _heap->trash_humongous_region_at(r);
484       } else {
485         assert(r->has_live(),
486                "Region " SIZE_FORMAT " should have live", r->index());
487       }
488     } else if (r->is_humongous_continuation()) {
489       // If we hit continuation, the non-live humongous starts should have been trashed already
490       assert(r->humongous_start_region()->has_live(),
491              "Region " SIZE_FORMAT " should have live", r->index());
492     } else if (r->is_regular()) {
493       if (!r->has_live()) {
494         r->make_trash_immediate();
495       }
496     }
497   }
498 };
499 
distribute_slices(ShenandoahHeapRegionSet ** worker_slices)500 void ShenandoahMarkCompact::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
501   ShenandoahHeap* heap = ShenandoahHeap::heap();
502 
503   uint n_workers = heap->workers()->active_workers();
504   size_t n_regions = heap->num_regions();
505 
506   // What we want to accomplish: have the dense prefix of data, while still balancing
507   // out the parallel work.
508   //
509   // Assuming the amount of work is driven by the live data that needs moving, we can slice
510   // the entire heap into equal-live-sized prefix slices, and compact into them. So, each
511   // thread takes all regions in its prefix subset, and then it takes some regions from
512   // the tail.
513   //
514   // Tail region selection becomes interesting.
515   //
516   // First, we want to distribute the regions fairly between the workers, and those regions
517   // might have different amount of live data. So, until we sure no workers need live data,
518   // we need to only take what the worker needs.
519   //
520   // Second, since we slide everything to the left in each slice, the most busy regions
521   // would be the ones on the left. Which means we want to have all workers have their after-tail
522   // regions as close to the left as possible.
523   //
524   // The easiest way to do this is to distribute after-tail regions in round-robin between
525   // workers that still need live data.
526   //
527   // Consider parallel workers A, B, C, then the target slice layout would be:
528   //
529   //  AAAAAAAABBBBBBBBCCCCCCCC|ABCABCABCABCABCABCABCABABABABABABABABABABAAAAA
530   //
531   //  (.....dense-prefix.....) (.....................tail...................)
532   //  [all regions fully live] [left-most regions are fuller that right-most]
533   //
534 
535   // Compute how much live data is there. This would approximate the size of dense prefix
536   // we target to create.
537   size_t total_live = 0;
538   for (size_t idx = 0; idx < n_regions; idx++) {
539     ShenandoahHeapRegion *r = heap->get_region(idx);
540     if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
541       total_live += r->get_live_data_words();
542     }
543   }
544 
545   // Estimate the size for the dense prefix. Note that we specifically count only the
546   // "full" regions, so there would be some non-full regions in the slice tail.
547   size_t live_per_worker = total_live / n_workers;
548   size_t prefix_regions_per_worker = live_per_worker / ShenandoahHeapRegion::region_size_words();
549   size_t prefix_regions_total = prefix_regions_per_worker * n_workers;
550   prefix_regions_total = MIN2(prefix_regions_total, n_regions);
551   assert(prefix_regions_total <= n_regions, "Sanity");
552 
553   // There might be non-candidate regions in the prefix. To compute where the tail actually
554   // ends up being, we need to account those as well.
555   size_t prefix_end = prefix_regions_total;
556   for (size_t idx = 0; idx < prefix_regions_total; idx++) {
557     ShenandoahHeapRegion *r = heap->get_region(idx);
558     if (!ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
559       prefix_end++;
560     }
561   }
562   prefix_end = MIN2(prefix_end, n_regions);
563   assert(prefix_end <= n_regions, "Sanity");
564 
565   // Distribute prefix regions per worker: each thread definitely gets its own same-sized
566   // subset of dense prefix.
567   size_t prefix_idx = 0;
568 
569   size_t* live = NEW_C_HEAP_ARRAY(size_t, n_workers, mtGC);
570 
571   for (size_t wid = 0; wid < n_workers; wid++) {
572     ShenandoahHeapRegionSet* slice = worker_slices[wid];
573 
574     live[wid] = 0;
575     size_t regs = 0;
576 
577     // Add all prefix regions for this worker
578     while (prefix_idx < prefix_end && regs < prefix_regions_per_worker) {
579       ShenandoahHeapRegion *r = heap->get_region(prefix_idx);
580       if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
581         slice->add_region(r);
582         live[wid] += r->get_live_data_words();
583         regs++;
584       }
585       prefix_idx++;
586     }
587   }
588 
589   // Distribute the tail among workers in round-robin fashion.
590   size_t wid = n_workers - 1;
591 
592   for (size_t tail_idx = prefix_end; tail_idx < n_regions; tail_idx++) {
593     ShenandoahHeapRegion *r = heap->get_region(tail_idx);
594     if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
595       assert(wid < n_workers, "Sanity");
596 
597       size_t live_region = r->get_live_data_words();
598 
599       // Select next worker that still needs live data.
600       size_t old_wid = wid;
601       do {
602         wid++;
603         if (wid == n_workers) wid = 0;
604       } while (live[wid] + live_region >= live_per_worker && old_wid != wid);
605 
606       if (old_wid == wid) {
607         // Circled back to the same worker? This means liveness data was
608         // miscalculated. Bump the live_per_worker limit so that
609         // everyone gets a piece of the leftover work.
610         live_per_worker += ShenandoahHeapRegion::region_size_words();
611       }
612 
613       worker_slices[wid]->add_region(r);
614       live[wid] += live_region;
615     }
616   }
617 
618   FREE_C_HEAP_ARRAY(size_t, live);
619 
620 #ifdef ASSERT
621   ResourceBitMap map(n_regions);
622   for (size_t wid = 0; wid < n_workers; wid++) {
623     ShenandoahHeapRegionSetIterator it(worker_slices[wid]);
624     ShenandoahHeapRegion* r = it.next();
625     while (r != NULL) {
626       size_t idx = r->index();
627       assert(ShenandoahPrepareForCompactionTask::is_candidate_region(r), "Sanity: " SIZE_FORMAT, idx);
628       assert(!map.at(idx), "No region distributed twice: " SIZE_FORMAT, idx);
629       map.at_put(idx, true);
630       r = it.next();
631     }
632   }
633 
634   for (size_t rid = 0; rid < n_regions; rid++) {
635     bool is_candidate = ShenandoahPrepareForCompactionTask::is_candidate_region(heap->get_region(rid));
636     bool is_distributed = map.at(rid);
637     assert(is_distributed || !is_candidate, "All candidates are distributed: " SIZE_FORMAT, rid);
638   }
639 #endif
640 }
641 
phase2_calculate_target_addresses(ShenandoahHeapRegionSet ** worker_slices)642 void ShenandoahMarkCompact::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
643   GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
644   ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
645 
646   ShenandoahHeap* heap = ShenandoahHeap::heap();
647 
648   // About to figure out which regions can be compacted, make sure pinning status
649   // had been updated in GC prologue.
650   heap->assert_pinned_region_status();
651 
652   {
653     // Trash the immediately collectible regions before computing addresses
654     ShenandoahTrashImmediateGarbageClosure tigcl;
655     heap->heap_region_iterate(&tigcl);
656 
657     // Make sure regions are in good state: committed, active, clean.
658     // This is needed because we are potentially sliding the data through them.
659     ShenandoahEnsureHeapActiveClosure ecl;
660     heap->heap_region_iterate(&ecl);
661   }
662 
663   // Compute the new addresses for regular objects
664   {
665     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
666 
667     distribute_slices(worker_slices);
668 
669     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
670     heap->workers()->run_task(&task);
671   }
672 
673   // Compute the new addresses for humongous objects
674   {
675     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);
676     calculate_target_humongous_objects();
677   }
678 }
679 
680 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure {
681 private:
682   ShenandoahHeap* const _heap;
683   ShenandoahMarkingContext* const _ctx;
684 
685   template <class T>
do_oop_work(T * p)686   inline void do_oop_work(T* p) {
687     T o = RawAccess<>::oop_load(p);
688     if (!CompressedOops::is_null(o)) {
689       oop obj = CompressedOops::decode_not_null(o);
690       assert(_ctx->is_marked(obj), "must be marked");
691       if (obj->is_forwarded()) {
692         oop forw = obj->forwardee();
693         RawAccess<IS_NOT_NULL>::oop_store(p, forw);
694       }
695     }
696   }
697 
698 public:
ShenandoahAdjustPointersClosure()699   ShenandoahAdjustPointersClosure() :
700     _heap(ShenandoahHeap::heap()),
701     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
702 
do_oop(oop * p)703   void do_oop(oop* p)       { do_oop_work(p); }
do_oop(narrowOop * p)704   void do_oop(narrowOop* p) { do_oop_work(p); }
705 };
706 
707 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure {
708 private:
709   ShenandoahHeap* const _heap;
710   ShenandoahAdjustPointersClosure _cl;
711 
712 public:
ShenandoahAdjustPointersObjectClosure()713   ShenandoahAdjustPointersObjectClosure() :
714     _heap(ShenandoahHeap::heap()) {
715   }
do_object(oop p)716   void do_object(oop p) {
717     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
718     p->oop_iterate(&_cl);
719   }
720 };
721 
722 class ShenandoahAdjustPointersTask : public AbstractGangTask {
723 private:
724   ShenandoahHeap*          const _heap;
725   ShenandoahRegionIterator       _regions;
726 
727 public:
ShenandoahAdjustPointersTask()728   ShenandoahAdjustPointersTask() :
729     AbstractGangTask("Shenandoah Adjust Pointers"),
730     _heap(ShenandoahHeap::heap()) {
731   }
732 
work(uint worker_id)733   void work(uint worker_id) {
734     ShenandoahParallelWorkerSession worker_session(worker_id);
735     ShenandoahAdjustPointersObjectClosure obj_cl;
736     ShenandoahHeapRegion* r = _regions.next();
737     while (r != NULL) {
738       if (!r->is_humongous_continuation() && r->has_live()) {
739         _heap->marked_object_iterate(r, &obj_cl);
740       }
741       r = _regions.next();
742     }
743   }
744 };
745 
746 class ShenandoahAdjustRootPointersTask : public AbstractGangTask {
747 private:
748   ShenandoahRootAdjuster* _rp;
749   PreservedMarksSet* _preserved_marks;
750 public:
ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster * rp,PreservedMarksSet * preserved_marks)751   ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) :
752     AbstractGangTask("Shenandoah Adjust Root Pointers"),
753     _rp(rp),
754     _preserved_marks(preserved_marks) {}
755 
work(uint worker_id)756   void work(uint worker_id) {
757     ShenandoahParallelWorkerSession worker_session(worker_id);
758     ShenandoahAdjustPointersClosure cl;
759     _rp->roots_do(worker_id, &cl);
760     _preserved_marks->get(worker_id)->adjust_during_full_gc();
761   }
762 };
763 
phase3_update_references()764 void ShenandoahMarkCompact::phase3_update_references() {
765   GCTraceTime(Info, gc, phases) time("Phase 3: Adjust pointers", _gc_timer);
766   ShenandoahGCPhase adjust_pointer_phase(ShenandoahPhaseTimings::full_gc_adjust_pointers);
767 
768   ShenandoahHeap* heap = ShenandoahHeap::heap();
769 
770   WorkGang* workers = heap->workers();
771   uint nworkers = workers->active_workers();
772   {
773 #if COMPILER2_OR_JVMCI
774     DerivedPointerTable::clear();
775 #endif
776     ShenandoahRootAdjuster rp(nworkers, ShenandoahPhaseTimings::full_gc_adjust_roots);
777     ShenandoahAdjustRootPointersTask task(&rp, _preserved_marks);
778     workers->run_task(&task);
779 #if COMPILER2_OR_JVMCI
780     DerivedPointerTable::update_pointers();
781 #endif
782   }
783 
784   ShenandoahAdjustPointersTask adjust_pointers_task;
785   workers->run_task(&adjust_pointers_task);
786 }
787 
788 class ShenandoahCompactObjectsClosure : public ObjectClosure {
789 private:
790   ShenandoahHeap* const _heap;
791   uint            const _worker_id;
792 
793 public:
ShenandoahCompactObjectsClosure(uint worker_id)794   ShenandoahCompactObjectsClosure(uint worker_id) :
795     _heap(ShenandoahHeap::heap()), _worker_id(worker_id) {}
796 
do_object(oop p)797   void do_object(oop p) {
798     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
799     size_t size = (size_t)p->size();
800     if (p->is_forwarded()) {
801       HeapWord* compact_from = cast_from_oop<HeapWord*>(p);
802       HeapWord* compact_to = cast_from_oop<HeapWord*>(p->forwardee());
803       Copy::aligned_conjoint_words(compact_from, compact_to, size);
804       oop new_obj = oop(compact_to);
805       new_obj->init_mark();
806     }
807   }
808 };
809 
810 class ShenandoahCompactObjectsTask : public AbstractGangTask {
811 private:
812   ShenandoahHeap* const _heap;
813   ShenandoahHeapRegionSet** const _worker_slices;
814 
815 public:
ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet ** worker_slices)816   ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** worker_slices) :
817     AbstractGangTask("Shenandoah Compact Objects"),
818     _heap(ShenandoahHeap::heap()),
819     _worker_slices(worker_slices) {
820   }
821 
work(uint worker_id)822   void work(uint worker_id) {
823     ShenandoahParallelWorkerSession worker_session(worker_id);
824     ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
825 
826     ShenandoahCompactObjectsClosure cl(worker_id);
827     ShenandoahHeapRegion* r = slice.next();
828     while (r != NULL) {
829       assert(!r->is_humongous(), "must not get humongous regions here");
830       if (r->has_live()) {
831         _heap->marked_object_iterate(r, &cl);
832       }
833       r->set_top(r->new_top());
834       r = slice.next();
835     }
836   }
837 };
838 
839 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
840 private:
841   ShenandoahHeap* const _heap;
842   size_t _live;
843 
844 public:
ShenandoahPostCompactClosure()845   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()), _live(0) {
846     _heap->free_set()->clear();
847   }
848 
heap_region_do(ShenandoahHeapRegion * r)849   void heap_region_do(ShenandoahHeapRegion* r) {
850     assert (!r->is_cset(), "cset regions should have been demoted already");
851 
852     // Need to reset the complete-top-at-mark-start pointer here because
853     // the complete marking bitmap is no longer valid. This ensures
854     // size-based iteration in marked_object_iterate().
855     // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
856     // pinned regions.
857     if (!r->is_pinned()) {
858       _heap->complete_marking_context()->reset_top_at_mark_start(r);
859     }
860 
861     size_t live = r->used();
862 
863     // Make empty regions that have been allocated into regular
864     if (r->is_empty() && live > 0) {
865       r->make_regular_bypass();
866     }
867 
868     // Reclaim regular regions that became empty
869     if (r->is_regular() && live == 0) {
870       r->make_trash();
871     }
872 
873     // Recycle all trash regions
874     if (r->is_trash()) {
875       live = 0;
876       r->recycle();
877     }
878 
879     r->set_live_data(live);
880     r->reset_alloc_metadata();
881     _live += live;
882   }
883 
get_live()884   size_t get_live() {
885     return _live;
886   }
887 };
888 
compact_humongous_objects()889 void ShenandoahMarkCompact::compact_humongous_objects() {
890   // Compact humongous regions, based on their fwdptr objects.
891   //
892   // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
893   // humongous regions are already compacted, and do not require further moves, which alleviates
894   // sliding costs. We may consider doing this in parallel in future.
895 
896   ShenandoahHeap* heap = ShenandoahHeap::heap();
897 
898   for (size_t c = heap->num_regions(); c > 0; c--) {
899     ShenandoahHeapRegion* r = heap->get_region(c - 1);
900     if (r->is_humongous_start()) {
901       oop old_obj = oop(r->bottom());
902       if (!old_obj->is_forwarded()) {
903         // No need to move the object, it stays at the same slot
904         continue;
905       }
906       size_t words_size = old_obj->size();
907       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
908 
909       size_t old_start = r->index();
910       size_t old_end   = old_start + num_regions - 1;
911       size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
912       size_t new_end   = new_start + num_regions - 1;
913       assert(old_start != new_start, "must be real move");
914       assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
915 
916       Copy::aligned_conjoint_words(heap->get_region(old_start)->bottom(),
917                                    heap->get_region(new_start)->bottom(),
918                                    words_size);
919 
920       oop new_obj = oop(heap->get_region(new_start)->bottom());
921       new_obj->init_mark();
922 
923       {
924         for (size_t c = old_start; c <= old_end; c++) {
925           ShenandoahHeapRegion* r = heap->get_region(c);
926           r->make_regular_bypass();
927           r->set_top(r->bottom());
928         }
929 
930         for (size_t c = new_start; c <= new_end; c++) {
931           ShenandoahHeapRegion* r = heap->get_region(c);
932           if (c == new_start) {
933             r->make_humongous_start_bypass();
934           } else {
935             r->make_humongous_cont_bypass();
936           }
937 
938           // Trailing region may be non-full, record the remainder there
939           size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
940           if ((c == new_end) && (remainder != 0)) {
941             r->set_top(r->bottom() + remainder);
942           } else {
943             r->set_top(r->end());
944           }
945 
946           r->reset_alloc_metadata();
947         }
948       }
949     }
950   }
951 }
952 
953 // This is slightly different to ShHeap::reset_next_mark_bitmap:
954 // we need to remain able to walk pinned regions.
955 // Since pinned region do not move and don't get compacted, we will get holes with
956 // unreachable objects in them (which may have pointers to unloaded Klasses and thus
957 // cannot be iterated over using oop->size(). The only way to safely iterate over those is using
958 // a valid marking bitmap and valid TAMS pointer. This class only resets marking
959 // bitmaps for un-pinned regions, and later we only reset TAMS for unpinned regions.
960 class ShenandoahMCResetCompleteBitmapTask : public AbstractGangTask {
961 private:
962   ShenandoahRegionIterator _regions;
963 
964 public:
ShenandoahMCResetCompleteBitmapTask()965   ShenandoahMCResetCompleteBitmapTask() :
966     AbstractGangTask("Shenandoah Reset Bitmap") {
967   }
968 
work(uint worker_id)969   void work(uint worker_id) {
970     ShenandoahParallelWorkerSession worker_session(worker_id);
971     ShenandoahHeapRegion* region = _regions.next();
972     ShenandoahHeap* heap = ShenandoahHeap::heap();
973     ShenandoahMarkingContext* const ctx = heap->complete_marking_context();
974     while (region != NULL) {
975       if (heap->is_bitmap_slice_committed(region) && !region->is_pinned() && region->has_live()) {
976         ctx->clear_bitmap(region);
977       }
978       region = _regions.next();
979     }
980   }
981 };
982 
phase4_compact_objects(ShenandoahHeapRegionSet ** worker_slices)983 void ShenandoahMarkCompact::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) {
984   GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer);
985   ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects);
986 
987   ShenandoahHeap* heap = ShenandoahHeap::heap();
988 
989   // Compact regular objects first
990   {
991     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular);
992     ShenandoahCompactObjectsTask compact_task(worker_slices);
993     heap->workers()->run_task(&compact_task);
994   }
995 
996   // Compact humongous objects after regular object moves
997   {
998     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
999     compact_humongous_objects();
1000   }
1001 
1002   // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
1003   // and must ensure the bitmap is in sync.
1004   {
1005     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
1006     ShenandoahMCResetCompleteBitmapTask task;
1007     heap->workers()->run_task(&task);
1008   }
1009 
1010   // Bring regions in proper states after the collection, and set heap properties.
1011   {
1012     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
1013 
1014     ShenandoahPostCompactClosure post_compact;
1015     heap->heap_region_iterate(&post_compact);
1016     heap->set_used(post_compact.get_live());
1017 
1018     heap->collection_set()->clear();
1019     heap->free_set()->rebuild();
1020   }
1021 
1022   heap->clear_cancelled_gc();
1023 }
1024