< prev index next >

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

Print this page

   1 /*
   2  * Copyright (c) 2014, 2021, Red Hat, Inc. All rights reserved.

   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 
  27 #include "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"
  51 #include "gc/shenandoah/shenandoahVerifier.hpp"
  52 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  53 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  54 #include "memory/metaspaceUtils.hpp"
  55 #include "memory/universe.hpp"
  56 #include "oops/compressedOops.inline.hpp"
  57 #include "oops/oop.inline.hpp"
  58 #include "runtime/javaThread.hpp"
  59 #include "runtime/orderAccess.hpp"
  60 #include "runtime/vmThread.hpp"
  61 #include "utilities/copy.hpp"
  62 #include "utilities/events.hpp"
  63 #include "utilities/growableArray.hpp"
  64 
  65 ShenandoahFullGC::ShenandoahFullGC() :
  66   _gc_timer(ShenandoahHeap::heap()->gc_timer()),
  67   _preserved_marks(new PreservedMarksSet(true)) {}
  68 
  69 ShenandoahFullGC::~ShenandoahFullGC() {
  70   delete _preserved_marks;
  71 }
  72 
  73 bool ShenandoahFullGC::collect(GCCause::Cause cause) {
  74   vmop_entry_full(cause);
  75   // Always success
  76   return true;
  77 }
  78 

  88 
  89 void ShenandoahFullGC::entry_full(GCCause::Cause cause) {
  90   static const char* msg = "Pause Full";
  91   ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::full_gc, true /* log_heap_usage */);
  92   EventMark em("%s", msg);
  93 
  94   ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
  95                               ShenandoahWorkerPolicy::calc_workers_for_fullgc(),
  96                               "full gc");
  97 
  98   op_full(cause);
  99 }
 100 
 101 void ShenandoahFullGC::op_full(GCCause::Cause cause) {
 102   ShenandoahMetricsSnapshot metrics;
 103   metrics.snap_before();
 104 
 105   // Perform full GC
 106   do_it(cause);
 107 






 108   metrics.snap_after();
 109 
 110   if (metrics.is_good_progress()) {
 111     ShenandoahHeap::heap()->notify_gc_progress();
 112   } else {
 113     // Nothing to do. Tell the allocation path that we have failed to make
 114     // progress, and it can finally fail.
 115     ShenandoahHeap::heap()->notify_gc_no_progress();









 116   }
 117 }
 118 
 119 void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
 120   ShenandoahHeap* heap = ShenandoahHeap::heap();
 121 




 122   if (ShenandoahVerify) {
 123     heap->verifier()->verify_before_fullgc();
 124   }
 125 
 126   if (VerifyBeforeGC) {
 127     Universe::verify();
 128   }
 129 
 130   // Degenerated GC may carry concurrent root flags when upgrading to
 131   // full GC. We need to reset it before mutators resume.
 132   heap->set_concurrent_strong_root_in_progress(false);
 133   heap->set_concurrent_weak_root_in_progress(false);
 134 
 135   heap->set_full_gc_in_progress(true);
 136 
 137   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
 138   assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped");
 139 
 140   {
 141     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_pre);

 144 
 145   {
 146     ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare);
 147     // Full GC is supposed to recover from any GC state:
 148 
 149     // a0. Remember if we have forwarded objects
 150     bool has_forwarded_objects = heap->has_forwarded_objects();
 151 
 152     // a1. Cancel evacuation, if in progress
 153     if (heap->is_evacuation_in_progress()) {
 154       heap->set_evacuation_in_progress(false);
 155     }
 156     assert(!heap->is_evacuation_in_progress(), "sanity");
 157 
 158     // a2. Cancel update-refs, if in progress
 159     if (heap->is_update_refs_in_progress()) {
 160       heap->set_update_refs_in_progress(false);
 161     }
 162     assert(!heap->is_update_refs_in_progress(), "sanity");
 163 
 164     // b. Cancel concurrent mark, if in progress
 165     if (heap->is_concurrent_mark_in_progress()) {
 166       ShenandoahConcurrentGC::cancel();
 167       heap->set_concurrent_mark_in_progress(false);
 168     }
 169     assert(!heap->is_concurrent_mark_in_progress(), "sanity");
 170 
 171     // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
 172     if (has_forwarded_objects) {
 173       update_roots(true /*full_gc*/);
 174     }
 175 
 176     // d. Reset the bitmaps for new marking
 177     heap->reset_mark_bitmap();
 178     assert(heap->marking_context()->is_bitmap_clear(), "sanity");
 179     assert(!heap->marking_context()->is_complete(), "sanity");
 180 
 181     // e. Abandon reference discovery and clear all discovered references.
 182     ShenandoahReferenceProcessor* rp = heap->ref_processor();
 183     rp->abandon_partial_discovery();
 184 
 185     // f. Sync pinned region status from the CP marks
 186     heap->sync_pinned_region_status();
 187 




 188     // The rest of prologue:
 189     _preserved_marks->init(heap->workers()->active_workers());
 190 
 191     assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
 192   }
 193 
 194   if (UseTLAB) {

 195     heap->gclabs_retire(ResizeTLAB);
 196     heap->tlabs_retire(ResizeTLAB);
 197   }
 198 
 199   OrderAccess::fence();
 200 
 201   phase1_mark_heap();
 202 
 203   // Once marking is done, which may have fixed up forwarded objects, we can drop it.
 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();
 257   }
 258 
 259   {
 260     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_post);
 261     heap->post_full_gc_dump(_gc_timer);
 262   }
 263 }
 264 
 265 class ShenandoahPrepareForMarkClosure: public ShenandoahHeapRegionClosure {
 266 private:
 267   ShenandoahMarkingContext* const _ctx;
 268 
 269 public:
 270   ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
 271 
 272   void heap_region_do(ShenandoahHeapRegion *r) {
 273     _ctx->capture_top_at_mark_start(r);
 274     r->clear_live_data();
 275   }
 276 };
 277 
 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),
 318     _compact_point(to_region->bottom()) {}
 319 
 320   void set_from_region(ShenandoahHeapRegion* from_region) {
 321     _from_region = from_region;
 322   }
 323 
 324   void finish_region() {
 325     assert(_to_region != nullptr, "should not happen");
 326     _to_region->set_new_top(_compact_point);
 327   }
 328 
 329   bool is_compact_same_region() {
 330     return _from_region == _to_region;
 331   }
 332 
 333   int empty_regions_pos() {
 334     return _empty_regions_pos;
 335   }
 336 
 337   void do_object(oop p) {
 338     assert(_from_region != nullptr, "must set before work");
 339     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 340     assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
 341 
 342     size_t obj_size = p->size();
 343     if (_compact_point + obj_size > _to_region->end()) {
 344       finish_region();
 345 
 346       // Object doesn't fit. Pick next empty region and start compacting there.
 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);

 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.
 508     r->set_new_top(r->top());
 509   }
 510 };
 511 
 512 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure {
 513 private:
 514   ShenandoahHeap* const _heap;
 515   ShenandoahMarkingContext* const _ctx;
 516 
 517 public:
 518   ShenandoahTrashImmediateGarbageClosure() :
 519     _heap(ShenandoahHeap::heap()),
 520     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
 521 
 522   void heap_region_do(ShenandoahHeapRegion* r) {
 523     if (r->is_humongous_start()) {
 524       oop humongous_obj = cast_to_oop(r->bottom());
 525       if (!_ctx->is_marked(humongous_obj)) {
 526         assert(!r->has_live(),
 527                "Region " SIZE_FORMAT " is not marked, should not have live", r->index());
 528         _heap->trash_humongous_region_at(r);
 529       } else {
 530         assert(r->has_live(),
 531                "Region " SIZE_FORMAT " should have live", r->index());
 532       }
 533     } else if (r->is_humongous_continuation()) {
 534       // If we hit continuation, the non-live humongous starts should have been trashed already
 535       assert(r->humongous_start_region()->has_live(),
 536              "Region " SIZE_FORMAT " should have live", r->index());
 537     } else if (r->is_regular()) {
 538       if (!r->has_live()) {
 539         r->make_trash_immediate();
 540       }
 541     }
 542   }
 543 };
 544 
 545 void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
 546   ShenandoahHeap* heap = ShenandoahHeap::heap();
 547 
 548   uint n_workers = heap->workers()->active_workers();
 549   size_t n_regions = heap->num_regions();
 550 
 551   // What we want to accomplish: have the dense prefix of data, while still balancing
 552   // out the parallel work.
 553   //
 554   // Assuming the amount of work is driven by the live data that needs moving, we can slice
 555   // the entire heap into equal-live-sized prefix slices, and compact into them. So, each
 556   // thread takes all regions in its prefix subset, and then it takes some regions from

 679   for (size_t rid = 0; rid < n_regions; rid++) {
 680     bool is_candidate = ShenandoahPrepareForCompactionTask::is_candidate_region(heap->get_region(rid));
 681     bool is_distributed = map.at(rid);
 682     assert(is_distributed || !is_candidate, "All candidates are distributed: " SIZE_FORMAT, rid);
 683   }
 684 #endif
 685 }
 686 
 687 void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
 688   GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
 689   ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
 690 
 691   ShenandoahHeap* heap = ShenandoahHeap::heap();
 692 
 693   // About to figure out which regions can be compacted, make sure pinning status
 694   // had been updated in GC prologue.
 695   heap->assert_pinned_region_status();
 696 
 697   {
 698     // Trash the immediately collectible regions before computing addresses
 699     ShenandoahTrashImmediateGarbageClosure tigcl;
 700     heap->heap_region_iterate(&tigcl);

 701 
 702     // Make sure regions are in good state: committed, active, clean.
 703     // This is needed because we are potentially sliding the data through them.
 704     ShenandoahEnsureHeapActiveClosure ecl;
 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);

 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();

 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.
 907     if (!r->is_pinned()) {
 908       _heap->complete_marking_context()->reset_top_at_mark_start(r);
 909     }
 910 
 911     size_t live = r->used();
 912 
 913     // Make empty regions that have been allocated into regular
 914     if (r->is_empty() && live > 0) {




 915       r->make_regular_bypass();
 916       if (ZapUnusedHeapArea) {
 917         SpaceMangler::mangle_region(MemRegion(r->top(), r->end()));
 918       }
 919     }
 920 
 921     // Reclaim regular regions that became empty
 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

1033 };
1034 
1035 void ShenandoahFullGC::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) {
1036   GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer);
1037   ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects);
1038 
1039   ShenandoahHeap* heap = ShenandoahHeap::heap();
1040 
1041   // Compact regular objects first
1042   {
1043     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular);
1044     ShenandoahCompactObjectsTask compact_task(worker_slices);
1045     heap->workers()->run_task(&compact_task);
1046   }
1047 
1048   // Compact humongous objects after regular object moves
1049   {
1050     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
1051     compact_humongous_objects();
1052   }





1053 
1054   // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
1055   // and must ensure the bitmap is in sync.
1056   {
1057     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
1058     ShenandoahMCResetCompleteBitmapTask task;
1059     heap->workers()->run_task(&task);
1060   }
1061 
1062   // Bring regions in proper states after the collection, and set heap properties.
1063   {
1064     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
1065 
1066     ShenandoahPostCompactClosure post_compact;
1067     heap->heap_region_iterate(&post_compact);
1068     heap->set_used(post_compact.get_live());




1069 
1070     heap->collection_set()->clear();
1071     heap->free_set()->rebuild();












1072   }
1073 
1074   heap->clear_cancelled_gc();








1075 }

   1 /*
   2  * Copyright (c) 2014, 2021, Red Hat, Inc. All rights reserved.
   3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 
  28 #include "compiler/oopMap.hpp"
  29 #include "gc/shared/continuationGCSupport.hpp"
  30 #include "gc/shared/gcTraceTime.inline.hpp"
  31 #include "gc/shared/preservedMarks.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/shenandoahCollectorPolicy.hpp"
  36 #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
  37 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  38 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  39 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  40 #include "gc/shenandoah/shenandoahFullGC.hpp"
  41 #include "gc/shenandoah/shenandoahGenerationalFullGC.hpp"
  42 #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
  43 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  44 #include "gc/shenandoah/shenandoahMark.inline.hpp"
  45 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  46 #include "gc/shenandoah/shenandoahHeapRegionClosures.hpp"
  47 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  48 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  49 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  50 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  51 #include "gc/shenandoah/shenandoahMetrics.hpp"
  52 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  53 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
  54 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  55 #include "gc/shenandoah/shenandoahSTWMark.hpp"
  56 #include "gc/shenandoah/shenandoahUtils.hpp"
  57 #include "gc/shenandoah/shenandoahVerifier.hpp"
  58 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  59 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  60 #include "memory/metaspaceUtils.hpp"
  61 #include "memory/universe.hpp"
  62 #include "oops/compressedOops.inline.hpp"
  63 #include "oops/oop.inline.hpp"

  64 #include "runtime/orderAccess.hpp"
  65 #include "runtime/vmThread.hpp"
  66 #include "utilities/copy.hpp"
  67 #include "utilities/events.hpp"
  68 #include "utilities/growableArray.hpp"
  69 
  70 ShenandoahFullGC::ShenandoahFullGC() :
  71   _gc_timer(ShenandoahHeap::heap()->gc_timer()),
  72   _preserved_marks(new PreservedMarksSet(true)) {}
  73 
  74 ShenandoahFullGC::~ShenandoahFullGC() {
  75   delete _preserved_marks;
  76 }
  77 
  78 bool ShenandoahFullGC::collect(GCCause::Cause cause) {
  79   vmop_entry_full(cause);
  80   // Always success
  81   return true;
  82 }
  83 

  93 
  94 void ShenandoahFullGC::entry_full(GCCause::Cause cause) {
  95   static const char* msg = "Pause Full";
  96   ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::full_gc, true /* log_heap_usage */);
  97   EventMark em("%s", msg);
  98 
  99   ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
 100                               ShenandoahWorkerPolicy::calc_workers_for_fullgc(),
 101                               "full gc");
 102 
 103   op_full(cause);
 104 }
 105 
 106 void ShenandoahFullGC::op_full(GCCause::Cause cause) {
 107   ShenandoahMetricsSnapshot metrics;
 108   metrics.snap_before();
 109 
 110   // Perform full GC
 111   do_it(cause);
 112 
 113   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 114 
 115   if (heap->mode()->is_generational()) {
 116     ShenandoahGenerationalFullGC::handle_completion(heap);
 117   }
 118 
 119   metrics.snap_after();
 120 
 121   if (metrics.is_good_progress(heap->global_generation())) {
 122     heap->notify_gc_progress();
 123   } else {
 124     // Nothing to do. Tell the allocation path that we have failed to make
 125     // progress, and it can finally fail.
 126     heap->notify_gc_no_progress();
 127   }
 128 
 129   // Regardless if progress was made, we record that we completed a "successful" full GC.
 130   heap->global_generation()->heuristics()->record_success_full();
 131   heap->shenandoah_policy()->record_success_full();
 132 
 133   {
 134     ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::full_gc_propagate_gc_state);
 135     heap->propagate_gc_state_to_all_threads();
 136   }
 137 }
 138 
 139 void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
 140   ShenandoahHeap* heap = ShenandoahHeap::heap();
 141 
 142   if (heap->mode()->is_generational()) {
 143     ShenandoahGenerationalFullGC::prepare();
 144   }
 145 
 146   if (ShenandoahVerify) {
 147     heap->verifier()->verify_before_fullgc();
 148   }
 149 
 150   if (VerifyBeforeGC) {
 151     Universe::verify();
 152   }
 153 
 154   // Degenerated GC may carry concurrent root flags when upgrading to
 155   // full GC. We need to reset it before mutators resume.
 156   heap->set_concurrent_strong_root_in_progress(false);
 157   heap->set_concurrent_weak_root_in_progress(false);
 158 
 159   heap->set_full_gc_in_progress(true);
 160 
 161   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
 162   assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped");
 163 
 164   {
 165     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_pre);

 168 
 169   {
 170     ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare);
 171     // Full GC is supposed to recover from any GC state:
 172 
 173     // a0. Remember if we have forwarded objects
 174     bool has_forwarded_objects = heap->has_forwarded_objects();
 175 
 176     // a1. Cancel evacuation, if in progress
 177     if (heap->is_evacuation_in_progress()) {
 178       heap->set_evacuation_in_progress(false);
 179     }
 180     assert(!heap->is_evacuation_in_progress(), "sanity");
 181 
 182     // a2. Cancel update-refs, if in progress
 183     if (heap->is_update_refs_in_progress()) {
 184       heap->set_update_refs_in_progress(false);
 185     }
 186     assert(!heap->is_update_refs_in_progress(), "sanity");
 187 
 188     // b. Cancel all concurrent marks, if in progress
 189     if (heap->is_concurrent_mark_in_progress()) {
 190       heap->cancel_concurrent_mark();

 191     }
 192     assert(!heap->is_concurrent_mark_in_progress(), "sanity");
 193 
 194     // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
 195     if (has_forwarded_objects) {
 196       update_roots(true /*full_gc*/);
 197     }
 198 
 199     // d. Abandon reference discovery and clear all discovered references.
 200     ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();





 201     rp->abandon_partial_discovery();
 202 
 203     // e. Sync pinned region status from the CP marks
 204     heap->sync_pinned_region_status();
 205 
 206     if (heap->mode()->is_generational()) {
 207       ShenandoahGenerationalFullGC::restore_top_before_promote(heap);
 208     }
 209 
 210     // The rest of prologue:
 211     _preserved_marks->init(heap->workers()->active_workers());
 212 
 213     assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
 214   }
 215 
 216   if (UseTLAB) {
 217     // Note: PLABs are also retired with GCLABs in generational mode.
 218     heap->gclabs_retire(ResizeTLAB);
 219     heap->tlabs_retire(ResizeTLAB);
 220   }
 221 
 222   OrderAccess::fence();
 223 
 224   phase1_mark_heap();
 225 
 226   // Once marking is done, which may have fixed up forwarded objects, we can drop it.
 227   // Coming out of Full GC, we would not have any forwarded objects.
 228   // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
 229   heap->set_has_forwarded_objects(false);
 230 
 231   heap->set_full_gc_move_in_progress(true);
 232 
 233   // Setup workers for the rest
 234   OrderAccess::fence();
 235 
 236   // Initialize worker slices
 237   ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
 238   for (uint i = 0; i < heap->max_workers(); i++) {
 239     worker_slices[i] = new ShenandoahHeapRegionSet();
 240   }
 241 
 242   {
 243     // The rest of code performs region moves, where region status is undefined
 244     // until all phases run together.
 245     ShenandoahHeapLocker lock(heap->lock());
 246 
 247     phase2_calculate_target_addresses(worker_slices);
 248 
 249     OrderAccess::fence();
 250 
 251     phase3_update_references();
 252 
 253     phase4_compact_objects(worker_slices);

 254 
 255     phase5_epilog();



 256   }
 257 
 258   // Resize metaspace
 259   MetaspaceGC::compute_new_size();
 260 
 261   // Free worker slices
 262   for (uint i = 0; i < heap->max_workers(); i++) {
 263     delete worker_slices[i];
 264   }
 265   FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
 266 
 267   heap->set_full_gc_move_in_progress(false);
 268   heap->set_full_gc_in_progress(false);
 269 
 270   if (ShenandoahVerify) {
 271     heap->verifier()->verify_after_fullgc();
 272   }
 273 
 274   if (VerifyAfterGC) {
 275     Universe::verify();
 276   }
 277 
 278   {
 279     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_post);
 280     heap->post_full_gc_dump(_gc_timer);
 281   }
 282 }
 283 













 284 void ShenandoahFullGC::phase1_mark_heap() {
 285   GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
 286   ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
 287 
 288   ShenandoahHeap* heap = ShenandoahHeap::heap();
 289 
 290   heap->global_generation()->reset_mark_bitmap<true, true>();
 291   assert(heap->marking_context()->is_bitmap_clear(), "sanity");
 292   assert(!heap->global_generation()->is_mark_complete(), "sanity");
 293 
 294   heap->set_unload_classes(heap->global_generation()->heuristics()->can_unload_classes());
 295 
 296   ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();
 297   // enable ("weak") refs discovery
 298   rp->set_soft_reference_policy(true); // forcefully purge all soft references
 299 
 300   ShenandoahSTWMark mark(heap->global_generation(), true /*full_gc*/);
 301   mark.mark();
 302   heap->parallel_cleaning(true /* full_gc */);
 303 
 304   if (ShenandoahHeap::heap()->mode()->is_generational()) {
 305     ShenandoahGenerationalFullGC::log_live_in_old(heap);
 306   }
 307 }
 308 
 309 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
 310 private:
 311   PreservedMarks*          const _preserved_marks;
 312   ShenandoahHeap*          const _heap;
 313   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 314   int _empty_regions_pos;
 315   ShenandoahHeapRegion*          _to_region;
 316   ShenandoahHeapRegion*          _from_region;
 317   HeapWord* _compact_point;
 318 
 319 public:
 320   ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks,
 321                                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 322                                               ShenandoahHeapRegion* to_region) :
 323     _preserved_marks(preserved_marks),
 324     _heap(ShenandoahHeap::heap()),
 325     _empty_regions(empty_regions),
 326     _empty_regions_pos(0),
 327     _to_region(to_region),
 328     _from_region(nullptr),
 329     _compact_point(to_region->bottom()) {}
 330 
 331   void set_from_region(ShenandoahHeapRegion* from_region) {
 332     _from_region = from_region;
 333   }
 334 
 335   void finish() {
 336     assert(_to_region != nullptr, "should not happen");
 337     _to_region->set_new_top(_compact_point);
 338   }
 339 
 340   bool is_compact_same_region() {
 341     return _from_region == _to_region;
 342   }
 343 
 344   int empty_regions_pos() {
 345     return _empty_regions_pos;
 346   }
 347 
 348   void do_object(oop p) {
 349     assert(_from_region != nullptr, "must set before work");
 350     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 351     assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
 352 
 353     size_t obj_size = p->size();
 354     if (_compact_point + obj_size > _to_region->end()) {
 355       finish();
 356 
 357       // Object doesn't fit. Pick next empty region and start compacting there.
 358       ShenandoahHeapRegion* new_to_region;
 359       if (_empty_regions_pos < _empty_regions.length()) {
 360         new_to_region = _empty_regions.at(_empty_regions_pos);
 361         _empty_regions_pos++;
 362       } else {
 363         // Out of empty region? Compact within the same region.
 364         new_to_region = _from_region;
 365       }
 366 
 367       assert(new_to_region != _to_region, "must not reuse same to-region");
 368       assert(new_to_region != nullptr, "must not be null");
 369       _to_region = new_to_region;
 370       _compact_point = _to_region->bottom();
 371     }
 372 
 373     // Object fits into current region, record new location, if object does not move:
 374     assert(_compact_point + obj_size <= _to_region->end(), "must fit");
 375     shenandoah_assert_not_forwarded(nullptr, p);

 389 
 390 public:
 391   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :
 392     WorkerTask("Shenandoah Prepare For Compaction"),
 393     _preserved_marks(preserved_marks),
 394     _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
 395   }
 396 
 397   static bool is_candidate_region(ShenandoahHeapRegion* r) {
 398     // Empty region: get it into the slice to defragment the slice itself.
 399     // We could have skipped this without violating correctness, but we really
 400     // want to compact all live regions to the start of the heap, which sometimes
 401     // means moving them into the fully empty regions.
 402     if (r->is_empty()) return true;
 403 
 404     // Can move the region, and this is not the humongous region. Humongous
 405     // moves are special cased here, because their moves are handled separately.
 406     return r->is_stw_move_allowed() && !r->is_humongous();
 407   }
 408 
 409   void work(uint worker_id) override;
 410 private:
 411   template<typename ClosureType>
 412   void prepare_for_compaction(ClosureType& cl,
 413                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 414                               ShenandoahHeapRegionSetIterator& it,
 415                               ShenandoahHeapRegion* from_region);
 416 };





 417 
 418 void ShenandoahPrepareForCompactionTask::work(uint worker_id) {
 419   ShenandoahParallelWorkerSession worker_session(worker_id);
 420   ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
 421   ShenandoahHeapRegionSetIterator it(slice);
 422   ShenandoahHeapRegion* from_region = it.next();
 423   // No work?
 424   if (from_region == nullptr) {
 425     return;
 426   }
 427 
 428   // Sliding compaction. Walk all regions in the slice, and compact them.
 429   // Remember empty regions and reuse them as needed.
 430   ResourceMark rm;
 431 
 432   GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());

 433 
 434   if (_heap->mode()->is_generational()) {
 435     ShenandoahPrepareForGenerationalCompactionObjectClosure cl(_preserved_marks->get(worker_id),
 436                                                                empty_regions, from_region, worker_id);
 437     prepare_for_compaction(cl, empty_regions, it, from_region);
 438   } else {
 439     ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
 440     prepare_for_compaction(cl, empty_regions, it, from_region);
 441   }
 442 }
 443 
 444 template<typename ClosureType>
 445 void ShenandoahPrepareForCompactionTask::prepare_for_compaction(ClosureType& cl,
 446                                                                 GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 447                                                                 ShenandoahHeapRegionSetIterator& it,
 448                                                                 ShenandoahHeapRegion* from_region) {
 449   while (from_region != nullptr) {
 450     assert(is_candidate_region(from_region), "Sanity");
 451     cl.set_from_region(from_region);
 452     if (from_region->has_live()) {
 453       _heap->marked_object_iterate(from_region, &cl);
 454     }

 455 
 456     // Compacted the region to somewhere else? From-region is empty then.
 457     if (!cl.is_compact_same_region()) {
 458       empty_regions.append(from_region);

 459     }
 460     from_region = it.next();
 461   }
 462   cl.finish();
 463 
 464   // Mark all remaining regions as empty
 465   for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
 466     ShenandoahHeapRegion* r = empty_regions.at(pos);
 467     r->set_new_top(r->bottom());
 468   }
 469 }
 470 
 471 void ShenandoahFullGC::calculate_target_humongous_objects() {
 472   ShenandoahHeap* heap = ShenandoahHeap::heap();
 473 
 474   // Compute the new addresses for humongous objects. We need to do this after addresses
 475   // for regular objects are calculated, and we know what regions in heap suffix are
 476   // available for humongous moves.
 477   //
 478   // Scan the heap backwards, because we are compacting humongous regions towards the end.
 479   // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
 480   // humongous start there.
 481   //
 482   // The complication is potential non-movable regions during the scan. If such region is
 483   // detected, then sliding restarts towards that non-movable region.
 484 
 485   size_t to_begin = heap->num_regions();
 486   size_t to_end = heap->num_regions();
 487 
 488   log_debug(gc)("Full GC calculating target humongous objects from end " SIZE_FORMAT, to_end);
 489   for (size_t c = heap->num_regions(); c > 0; c--) {
 490     ShenandoahHeapRegion *r = heap->get_region(c - 1);
 491     if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
 492       // To-region candidate: record this, and continue scan
 493       to_begin = r->index();
 494       continue;
 495     }
 496 
 497     if (r->is_humongous_start() && r->is_stw_move_allowed()) {
 498       // From-region candidate: movable humongous region
 499       oop old_obj = cast_to_oop(r->bottom());
 500       size_t words_size = old_obj->size();
 501       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 502 
 503       size_t start = to_end - num_regions;
 504 
 505       if (start >= to_begin && start != r->index()) {
 506         // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
 507         _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark());
 508         old_obj->forward_to(cast_to_oop(heap->get_region(start)->bottom()));
 509         to_end = start;
 510         continue;
 511       }
 512     }
 513 
 514     // Failed to fit. Scan starting from current region.
 515     to_begin = r->index();
 516     to_end = r->index();
 517   }
 518 }
 519 
 520 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
 521 private:
 522   ShenandoahHeap* const _heap;
 523 
 524 public:
 525   ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {}
 526   void heap_region_do(ShenandoahHeapRegion* r) {
 527     if (r->is_trash()) {
 528       r->try_recycle_under_lock();
 529     }
 530     if (r->is_cset()) {
 531       // Leave affiliation unchanged
 532       r->make_regular_bypass();
 533     }
 534     if (r->is_empty_uncommitted()) {
 535       r->make_committed_bypass();
 536     }
 537     assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->index());
 538 
 539     // Record current region occupancy: this communicates empty regions are free
 540     // to the rest of Full GC code.
 541     r->set_new_top(r->top());
 542   }
 543 };
 544 
 545 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure {
 546 private:
 547   ShenandoahHeap* const _heap;
 548   ShenandoahMarkingContext* const _ctx;
 549 
 550 public:
 551   ShenandoahTrashImmediateGarbageClosure() :
 552     _heap(ShenandoahHeap::heap()),
 553     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
 554 
 555   void heap_region_do(ShenandoahHeapRegion* r) override {
 556     if (r->is_humongous_start()) {
 557       oop humongous_obj = cast_to_oop(r->bottom());
 558       if (!_ctx->is_marked(humongous_obj)) {
 559         assert(!r->has_live(), "Region " SIZE_FORMAT " is not marked, should not have live", r->index());

 560         _heap->trash_humongous_region_at(r);
 561       } else {
 562         assert(r->has_live(), "Region " SIZE_FORMAT " should have live", r->index());

 563       }
 564     } else if (r->is_humongous_continuation()) {
 565       // If we hit continuation, the non-live humongous starts should have been trashed already
 566       assert(r->humongous_start_region()->has_live(), "Region " SIZE_FORMAT " should have live", r->index());

 567     } else if (r->is_regular()) {
 568       if (!r->has_live()) {
 569         r->make_trash_immediate();
 570       }
 571     }
 572   }
 573 };
 574 
 575 void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
 576   ShenandoahHeap* heap = ShenandoahHeap::heap();
 577 
 578   uint n_workers = heap->workers()->active_workers();
 579   size_t n_regions = heap->num_regions();
 580 
 581   // What we want to accomplish: have the dense prefix of data, while still balancing
 582   // out the parallel work.
 583   //
 584   // Assuming the amount of work is driven by the live data that needs moving, we can slice
 585   // the entire heap into equal-live-sized prefix slices, and compact into them. So, each
 586   // thread takes all regions in its prefix subset, and then it takes some regions from

 709   for (size_t rid = 0; rid < n_regions; rid++) {
 710     bool is_candidate = ShenandoahPrepareForCompactionTask::is_candidate_region(heap->get_region(rid));
 711     bool is_distributed = map.at(rid);
 712     assert(is_distributed || !is_candidate, "All candidates are distributed: " SIZE_FORMAT, rid);
 713   }
 714 #endif
 715 }
 716 
 717 void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
 718   GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
 719   ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
 720 
 721   ShenandoahHeap* heap = ShenandoahHeap::heap();
 722 
 723   // About to figure out which regions can be compacted, make sure pinning status
 724   // had been updated in GC prologue.
 725   heap->assert_pinned_region_status();
 726 
 727   {
 728     // Trash the immediately collectible regions before computing addresses
 729     ShenandoahTrashImmediateGarbageClosure trash_immediate_garbage;
 730     ShenandoahExcludeRegionClosure<FREE> cl(&trash_immediate_garbage);
 731     heap->heap_region_iterate(&cl);
 732 
 733     // Make sure regions are in good state: committed, active, clean.
 734     // This is needed because we are potentially sliding the data through them.
 735     ShenandoahEnsureHeapActiveClosure ecl;
 736     heap->heap_region_iterate(&ecl);
 737   }
 738 
 739   // Compute the new addresses for regular objects
 740   {
 741     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
 742 
 743     distribute_slices(worker_slices);
 744 
 745     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
 746     heap->workers()->run_task(&task);
 747   }
 748 
 749   // Compute the new addresses for humongous objects
 750   {
 751     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);

 799 
 800 class ShenandoahAdjustPointersTask : public WorkerTask {
 801 private:
 802   ShenandoahHeap*          const _heap;
 803   ShenandoahRegionIterator       _regions;
 804 
 805 public:
 806   ShenandoahAdjustPointersTask() :
 807     WorkerTask("Shenandoah Adjust Pointers"),
 808     _heap(ShenandoahHeap::heap()) {
 809   }
 810 
 811   void work(uint worker_id) {
 812     ShenandoahParallelWorkerSession worker_session(worker_id);
 813     ShenandoahAdjustPointersObjectClosure obj_cl;
 814     ShenandoahHeapRegion* r = _regions.next();
 815     while (r != nullptr) {
 816       if (!r->is_humongous_continuation() && r->has_live()) {
 817         _heap->marked_object_iterate(r, &obj_cl);
 818       }
 819       if (_heap->mode()->is_generational()) {
 820         ShenandoahGenerationalFullGC::maybe_coalesce_and_fill_region(r);
 821       }
 822       r = _regions.next();
 823     }
 824   }
 825 };
 826 
 827 class ShenandoahAdjustRootPointersTask : public WorkerTask {
 828 private:
 829   ShenandoahRootAdjuster* _rp;
 830   PreservedMarksSet* _preserved_marks;
 831 public:
 832   ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) :
 833     WorkerTask("Shenandoah Adjust Root Pointers"),
 834     _rp(rp),
 835     _preserved_marks(preserved_marks) {}
 836 
 837   void work(uint worker_id) {
 838     ShenandoahParallelWorkerSession worker_session(worker_id);
 839     ShenandoahAdjustPointersClosure cl;
 840     _rp->roots_do(worker_id, &cl);
 841     _preserved_marks->get(worker_id)->adjust_during_full_gc();

 906   void work(uint worker_id) {
 907     ShenandoahParallelWorkerSession worker_session(worker_id);
 908     ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
 909 
 910     ShenandoahCompactObjectsClosure cl(worker_id);
 911     ShenandoahHeapRegion* r = slice.next();
 912     while (r != nullptr) {
 913       assert(!r->is_humongous(), "must not get humongous regions here");
 914       if (r->has_live()) {
 915         _heap->marked_object_iterate(r, &cl);
 916       }
 917       r->set_top(r->new_top());
 918       r = slice.next();
 919     }
 920   }
 921 };
 922 
 923 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
 924 private:
 925   ShenandoahHeap* const _heap;
 926   bool _is_generational;
 927   size_t _young_regions, _young_usage, _young_humongous_waste;
 928   size_t _old_regions, _old_usage, _old_humongous_waste;
 929 
 930 public:
 931   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()),
 932                                    _is_generational(_heap->mode()->is_generational()),
 933                                    _young_regions(0),
 934                                    _young_usage(0),
 935                                    _young_humongous_waste(0),
 936                                    _old_regions(0),
 937                                    _old_usage(0),
 938                                    _old_humongous_waste(0)
 939   {
 940     _heap->free_set()->clear();
 941   }
 942 
 943   void heap_region_do(ShenandoahHeapRegion* r) {
 944     assert (!r->is_cset(), "cset regions should have been demoted already");
 945 
 946     // Need to reset the complete-top-at-mark-start pointer here because
 947     // the complete marking bitmap is no longer valid. This ensures
 948     // size-based iteration in marked_object_iterate().
 949     // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
 950     // pinned regions.
 951     if (!r->is_pinned()) {
 952       _heap->complete_marking_context()->reset_top_at_mark_start(r);
 953     }
 954 
 955     size_t live = r->used();
 956 
 957     // Make empty regions that have been allocated into regular
 958     if (r->is_empty() && live > 0) {
 959       if (!_is_generational) {
 960         r->make_affiliated_maybe();
 961       }
 962       // else, generational mode compaction has already established affiliation.
 963       r->make_regular_bypass();
 964       if (ZapUnusedHeapArea) {
 965         SpaceMangler::mangle_region(MemRegion(r->top(), r->end()));
 966       }
 967     }
 968 
 969     // Reclaim regular regions that became empty
 970     if (r->is_regular() && live == 0) {
 971       r->make_trash();
 972     }
 973 
 974     // Recycle all trash regions
 975     if (r->is_trash()) {
 976       live = 0;
 977       r->try_recycle_under_lock();
 978     } else {
 979       if (r->is_old()) {
 980         ShenandoahGenerationalFullGC::account_for_region(r, _old_regions, _old_usage, _old_humongous_waste);
 981       } else if (r->is_young()) {
 982         ShenandoahGenerationalFullGC::account_for_region(r, _young_regions, _young_usage, _young_humongous_waste);
 983       }
 984     }

 985     r->set_live_data(live);
 986     r->reset_alloc_metadata();

 987   }
 988 
 989   void update_generation_usage() {
 990     if (_is_generational) {
 991       _heap->old_generation()->establish_usage(_old_regions, _old_usage, _old_humongous_waste);
 992       _heap->young_generation()->establish_usage(_young_regions, _young_usage, _young_humongous_waste);
 993     } else {
 994       assert(_old_regions == 0, "Old regions only expected in generational mode");
 995       assert(_old_usage == 0, "Old usage only expected in generational mode");
 996       assert(_old_humongous_waste == 0, "Old humongous waste only expected in generational mode");
 997     }
 998 
 999     // In generational mode, global usage should be the sum of young and old. This is also true
1000     // for non-generational modes except that there are no old regions.
1001     _heap->global_generation()->establish_usage(_old_regions + _young_regions,
1002                                                 _old_usage + _young_usage,
1003                                                 _old_humongous_waste + _young_humongous_waste);
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 (!old_obj->is_forwarded()) {
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(old_obj->forwardee());
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 " SIZE_FORMAT " should be movable", r->index());
1033 
1034       log_debug(gc)("Full GC compaction moves humongous object from region " SIZE_FORMAT " to region " SIZE_FORMAT, 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

1101 };
1102 
1103 void ShenandoahFullGC::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) {
1104   GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer);
1105   ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects);
1106 
1107   ShenandoahHeap* heap = ShenandoahHeap::heap();
1108 
1109   // Compact regular objects first
1110   {
1111     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular);
1112     ShenandoahCompactObjectsTask compact_task(worker_slices);
1113     heap->workers()->run_task(&compact_task);
1114   }
1115 
1116   // Compact humongous objects after regular object moves
1117   {
1118     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
1119     compact_humongous_objects();
1120   }
1121 }
1122 
1123 void ShenandoahFullGC::phase5_epilog() {
1124   GCTraceTime(Info, gc, phases) time("Phase 5: Full GC epilog", _gc_timer);
1125   ShenandoahHeap* heap = ShenandoahHeap::heap();
1126 
1127   // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
1128   // and must ensure the bitmap is in sync.
1129   {
1130     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
1131     ShenandoahMCResetCompleteBitmapTask task;
1132     heap->workers()->run_task(&task);
1133   }
1134 
1135   // Bring regions in proper states after the collection, and set heap properties.
1136   {
1137     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);

1138     ShenandoahPostCompactClosure post_compact;
1139     heap->heap_region_iterate(&post_compact);
1140     post_compact.update_generation_usage();
1141 
1142     if (heap->mode()->is_generational()) {
1143       ShenandoahGenerationalFullGC::balance_generations_after_gc(heap);
1144     }
1145 
1146     heap->collection_set()->clear();
1147     size_t young_cset_regions, old_cset_regions;
1148     size_t first_old, last_old, num_old;
1149     heap->free_set()->prepare_to_rebuild(young_cset_regions, old_cset_regions, first_old, last_old, num_old);
1150 
1151     // We also do not expand old generation size following Full GC because we have scrambled age populations and
1152     // no longer have objects separated by age into distinct regions.
1153     if (heap->mode()->is_generational()) {
1154       ShenandoahGenerationalFullGC::compute_balances();
1155     }
1156 
1157     heap->free_set()->finish_rebuild(young_cset_regions, old_cset_regions, num_old);
1158 
1159     heap->clear_cancelled_gc(true /* clear oom handler */);
1160   }
1161 
1162   _preserved_marks->restore(heap->workers());
1163   _preserved_marks->reclaim();
1164 
1165   // We defer generation resizing actions until after cset regions have been recycled.  We do this even following an
1166   // abbreviated cycle.
1167   if (heap->mode()->is_generational()) {
1168     ShenandoahGenerationalFullGC::balance_generations_after_rebuilding_free_set();
1169     ShenandoahGenerationalFullGC::rebuild_remembered_set(heap);
1170   }
1171 }
< prev index next >