< prev index next >

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

Print this page

  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 "compiler/oopMap.hpp"
  28 #include "gc/shared/continuationGCSupport.hpp"
  29 #include "gc/shared/gcTraceTime.inline.hpp"
  30 #include "gc/shared/preservedMarks.inline.hpp"

  31 #include "gc/shared/tlab_globals.hpp"
  32 #include "gc/shared/workerThread.hpp"
  33 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  34 #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
  35 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  36 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  37 #include "gc/shenandoah/shenandoahFullGC.hpp"
  38 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  39 #include "gc/shenandoah/shenandoahMark.inline.hpp"
  40 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  41 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  42 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  43 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  44 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  45 #include "gc/shenandoah/shenandoahMetrics.hpp"
  46 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  47 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
  48 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  49 #include "gc/shenandoah/shenandoahSTWMark.hpp"
  50 #include "gc/shenandoah/shenandoahUtils.hpp"

 204   // Coming out of Full GC, we would not have any forwarded objects.
 205   // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
 206   heap->set_has_forwarded_objects(false);
 207 
 208   heap->set_full_gc_move_in_progress(true);
 209 
 210   // Setup workers for the rest
 211   OrderAccess::fence();
 212 
 213   // Initialize worker slices
 214   ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
 215   for (uint i = 0; i < heap->max_workers(); i++) {
 216     worker_slices[i] = new ShenandoahHeapRegionSet();
 217   }
 218 
 219   {
 220     // The rest of code performs region moves, where region status is undefined
 221     // until all phases run together.
 222     ShenandoahHeapLocker lock(heap->lock());
 223 


 224     phase2_calculate_target_addresses(worker_slices);
 225 
 226     OrderAccess::fence();
 227 
 228     phase3_update_references();
 229 
 230     phase4_compact_objects(worker_slices);
 231   }
 232 
 233   {
 234     // Epilogue
 235     _preserved_marks->restore(heap->workers());
 236     _preserved_marks->reclaim();

 237   }
 238 
 239   // Resize metaspace
 240   MetaspaceGC::compute_new_size();
 241 
 242   // Free worker slices
 243   for (uint i = 0; i < heap->max_workers(); i++) {
 244     delete worker_slices[i];
 245   }
 246   FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
 247 
 248   heap->set_full_gc_move_in_progress(false);
 249   heap->set_full_gc_in_progress(false);
 250 
 251   if (ShenandoahVerify) {
 252     heap->verifier()->verify_after_fullgc();
 253   }
 254 
 255   if (VerifyAfterGC) {
 256     Universe::verify();

 278 void ShenandoahFullGC::phase1_mark_heap() {
 279   GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
 280   ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
 281 
 282   ShenandoahHeap* heap = ShenandoahHeap::heap();
 283 
 284   ShenandoahPrepareForMarkClosure cl;
 285   heap->heap_region_iterate(&cl);
 286 
 287   heap->set_unload_classes(heap->heuristics()->can_unload_classes());
 288 
 289   ShenandoahReferenceProcessor* rp = heap->ref_processor();
 290   // enable ("weak") refs discovery
 291   rp->set_soft_reference_policy(true); // forcefully purge all soft references
 292 
 293   ShenandoahSTWMark mark(true /*full_gc*/);
 294   mark.mark();
 295   heap->parallel_cleaning(true /* full_gc */);
 296 }
 297 

 298 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
 299 private:
 300   PreservedMarks*          const _preserved_marks;
 301   ShenandoahHeap*          const _heap;
 302   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 303   int _empty_regions_pos;
 304   ShenandoahHeapRegion*          _to_region;
 305   ShenandoahHeapRegion*          _from_region;
 306   HeapWord* _compact_point;
 307 
 308 public:
 309   ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks,
 310                                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 311                                               ShenandoahHeapRegion* to_region) :
 312     _preserved_marks(preserved_marks),
 313     _heap(ShenandoahHeap::heap()),
 314     _empty_regions(empty_regions),
 315     _empty_regions_pos(0),
 316     _to_region(to_region),
 317     _from_region(nullptr),

 347       ShenandoahHeapRegion* new_to_region;
 348       if (_empty_regions_pos < _empty_regions.length()) {
 349         new_to_region = _empty_regions.at(_empty_regions_pos);
 350         _empty_regions_pos++;
 351       } else {
 352         // Out of empty region? Compact within the same region.
 353         new_to_region = _from_region;
 354       }
 355 
 356       assert(new_to_region != _to_region, "must not reuse same to-region");
 357       assert(new_to_region != nullptr, "must not be null");
 358       _to_region = new_to_region;
 359       _compact_point = _to_region->bottom();
 360     }
 361 
 362     // Object fits into current region, record new location, if object does not move:
 363     assert(_compact_point + obj_size <= _to_region->end(), "must fit");
 364     shenandoah_assert_not_forwarded(nullptr, p);
 365     if (_compact_point != cast_from_oop<HeapWord*>(p)) {
 366       _preserved_marks->push_if_necessary(p, p->mark());
 367       p->forward_to(cast_to_oop(_compact_point));
 368     }
 369     _compact_point += obj_size;
 370   }
 371 };
 372 
 373 class ShenandoahPrepareForCompactionTask : public WorkerTask {
 374 private:
 375   PreservedMarksSet*        const _preserved_marks;
 376   ShenandoahHeap*           const _heap;
 377   ShenandoahHeapRegionSet** const _worker_slices;
 378 
 379 public:
 380   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :
 381     WorkerTask("Shenandoah Prepare For Compaction"),
 382     _preserved_marks(preserved_marks),
 383     _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
 384   }
 385 
 386   static bool is_candidate_region(ShenandoahHeapRegion* r) {
 387     // Empty region: get it into the slice to defragment the slice itself.
 388     // We could have skipped this without violating correctness, but we really
 389     // want to compact all live regions to the start of the heap, which sometimes
 390     // means moving them into the fully empty regions.
 391     if (r->is_empty()) return true;
 392 
 393     // Can move the region, and this is not the humongous region. Humongous
 394     // moves are special cased here, because their moves are handled separately.
 395     return r->is_stw_move_allowed() && !r->is_humongous();
 396   }
 397 
 398   void work(uint worker_id) {










 399     ShenandoahParallelWorkerSession worker_session(worker_id);
 400     ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
 401     ShenandoahHeapRegionSetIterator it(slice);
 402     ShenandoahHeapRegion* from_region = it.next();
 403     // No work?
 404     if (from_region == nullptr) {
 405        return;
 406     }
 407 
 408     // Sliding compaction. Walk all regions in the slice, and compact them.
 409     // Remember empty regions and reuse them as needed.
 410     ResourceMark rm;
 411 
 412     GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
 413 
 414     ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
 415 
 416     while (from_region != nullptr) {
 417       assert(is_candidate_region(from_region), "Sanity");
 418 
 419       cl.set_from_region(from_region);
 420       if (from_region->has_live()) {
 421         _heap->marked_object_iterate(from_region, &cl);
 422       }
 423 
 424       // Compacted the region to somewhere else? From-region is empty then.
 425       if (!cl.is_compact_same_region()) {
 426         empty_regions.append(from_region);
 427       }
 428       from_region = it.next();
 429     }
 430     cl.finish_region();
 431 
 432     // Mark all remaining regions as empty
 433     for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
 434       ShenandoahHeapRegion* r = empty_regions.at(pos);
 435       r->set_new_top(r->bottom());
 436     }
 437   }
 438 };
 439 
 440 void ShenandoahFullGC::calculate_target_humongous_objects() {

 441   ShenandoahHeap* heap = ShenandoahHeap::heap();
 442 
 443   // Compute the new addresses for humongous objects. We need to do this after addresses
 444   // for regular objects are calculated, and we know what regions in heap suffix are
 445   // available for humongous moves.
 446   //
 447   // Scan the heap backwards, because we are compacting humongous regions towards the end.
 448   // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
 449   // humongous start there.
 450   //
 451   // The complication is potential non-movable regions during the scan. If such region is
 452   // detected, then sliding restarts towards that non-movable region.
 453 
 454   size_t to_begin = heap->num_regions();
 455   size_t to_end = heap->num_regions();
 456 
 457   for (size_t c = heap->num_regions(); c > 0; c--) {
 458     ShenandoahHeapRegion *r = heap->get_region(c - 1);
 459     if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
 460       // To-region candidate: record this, and continue scan
 461       to_begin = r->index();
 462       continue;
 463     }
 464 
 465     if (r->is_humongous_start() && r->is_stw_move_allowed()) {
 466       // From-region candidate: movable humongous region
 467       oop old_obj = cast_to_oop(r->bottom());
 468       size_t words_size = old_obj->size();
 469       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 470 
 471       size_t start = to_end - num_regions;
 472 
 473       if (start >= to_begin && start != r->index()) {
 474         // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
 475         _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark());
 476         old_obj->forward_to(cast_to_oop(heap->get_region(start)->bottom()));
 477         to_end = start;
 478         continue;
 479       }
 480     }
 481 
 482     // Failed to fit. Scan starting from current region.
 483     to_begin = r->index();
 484     to_end = r->index();
 485   }
 486 }
 487 








 488 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
 489 private:
 490   ShenandoahHeap* const _heap;
 491 
 492 public:
 493   ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {}
 494   void heap_region_do(ShenandoahHeapRegion* r) {
 495     if (r->is_trash()) {
 496       r->recycle();
 497     }
 498     if (r->is_cset()) {
 499       r->make_regular_bypass();
 500     }
 501     if (r->is_empty_uncommitted()) {
 502       r->make_committed_bypass();
 503     }
 504     assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->index());
 505 
 506     // Record current region occupancy: this communicates empty regions are free
 507     // to the rest of Full GC code.

 705     heap->heap_region_iterate(&ecl);
 706   }
 707 
 708   // Compute the new addresses for regular objects
 709   {
 710     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
 711 
 712     distribute_slices(worker_slices);
 713 
 714     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
 715     heap->workers()->run_task(&task);
 716   }
 717 
 718   // Compute the new addresses for humongous objects
 719   {
 720     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);
 721     calculate_target_humongous_objects();
 722   }
 723 }
 724 

 725 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure {
 726 private:
 727   ShenandoahHeap* const _heap;
 728   ShenandoahMarkingContext* const _ctx;
 729 
 730   template <class T>
 731   inline void do_oop_work(T* p) {
 732     T o = RawAccess<>::oop_load(p);
 733     if (!CompressedOops::is_null(o)) {
 734       oop obj = CompressedOops::decode_not_null(o);
 735       assert(_ctx->is_marked(obj), "must be marked");
 736       if (obj->is_forwarded()) {
 737         oop forw = obj->forwardee();
 738         RawAccess<IS_NOT_NULL>::oop_store(p, forw);
 739       }
 740     }
 741   }
 742 
 743 public:
 744   ShenandoahAdjustPointersClosure() :
 745     _heap(ShenandoahHeap::heap()),
 746     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
 747 
 748   void do_oop(oop* p)       { do_oop_work(p); }
 749   void do_oop(narrowOop* p) { do_oop_work(p); }
 750   void do_method(Method* m) {}
 751   void do_nmethod(nmethod* nm) {}
 752 };
 753 

 754 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure {
 755 private:
 756   ShenandoahHeap* const _heap;
 757   ShenandoahAdjustPointersClosure _cl;
 758 
 759 public:
 760   ShenandoahAdjustPointersObjectClosure() :
 761     _heap(ShenandoahHeap::heap()) {
 762   }
 763   void do_object(oop p) {
 764     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 765     p->oop_iterate(&_cl);
 766   }
 767 };
 768 
 769 class ShenandoahAdjustPointersTask : public WorkerTask {
 770 private:
 771   ShenandoahHeap*          const _heap;
 772   ShenandoahRegionIterator       _regions;
 773 
 774 public:
 775   ShenandoahAdjustPointersTask() :
 776     WorkerTask("Shenandoah Adjust Pointers"),
 777     _heap(ShenandoahHeap::heap()) {
 778   }
 779 
 780   void work(uint worker_id) {


 781     ShenandoahParallelWorkerSession worker_session(worker_id);
 782     ShenandoahAdjustPointersObjectClosure obj_cl;
 783     ShenandoahHeapRegion* r = _regions.next();
 784     while (r != nullptr) {
 785       if (!r->is_humongous_continuation() && r->has_live()) {
 786         _heap->marked_object_iterate(r, &obj_cl);
 787       }
 788       r = _regions.next();
 789     }
 790   }









 791 };
 792 
 793 class ShenandoahAdjustRootPointersTask : public WorkerTask {
 794 private:
 795   ShenandoahRootAdjuster* _rp;
 796   PreservedMarksSet* _preserved_marks;

 797 public:
 798   ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) :
 799     WorkerTask("Shenandoah Adjust Root Pointers"),
 800     _rp(rp),
 801     _preserved_marks(preserved_marks) {}
 802 
 803   void work(uint worker_id) {


 804     ShenandoahParallelWorkerSession worker_session(worker_id);
 805     ShenandoahAdjustPointersClosure cl;
 806     _rp->roots_do(worker_id, &cl);
 807     _preserved_marks->get(worker_id)->adjust_during_full_gc();
 808   }









 809 };
 810 
 811 void ShenandoahFullGC::phase3_update_references() {
 812   GCTraceTime(Info, gc, phases) time("Phase 3: Adjust pointers", _gc_timer);
 813   ShenandoahGCPhase adjust_pointer_phase(ShenandoahPhaseTimings::full_gc_adjust_pointers);
 814 
 815   ShenandoahHeap* heap = ShenandoahHeap::heap();
 816 
 817   WorkerThreads* workers = heap->workers();
 818   uint nworkers = workers->active_workers();
 819   {
 820 #if COMPILER2_OR_JVMCI
 821     DerivedPointerTable::clear();
 822 #endif
 823     ShenandoahRootAdjuster rp(nworkers, ShenandoahPhaseTimings::full_gc_adjust_roots);
 824     ShenandoahAdjustRootPointersTask task(&rp, _preserved_marks);
 825     workers->run_task(&task);
 826 #if COMPILER2_OR_JVMCI
 827     DerivedPointerTable::update_pointers();
 828 #endif
 829   }
 830 
 831   ShenandoahAdjustPointersTask adjust_pointers_task;
 832   workers->run_task(&adjust_pointers_task);
 833 }
 834 

 835 class ShenandoahCompactObjectsClosure : public ObjectClosure {
 836 private:
 837   ShenandoahHeap* const _heap;
 838   uint            const _worker_id;
 839 
 840 public:
 841   ShenandoahCompactObjectsClosure(uint worker_id) :
 842     _heap(ShenandoahHeap::heap()), _worker_id(worker_id) {}
 843 
 844   void do_object(oop p) {
 845     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 846     size_t size = p->size();
 847     if (p->is_forwarded()) {
 848       HeapWord* compact_from = cast_from_oop<HeapWord*>(p);
 849       HeapWord* compact_to = cast_from_oop<HeapWord*>(p->forwardee());
 850       assert(compact_from != compact_to, "Forwarded object should move");
 851       Copy::aligned_conjoint_words(compact_from, compact_to, size);
 852       oop new_obj = cast_to_oop(compact_to);
 853 
 854       ContinuationGCSupport::relativize_stack_chunk(new_obj);
 855       new_obj->init_mark();
 856     }
 857   }
 858 };
 859 
 860 class ShenandoahCompactObjectsTask : public WorkerTask {
 861 private:
 862   ShenandoahHeap* const _heap;
 863   ShenandoahHeapRegionSet** const _worker_slices;
 864 
 865 public:
 866   ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** worker_slices) :
 867     WorkerTask("Shenandoah Compact Objects"),
 868     _heap(ShenandoahHeap::heap()),
 869     _worker_slices(worker_slices) {
 870   }
 871 
 872   void work(uint worker_id) {


 873     ShenandoahParallelWorkerSession worker_session(worker_id);
 874     ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
 875 
 876     ShenandoahCompactObjectsClosure cl(worker_id);
 877     ShenandoahHeapRegion* r = slice.next();
 878     while (r != nullptr) {
 879       assert(!r->is_humongous(), "must not get humongous regions here");
 880       if (r->has_live()) {
 881         _heap->marked_object_iterate(r, &cl);
 882       }
 883       r->set_top(r->new_top());
 884       r = slice.next();
 885     }
 886   }









 887 };
 888 
 889 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
 890 private:
 891   ShenandoahHeap* const _heap;
 892   size_t _live;
 893 
 894 public:
 895   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()), _live(0) {
 896     _heap->free_set()->clear();
 897   }
 898 
 899   void heap_region_do(ShenandoahHeapRegion* r) {
 900     assert (!r->is_cset(), "cset regions should have been demoted already");
 901 
 902     // Need to reset the complete-top-at-mark-start pointer here because
 903     // the complete marking bitmap is no longer valid. This ensures
 904     // size-based iteration in marked_object_iterate().
 905     // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
 906     // pinned regions.

 922     if (r->is_regular() && live == 0) {
 923       r->make_trash();
 924     }
 925 
 926     // Recycle all trash regions
 927     if (r->is_trash()) {
 928       live = 0;
 929       r->recycle();
 930     }
 931 
 932     r->set_live_data(live);
 933     r->reset_alloc_metadata();
 934     _live += live;
 935   }
 936 
 937   size_t get_live() {
 938     return _live;
 939   }
 940 };
 941 
 942 void ShenandoahFullGC::compact_humongous_objects() {

 943   // Compact humongous regions, based on their fwdptr objects.
 944   //
 945   // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
 946   // humongous regions are already compacted, and do not require further moves, which alleviates
 947   // sliding costs. We may consider doing this in parallel in future.
 948 
 949   ShenandoahHeap* heap = ShenandoahHeap::heap();
 950 
 951   for (size_t c = heap->num_regions(); c > 0; c--) {
 952     ShenandoahHeapRegion* r = heap->get_region(c - 1);
 953     if (r->is_humongous_start()) {
 954       oop old_obj = cast_to_oop(r->bottom());
 955       if (!old_obj->is_forwarded()) {
 956         // No need to move the object, it stays at the same slot
 957         continue;
 958       }
 959       size_t words_size = old_obj->size();
 960       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 961 
 962       size_t old_start = r->index();
 963       size_t old_end   = old_start + num_regions - 1;
 964       size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
 965       size_t new_end   = new_start + num_regions - 1;
 966       assert(old_start != new_start, "must be real move");
 967       assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
 968 
 969       Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), words_size);
 970       ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom()));
 971 
 972       oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
 973       new_obj->init_mark();
 974 
 975       {
 976         for (size_t c = old_start; c <= old_end; c++) {
 977           ShenandoahHeapRegion* r = heap->get_region(c);
 978           r->make_regular_bypass();
 979           r->set_top(r->bottom());
 980         }
 981 
 982         for (size_t c = new_start; c <= new_end; c++) {
 983           ShenandoahHeapRegion* r = heap->get_region(c);
 984           if (c == new_start) {
 985             r->make_humongous_start_bypass();
 986           } else {
 987             r->make_humongous_cont_bypass();
 988           }
 989 
 990           // Trailing region may be non-full, record the remainder there
 991           size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
 992           if ((c == new_end) && (remainder != 0)) {
 993             r->set_top(r->bottom() + remainder);
 994           } else {
 995             r->set_top(r->end());
 996           }
 997 
 998           r->reset_alloc_metadata();
 999         }
1000       }
1001     }
1002   }
1003 }
1004 








1005 // This is slightly different to ShHeap::reset_next_mark_bitmap:
1006 // we need to remain able to walk pinned regions.
1007 // Since pinned region do not move and don't get compacted, we will get holes with
1008 // unreachable objects in them (which may have pointers to unloaded Klasses and thus
1009 // cannot be iterated over using oop->size(). The only way to safely iterate over those is using
1010 // a valid marking bitmap and valid TAMS pointer. This class only resets marking
1011 // bitmaps for un-pinned regions, and later we only reset TAMS for unpinned regions.
1012 class ShenandoahMCResetCompleteBitmapTask : public WorkerTask {
1013 private:
1014   ShenandoahRegionIterator _regions;
1015 
1016 public:
1017   ShenandoahMCResetCompleteBitmapTask() :
1018     WorkerTask("Shenandoah Reset Bitmap") {
1019   }
1020 
1021   void work(uint worker_id) {
1022     ShenandoahParallelWorkerSession worker_session(worker_id);
1023     ShenandoahHeapRegion* region = _regions.next();
1024     ShenandoahHeap* heap = ShenandoahHeap::heap();

  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 "compiler/oopMap.hpp"
  28 #include "gc/shared/continuationGCSupport.hpp"
  29 #include "gc/shared/gcTraceTime.inline.hpp"
  30 #include "gc/shared/preservedMarks.inline.hpp"
  31 #include "gc/shared/slidingForwarding.inline.hpp"
  32 #include "gc/shared/tlab_globals.hpp"
  33 #include "gc/shared/workerThread.hpp"
  34 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  35 #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
  36 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  37 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  38 #include "gc/shenandoah/shenandoahFullGC.hpp"
  39 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  40 #include "gc/shenandoah/shenandoahMark.inline.hpp"
  41 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  42 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  43 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  44 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  45 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  46 #include "gc/shenandoah/shenandoahMetrics.hpp"
  47 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  48 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
  49 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  50 #include "gc/shenandoah/shenandoahSTWMark.hpp"
  51 #include "gc/shenandoah/shenandoahUtils.hpp"

 205   // Coming out of Full GC, we would not have any forwarded objects.
 206   // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
 207   heap->set_has_forwarded_objects(false);
 208 
 209   heap->set_full_gc_move_in_progress(true);
 210 
 211   // Setup workers for the rest
 212   OrderAccess::fence();
 213 
 214   // Initialize worker slices
 215   ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
 216   for (uint i = 0; i < heap->max_workers(); i++) {
 217     worker_slices[i] = new ShenandoahHeapRegionSet();
 218   }
 219 
 220   {
 221     // The rest of code performs region moves, where region status is undefined
 222     // until all phases run together.
 223     ShenandoahHeapLocker lock(heap->lock());
 224 
 225     SlidingForwarding::begin();
 226 
 227     phase2_calculate_target_addresses(worker_slices);
 228 
 229     OrderAccess::fence();
 230 
 231     phase3_update_references();
 232 
 233     phase4_compact_objects(worker_slices);
 234   }
 235 
 236   {
 237     // Epilogue
 238     _preserved_marks->restore(heap->workers());
 239     _preserved_marks->reclaim();
 240     SlidingForwarding::end();
 241   }
 242 
 243   // Resize metaspace
 244   MetaspaceGC::compute_new_size();
 245 
 246   // Free worker slices
 247   for (uint i = 0; i < heap->max_workers(); i++) {
 248     delete worker_slices[i];
 249   }
 250   FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
 251 
 252   heap->set_full_gc_move_in_progress(false);
 253   heap->set_full_gc_in_progress(false);
 254 
 255   if (ShenandoahVerify) {
 256     heap->verifier()->verify_after_fullgc();
 257   }
 258 
 259   if (VerifyAfterGC) {
 260     Universe::verify();

 282 void ShenandoahFullGC::phase1_mark_heap() {
 283   GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
 284   ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
 285 
 286   ShenandoahHeap* heap = ShenandoahHeap::heap();
 287 
 288   ShenandoahPrepareForMarkClosure cl;
 289   heap->heap_region_iterate(&cl);
 290 
 291   heap->set_unload_classes(heap->heuristics()->can_unload_classes());
 292 
 293   ShenandoahReferenceProcessor* rp = heap->ref_processor();
 294   // enable ("weak") refs discovery
 295   rp->set_soft_reference_policy(true); // forcefully purge all soft references
 296 
 297   ShenandoahSTWMark mark(true /*full_gc*/);
 298   mark.mark();
 299   heap->parallel_cleaning(true /* full_gc */);
 300 }
 301 
 302 template <bool ALT_FWD>
 303 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
 304 private:
 305   PreservedMarks*          const _preserved_marks;
 306   ShenandoahHeap*          const _heap;
 307   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 308   int _empty_regions_pos;
 309   ShenandoahHeapRegion*          _to_region;
 310   ShenandoahHeapRegion*          _from_region;
 311   HeapWord* _compact_point;
 312 
 313 public:
 314   ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks,
 315                                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 316                                               ShenandoahHeapRegion* to_region) :
 317     _preserved_marks(preserved_marks),
 318     _heap(ShenandoahHeap::heap()),
 319     _empty_regions(empty_regions),
 320     _empty_regions_pos(0),
 321     _to_region(to_region),
 322     _from_region(nullptr),

 352       ShenandoahHeapRegion* new_to_region;
 353       if (_empty_regions_pos < _empty_regions.length()) {
 354         new_to_region = _empty_regions.at(_empty_regions_pos);
 355         _empty_regions_pos++;
 356       } else {
 357         // Out of empty region? Compact within the same region.
 358         new_to_region = _from_region;
 359       }
 360 
 361       assert(new_to_region != _to_region, "must not reuse same to-region");
 362       assert(new_to_region != nullptr, "must not be null");
 363       _to_region = new_to_region;
 364       _compact_point = _to_region->bottom();
 365     }
 366 
 367     // Object fits into current region, record new location, if object does not move:
 368     assert(_compact_point + obj_size <= _to_region->end(), "must fit");
 369     shenandoah_assert_not_forwarded(nullptr, p);
 370     if (_compact_point != cast_from_oop<HeapWord*>(p)) {
 371       _preserved_marks->push_if_necessary(p, p->mark());
 372       SlidingForwarding::forward_to<ALT_FWD>(p, cast_to_oop(_compact_point));
 373     }
 374     _compact_point += obj_size;
 375   }
 376 };
 377 
 378 class ShenandoahPrepareForCompactionTask : public WorkerTask {
 379 private:
 380   PreservedMarksSet*        const _preserved_marks;
 381   ShenandoahHeap*           const _heap;
 382   ShenandoahHeapRegionSet** const _worker_slices;
 383 
 384 public:
 385   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :
 386     WorkerTask("Shenandoah Prepare For Compaction"),
 387     _preserved_marks(preserved_marks),
 388     _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
 389   }
 390 
 391   static bool is_candidate_region(ShenandoahHeapRegion* r) {
 392     // Empty region: get it into the slice to defragment the slice itself.
 393     // We could have skipped this without violating correctness, but we really
 394     // want to compact all live regions to the start of the heap, which sometimes
 395     // means moving them into the fully empty regions.
 396     if (r->is_empty()) return true;
 397 
 398     // Can move the region, and this is not the humongous region. Humongous
 399     // moves are special cased here, because their moves are handled separately.
 400     return r->is_stw_move_allowed() && !r->is_humongous();
 401   }
 402 
 403   void work(uint worker_id) {
 404     if (UseAltGCForwarding) {
 405       work_impl<true>(worker_id);
 406     } else {
 407       work_impl<false>(worker_id);
 408     }
 409   }
 410 
 411 private:
 412   template <bool ALT_FWD>
 413   void work_impl(uint worker_id) {
 414     ShenandoahParallelWorkerSession worker_session(worker_id);
 415     ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
 416     ShenandoahHeapRegionSetIterator it(slice);
 417     ShenandoahHeapRegion* from_region = it.next();
 418     // No work?
 419     if (from_region == nullptr) {
 420        return;
 421     }
 422 
 423     // Sliding compaction. Walk all regions in the slice, and compact them.
 424     // Remember empty regions and reuse them as needed.
 425     ResourceMark rm;
 426 
 427     GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
 428 
 429     ShenandoahPrepareForCompactionObjectClosure<ALT_FWD> cl(_preserved_marks->get(worker_id), empty_regions, from_region);
 430 
 431     while (from_region != nullptr) {
 432       assert(is_candidate_region(from_region), "Sanity");
 433 
 434       cl.set_from_region(from_region);
 435       if (from_region->has_live()) {
 436         _heap->marked_object_iterate(from_region, &cl);
 437       }
 438 
 439       // Compacted the region to somewhere else? From-region is empty then.
 440       if (!cl.is_compact_same_region()) {
 441         empty_regions.append(from_region);
 442       }
 443       from_region = it.next();
 444     }
 445     cl.finish_region();
 446 
 447     // Mark all remaining regions as empty
 448     for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
 449       ShenandoahHeapRegion* r = empty_regions.at(pos);
 450       r->set_new_top(r->bottom());
 451     }
 452   }
 453 };
 454 
 455 template <bool ALT_FWD>
 456 void ShenandoahFullGC::calculate_target_humongous_objects_impl() {
 457   ShenandoahHeap* heap = ShenandoahHeap::heap();
 458 
 459   // Compute the new addresses for humongous objects. We need to do this after addresses
 460   // for regular objects are calculated, and we know what regions in heap suffix are
 461   // available for humongous moves.
 462   //
 463   // Scan the heap backwards, because we are compacting humongous regions towards the end.
 464   // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
 465   // humongous start there.
 466   //
 467   // The complication is potential non-movable regions during the scan. If such region is
 468   // detected, then sliding restarts towards that non-movable region.
 469 
 470   size_t to_begin = heap->num_regions();
 471   size_t to_end = heap->num_regions();
 472 
 473   for (size_t c = heap->num_regions(); c > 0; c--) {
 474     ShenandoahHeapRegion *r = heap->get_region(c - 1);
 475     if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
 476       // To-region candidate: record this, and continue scan
 477       to_begin = r->index();
 478       continue;
 479     }
 480 
 481     if (r->is_humongous_start() && r->is_stw_move_allowed()) {
 482       // From-region candidate: movable humongous region
 483       oop old_obj = cast_to_oop(r->bottom());
 484       size_t words_size = old_obj->size();
 485       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 486 
 487       size_t start = to_end - num_regions;
 488 
 489       if (start >= to_begin && start != r->index()) {
 490         // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
 491         _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark());
 492         SlidingForwarding::forward_to<ALT_FWD>(old_obj, cast_to_oop(heap->get_region(start)->bottom()));
 493         to_end = start;
 494         continue;
 495       }
 496     }
 497 
 498     // Failed to fit. Scan starting from current region.
 499     to_begin = r->index();
 500     to_end = r->index();
 501   }
 502 }
 503 
 504 void ShenandoahFullGC::calculate_target_humongous_objects() {
 505   if (UseAltGCForwarding) {
 506     calculate_target_humongous_objects_impl<true>();
 507   } else {
 508     calculate_target_humongous_objects_impl<false>();
 509   }
 510 }
 511 
 512 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
 513 private:
 514   ShenandoahHeap* const _heap;
 515 
 516 public:
 517   ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {}
 518   void heap_region_do(ShenandoahHeapRegion* r) {
 519     if (r->is_trash()) {
 520       r->recycle();
 521     }
 522     if (r->is_cset()) {
 523       r->make_regular_bypass();
 524     }
 525     if (r->is_empty_uncommitted()) {
 526       r->make_committed_bypass();
 527     }
 528     assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->index());
 529 
 530     // Record current region occupancy: this communicates empty regions are free
 531     // to the rest of Full GC code.

 729     heap->heap_region_iterate(&ecl);
 730   }
 731 
 732   // Compute the new addresses for regular objects
 733   {
 734     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
 735 
 736     distribute_slices(worker_slices);
 737 
 738     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
 739     heap->workers()->run_task(&task);
 740   }
 741 
 742   // Compute the new addresses for humongous objects
 743   {
 744     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);
 745     calculate_target_humongous_objects();
 746   }
 747 }
 748 
 749 template <bool ALT_FWD>
 750 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure {
 751 private:
 752   ShenandoahHeap* const _heap;
 753   ShenandoahMarkingContext* const _ctx;
 754 
 755   template <class T>
 756   inline void do_oop_work(T* p) {
 757     T o = RawAccess<>::oop_load(p);
 758     if (!CompressedOops::is_null(o)) {
 759       oop obj = CompressedOops::decode_not_null(o);
 760       assert(_ctx->is_marked(obj), "must be marked");
 761       if (SlidingForwarding::is_forwarded(obj)) {
 762         oop forw = SlidingForwarding::forwardee<ALT_FWD>(obj);
 763         RawAccess<IS_NOT_NULL>::oop_store(p, forw);
 764       }
 765     }
 766   }
 767 
 768 public:
 769   ShenandoahAdjustPointersClosure() :
 770     _heap(ShenandoahHeap::heap()),
 771     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
 772 
 773   void do_oop(oop* p)       { do_oop_work(p); }
 774   void do_oop(narrowOop* p) { do_oop_work(p); }
 775   void do_method(Method* m) {}
 776   void do_nmethod(nmethod* nm) {}
 777 };
 778 
 779 template <bool ALT_FWD>
 780 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure {
 781 private:
 782   ShenandoahHeap* const _heap;
 783   ShenandoahAdjustPointersClosure<ALT_FWD> _cl;
 784 
 785 public:
 786   ShenandoahAdjustPointersObjectClosure() :
 787     _heap(ShenandoahHeap::heap()) {
 788   }
 789   void do_object(oop p) {
 790     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 791     p->oop_iterate(&_cl);
 792   }
 793 };
 794 
 795 class ShenandoahAdjustPointersTask : public WorkerTask {
 796 private:
 797   ShenandoahHeap*          const _heap;
 798   ShenandoahRegionIterator       _regions;
 799 
 800 public:
 801   ShenandoahAdjustPointersTask() :
 802     WorkerTask("Shenandoah Adjust Pointers"),
 803     _heap(ShenandoahHeap::heap()) {
 804   }
 805 
 806 private:
 807   template <bool ALT_FWD>
 808   void work_impl(uint worker_id) {
 809     ShenandoahParallelWorkerSession worker_session(worker_id);
 810     ShenandoahAdjustPointersObjectClosure<ALT_FWD> obj_cl;
 811     ShenandoahHeapRegion* r = _regions.next();
 812     while (r != nullptr) {
 813       if (!r->is_humongous_continuation() && r->has_live()) {
 814         _heap->marked_object_iterate(r, &obj_cl);
 815       }
 816       r = _regions.next();
 817     }
 818   }
 819 
 820 public:
 821   void work(uint worker_id) {
 822     if (UseAltGCForwarding) {
 823       work_impl<true>(worker_id);
 824     } else {
 825       work_impl<false>(worker_id);
 826     }
 827   }
 828 };
 829 
 830 class ShenandoahAdjustRootPointersTask : public WorkerTask {
 831 private:
 832   ShenandoahRootAdjuster* _rp;
 833   PreservedMarksSet* _preserved_marks;
 834 
 835 public:
 836   ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) :
 837     WorkerTask("Shenandoah Adjust Root Pointers"),
 838     _rp(rp),
 839     _preserved_marks(preserved_marks) {}
 840 
 841 private:
 842   template <bool ALT_FWD>
 843   void work_impl(uint worker_id) {
 844     ShenandoahParallelWorkerSession worker_session(worker_id);
 845     ShenandoahAdjustPointersClosure<ALT_FWD> cl;
 846     _rp->roots_do(worker_id, &cl);
 847     _preserved_marks->get(worker_id)->adjust_during_full_gc();
 848   }
 849 
 850 public:
 851   void work(uint worker_id) {
 852     if (UseAltGCForwarding) {
 853       work_impl<true>(worker_id);
 854     } else {
 855       work_impl<false>(worker_id);
 856     }
 857   }
 858 };
 859 
 860 void ShenandoahFullGC::phase3_update_references() {
 861   GCTraceTime(Info, gc, phases) time("Phase 3: Adjust pointers", _gc_timer);
 862   ShenandoahGCPhase adjust_pointer_phase(ShenandoahPhaseTimings::full_gc_adjust_pointers);
 863 
 864   ShenandoahHeap* heap = ShenandoahHeap::heap();
 865 
 866   WorkerThreads* workers = heap->workers();
 867   uint nworkers = workers->active_workers();
 868   {
 869 #if COMPILER2_OR_JVMCI
 870     DerivedPointerTable::clear();
 871 #endif
 872     ShenandoahRootAdjuster rp(nworkers, ShenandoahPhaseTimings::full_gc_adjust_roots);
 873     ShenandoahAdjustRootPointersTask task(&rp, _preserved_marks);
 874     workers->run_task(&task);
 875 #if COMPILER2_OR_JVMCI
 876     DerivedPointerTable::update_pointers();
 877 #endif
 878   }
 879 
 880   ShenandoahAdjustPointersTask adjust_pointers_task;
 881   workers->run_task(&adjust_pointers_task);
 882 }
 883 
 884 template <bool ALT_FWD>
 885 class ShenandoahCompactObjectsClosure : public ObjectClosure {
 886 private:
 887   ShenandoahHeap* const _heap;
 888   uint            const _worker_id;
 889 
 890 public:
 891   ShenandoahCompactObjectsClosure(uint worker_id) :
 892     _heap(ShenandoahHeap::heap()), _worker_id(worker_id) {}
 893 
 894   void do_object(oop p) {
 895     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 896     size_t size = p->size();
 897     if (SlidingForwarding::is_forwarded(p)) {
 898       HeapWord* compact_from = cast_from_oop<HeapWord*>(p);
 899       HeapWord* compact_to = cast_from_oop<HeapWord*>(SlidingForwarding::forwardee<ALT_FWD>(p));
 900       assert(compact_from != compact_to, "Forwarded object should move");
 901       Copy::aligned_conjoint_words(compact_from, compact_to, size);
 902       oop new_obj = cast_to_oop(compact_to);
 903 
 904       ContinuationGCSupport::relativize_stack_chunk(new_obj);
 905       new_obj->init_mark();
 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 private:
 923   template <bool ALT_FWD>
 924   void work_impl(uint worker_id) {
 925     ShenandoahParallelWorkerSession worker_session(worker_id);
 926     ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
 927 
 928     ShenandoahCompactObjectsClosure<ALT_FWD> cl(worker_id);
 929     ShenandoahHeapRegion* r = slice.next();
 930     while (r != nullptr) {
 931       assert(!r->is_humongous(), "must not get humongous regions here");
 932       if (r->has_live()) {
 933         _heap->marked_object_iterate(r, &cl);
 934       }
 935       r->set_top(r->new_top());
 936       r = slice.next();
 937     }
 938   }
 939 
 940 public:
 941   void work(uint worker_id) {
 942     if (UseAltGCForwarding) {
 943       work_impl<true>(worker_id);
 944     } else {
 945       work_impl<false>(worker_id);
 946     }
 947   }
 948 };
 949 
 950 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
 951 private:
 952   ShenandoahHeap* const _heap;
 953   size_t _live;
 954 
 955 public:
 956   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()), _live(0) {
 957     _heap->free_set()->clear();
 958   }
 959 
 960   void heap_region_do(ShenandoahHeapRegion* r) {
 961     assert (!r->is_cset(), "cset regions should have been demoted already");
 962 
 963     // Need to reset the complete-top-at-mark-start pointer here because
 964     // the complete marking bitmap is no longer valid. This ensures
 965     // size-based iteration in marked_object_iterate().
 966     // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
 967     // pinned regions.

 983     if (r->is_regular() && live == 0) {
 984       r->make_trash();
 985     }
 986 
 987     // Recycle all trash regions
 988     if (r->is_trash()) {
 989       live = 0;
 990       r->recycle();
 991     }
 992 
 993     r->set_live_data(live);
 994     r->reset_alloc_metadata();
 995     _live += live;
 996   }
 997 
 998   size_t get_live() {
 999     return _live;
1000   }
1001 };
1002 
1003 template <bool ALT_FWD>
1004 void ShenandoahFullGC::compact_humongous_objects_impl() {
1005   // Compact humongous regions, based on their fwdptr objects.
1006   //
1007   // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
1008   // humongous regions are already compacted, and do not require further moves, which alleviates
1009   // sliding costs. We may consider doing this in parallel in future.
1010 
1011   ShenandoahHeap* heap = ShenandoahHeap::heap();
1012 
1013   for (size_t c = heap->num_regions(); c > 0; c--) {
1014     ShenandoahHeapRegion* r = heap->get_region(c - 1);
1015     if (r->is_humongous_start()) {
1016       oop old_obj = cast_to_oop(r->bottom());
1017       if (SlidingForwarding::is_not_forwarded(old_obj)) {
1018         // No need to move the object, it stays at the same slot
1019         continue;
1020       }
1021       size_t words_size = old_obj->size();
1022       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
1023 
1024       size_t old_start = r->index();
1025       size_t old_end   = old_start + num_regions - 1;
1026       size_t new_start = heap->heap_region_index_containing(SlidingForwarding::forwardee<ALT_FWD>(old_obj));
1027       size_t new_end   = new_start + num_regions - 1;
1028       assert(old_start != new_start, "must be real move");
1029       assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
1030 
1031       Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), words_size);
1032       ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom()));
1033 
1034       oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
1035       new_obj->init_mark();
1036 
1037       {
1038         for (size_t c = old_start; c <= old_end; c++) {
1039           ShenandoahHeapRegion* r = heap->get_region(c);
1040           r->make_regular_bypass();
1041           r->set_top(r->bottom());
1042         }
1043 
1044         for (size_t c = new_start; c <= new_end; c++) {
1045           ShenandoahHeapRegion* r = heap->get_region(c);
1046           if (c == new_start) {
1047             r->make_humongous_start_bypass();
1048           } else {
1049             r->make_humongous_cont_bypass();
1050           }
1051 
1052           // Trailing region may be non-full, record the remainder there
1053           size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
1054           if ((c == new_end) && (remainder != 0)) {
1055             r->set_top(r->bottom() + remainder);
1056           } else {
1057             r->set_top(r->end());
1058           }
1059 
1060           r->reset_alloc_metadata();
1061         }
1062       }
1063     }
1064   }
1065 }
1066 
1067 void ShenandoahFullGC::compact_humongous_objects() {
1068   if (UseAltGCForwarding) {
1069     compact_humongous_objects_impl<true>();
1070   } else {
1071     compact_humongous_objects_impl<false>();
1072   }
1073 }
1074 
1075 // This is slightly different to ShHeap::reset_next_mark_bitmap:
1076 // we need to remain able to walk pinned regions.
1077 // Since pinned region do not move and don't get compacted, we will get holes with
1078 // unreachable objects in them (which may have pointers to unloaded Klasses and thus
1079 // cannot be iterated over using oop->size(). The only way to safely iterate over those is using
1080 // a valid marking bitmap and valid TAMS pointer. This class only resets marking
1081 // bitmaps for un-pinned regions, and later we only reset TAMS for unpinned regions.
1082 class ShenandoahMCResetCompleteBitmapTask : public WorkerTask {
1083 private:
1084   ShenandoahRegionIterator _regions;
1085 
1086 public:
1087   ShenandoahMCResetCompleteBitmapTask() :
1088     WorkerTask("Shenandoah Reset Bitmap") {
1089   }
1090 
1091   void work(uint worker_id) {
1092     ShenandoahParallelWorkerSession worker_session(worker_id);
1093     ShenandoahHeapRegion* region = _regions.next();
1094     ShenandoahHeap* heap = ShenandoahHeap::heap();
< prev index next >