1 /*
   2  * Copyright (c) 2014, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "code/codeCache.hpp"
  27 #include "gc/shared/gcTraceTime.inline.hpp"
  28 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
  29 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  30 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  31 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  32 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
  33 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  34 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  35 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  36 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  37 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  38 #include "gc/shenandoah/shenandoahTraversalGC.hpp"
  39 #include "gc/shenandoah/shenandoahTaskqueue.inline.hpp"
  40 #include "gc/shenandoah/shenandoahUtils.hpp"
  41 #include "gc/shenandoah/shenandoahVerifier.hpp"
  42 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  43 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  44 #include "memory/metaspace.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "runtime/thread.hpp"
  47 #include "utilities/copy.hpp"
  48 #include "utilities/growableArray.hpp"
  49 #include "gc/shared/workgroup.hpp"
  50 
  51 void ShenandoahMarkCompact::initialize(GCTimer* gc_timer) {
  52   _gc_timer = gc_timer;
  53 }
  54 
  55 void ShenandoahMarkCompact::do_it(GCCause::Cause gc_cause) {
  56   ShenandoahHeap* heap = ShenandoahHeap::heap();
  57 
  58   if (ShenandoahVerify) {
  59     heap->verifier()->verify_before_fullgc();
  60   }
  61 
  62   if (VerifyBeforeGC) {
  63     Universe::verify();
  64   }
  65 
  66   heap->set_full_gc_in_progress(true);
  67 
  68   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
  69   assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped");
  70 
  71   {
  72     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdumps);
  73     heap->pre_full_gc_dump(_gc_timer);
  74   }
  75 
  76   {
  77     ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare);
  78     // Full GC is supposed to recover from any GC state:
  79 
  80     // a0. Remember if we have forwarded objects
  81     bool has_forwarded_objects = heap->has_forwarded_objects();
  82 
  83     // a1. Cancel evacuation, if in progress
  84     if (heap->is_evacuation_in_progress()) {
  85       heap->set_evacuation_in_progress(false);
  86     }
  87     assert(!heap->is_evacuation_in_progress(), "sanity");
  88 
  89     // a2. Cancel update-refs, if in progress
  90     if (heap->is_update_refs_in_progress()) {
  91       heap->set_update_refs_in_progress(false);
  92     }
  93     assert(!heap->is_update_refs_in_progress(), "sanity");
  94 
  95     // a3. Cancel concurrent traversal GC, if in progress
  96     if (heap->is_concurrent_traversal_in_progress()) {
  97       heap->traversal_gc()->reset();
  98       heap->set_concurrent_traversal_in_progress(false);
  99     }
 100 
 101     // b. Cancel concurrent mark, if in progress
 102     if (heap->is_concurrent_mark_in_progress()) {
 103       heap->concurrent_mark()->cancel();
 104       heap->stop_concurrent_marking();
 105     }
 106     assert(!heap->is_concurrent_mark_in_progress(), "sanity");
 107 
 108     // c. Reset the bitmaps for new marking
 109     heap->reset_mark_bitmap();
 110     assert(heap->marking_context()->is_bitmap_clear(), "sanity");
 111     assert(!heap->marking_context()->is_complete(), "sanity");
 112 
 113     // d. Abandon reference discovery and clear all discovered references.
 114     ReferenceProcessor* rp = heap->ref_processor();
 115     rp->disable_discovery();
 116     rp->abandon_partial_discovery();
 117     rp->verify_no_references_recorded();
 118 
 119     // e. Set back forwarded objects bit back, in case some steps above dropped it.
 120     heap->set_has_forwarded_objects(has_forwarded_objects);
 121   }
 122 
 123   heap->make_parsable(true);
 124 
 125   OrderAccess::fence();
 126 
 127   phase1_mark_heap();
 128 
 129   // Once marking is done, which may have fixed up forwarded objects, we can drop it.
 130   // Coming out of Full GC, we would not have any forwarded objects.
 131   // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
 132   heap->set_has_forwarded_objects(false);
 133 
 134   heap->set_full_gc_move_in_progress(true);
 135 
 136   // Setup workers for the rest
 137   OrderAccess::fence();
 138 
 139   // Initialize worker slices
 140   ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
 141   for (uint i = 0; i < heap->max_workers(); i++) {
 142     worker_slices[i] = new ShenandoahHeapRegionSet();
 143   }
 144 
 145   {
 146     // The rest of code performs region moves, where region status is undefined
 147     // until all phases run together.
 148     ShenandoahHeapLocker lock(heap->lock());
 149 
 150     phase2_calculate_target_addresses(worker_slices);
 151 
 152     OrderAccess::fence();
 153 
 154     phase3_update_references();
 155 
 156     phase4_compact_objects(worker_slices);
 157   }
 158 
 159   // Resize metaspace
 160   MetaspaceGC::compute_new_size();
 161 
 162   // Free worker slices
 163   for (uint i = 0; i < heap->max_workers(); i++) {
 164     delete worker_slices[i];
 165   }
 166   FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
 167 
 168   JvmtiExport::gc_epilogue();
 169 
 170   heap->set_full_gc_move_in_progress(false);
 171   heap->set_full_gc_in_progress(false);
 172 
 173   if (ShenandoahVerify) {
 174     heap->verifier()->verify_after_fullgc();
 175   }
 176 
 177   if (VerifyAfterGC) {
 178     Universe::verify();
 179   }
 180 
 181   {
 182     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdumps);
 183     heap->post_full_gc_dump(_gc_timer);
 184   }
 185 }
 186 
 187 class ShenandoahPrepareForMarkClosure: public ShenandoahHeapRegionClosure {
 188 private:
 189   ShenandoahMarkingContext* const _ctx;
 190 
 191 public:
 192   ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
 193 
 194   void heap_region_do(ShenandoahHeapRegion *r) {
 195     _ctx->capture_top_at_mark_start(r);
 196     r->clear_live_data();
 197     r->set_concurrent_iteration_safe_limit(r->top());
 198   }
 199 };
 200 
 201 void ShenandoahMarkCompact::phase1_mark_heap() {
 202   GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
 203   ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
 204 
 205   ShenandoahHeap* heap = ShenandoahHeap::heap();
 206 
 207   ShenandoahPrepareForMarkClosure cl;
 208   heap->heap_region_iterate(&cl);
 209 
 210   ShenandoahConcurrentMark* cm = heap->concurrent_mark();
 211 
 212   heap->set_process_references(heap->heuristics()->can_process_references());
 213   heap->set_unload_classes(heap->heuristics()->can_unload_classes());
 214 
 215   ReferenceProcessor* rp = heap->ref_processor();
 216   // enable ("weak") refs discovery
 217   rp->enable_discovery(true /*verify_no_refs*/);
 218   rp->setup_policy(true); // forcefully purge all soft references
 219   rp->set_active_mt_degree(heap->workers()->active_workers());
 220 
 221   cm->update_roots(ShenandoahPhaseTimings::full_gc_roots);
 222   cm->mark_roots(ShenandoahPhaseTimings::full_gc_roots);
 223   cm->finish_mark_from_roots(/* full_gc = */ true);
 224 
 225   heap->mark_complete_marking_context();
 226 }
 227 
 228 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
 229 private:
 230   ShenandoahMarkCompact*   const _mark_compact;
 231   ShenandoahHeap*          const _heap;
 232   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 233   int _empty_regions_pos;
 234   ShenandoahHeapRegion*          _to_region;
 235   ShenandoahHeapRegion*          _from_region;
 236   HeapWord* _compact_point;
 237 
 238 public:
 239   ShenandoahPrepareForCompactionObjectClosure(ShenandoahMarkCompact* mc, GrowableArray<ShenandoahHeapRegion*>& empty_regions, ShenandoahHeapRegion* to_region) :
 240     _mark_compact(mc),
 241     _heap(ShenandoahHeap::heap()),
 242     _empty_regions(empty_regions),
 243     _empty_regions_pos(0),
 244     _to_region(to_region),
 245     _from_region(NULL),
 246     _compact_point(to_region->bottom()) {}
 247 
 248   void set_from_region(ShenandoahHeapRegion* from_region) {
 249     _from_region = from_region;
 250   }
 251 
 252   void finish_region() {
 253     assert(_to_region != NULL, "should not happen");
 254     _to_region->set_new_top(_compact_point);
 255   }
 256 
 257   bool is_compact_same_region() {
 258     return _from_region == _to_region;
 259   }
 260 
 261   int empty_regions_pos() {
 262     return _empty_regions_pos;
 263   }
 264 
 265   void do_object(oop p) {
 266     assert(_from_region != NULL, "must set before work");
 267     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 268     assert(!_heap->complete_marking_context()->allocated_after_mark_start((HeapWord*) p), "must be truly marked");
 269 
 270     size_t obj_size = p->size();
 271     if (_compact_point + obj_size > _to_region->end()) {
 272       finish_region();
 273 
 274       // Object doesn't fit. Pick next empty region and start compacting there.
 275       ShenandoahHeapRegion* new_to_region;
 276       if (_empty_regions_pos < _empty_regions.length()) {
 277         new_to_region = _empty_regions.at(_empty_regions_pos);
 278         _empty_regions_pos++;
 279       } else {
 280         // Out of empty region? Compact within the same region.
 281         new_to_region = _from_region;
 282       }
 283 
 284       assert(new_to_region != _to_region, "must not reuse same to-region");
 285       assert(new_to_region != NULL, "must not be NULL");
 286       _to_region = new_to_region;
 287       _compact_point = _to_region->bottom();
 288     }
 289 
 290     // Object fits into current region, record new location:
 291     assert(_compact_point + obj_size <= _to_region->end(), "must fit");
 292     shenandoah_assert_not_forwarded(NULL, p);
 293     _mark_compact->preserve_mark(p);
 294     p->forward_to(oop(_compact_point));
 295     _compact_point += obj_size;
 296   }
 297 };
 298 
 299 class ShenandoahPrepareForCompactionTask : public AbstractGangTask {
 300 private:
 301   ShenandoahMarkCompact*    const _mark_compact;
 302   ShenandoahHeap*           const _heap;
 303   ShenandoahHeapRegionSet** const _worker_slices;
 304   ShenandoahRegionIterator        _heap_regions;
 305 
 306   ShenandoahHeapRegion* next_from_region(ShenandoahHeapRegionSet* slice) {
 307     ShenandoahHeapRegion* from_region = _heap_regions.next();
 308 
 309     while (from_region != NULL && (!from_region->is_move_allowed() || from_region->is_humongous())) {
 310       from_region = _heap_regions.next();
 311     }
 312 
 313     if (from_region != NULL) {
 314       assert(slice != NULL, "sanity");
 315       assert(!from_region->is_humongous(), "this path cannot handle humongous regions");
 316       assert(from_region->is_move_allowed(), "only regions that can be moved in mark-compact");
 317       slice->add_region(from_region);
 318     }
 319 
 320     return from_region;
 321   }
 322 
 323 public:
 324   ShenandoahPrepareForCompactionTask(ShenandoahMarkCompact* mc, ShenandoahHeapRegionSet** worker_slices) :
 325     AbstractGangTask("Shenandoah Prepare For Compaction Task"),
 326     _mark_compact(mc),
 327     _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
 328   }
 329 
 330   void work(uint worker_id) {
 331     ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
 332     ShenandoahHeapRegion* from_region = next_from_region(slice);
 333     // No work?
 334     if (from_region == NULL) {
 335       return;
 336     }
 337 
 338     // Sliding compaction. Walk all regions in the slice, and compact them.
 339     // Remember empty regions and reuse them as needed.
 340     ResourceMark rm;
 341     GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
 342     ShenandoahPrepareForCompactionObjectClosure cl(_mark_compact, empty_regions, from_region);
 343     while (from_region != NULL) {
 344       cl.set_from_region(from_region);
 345       if (from_region->has_live()) {
 346         _heap->marked_object_iterate(from_region, &cl);
 347       }
 348 
 349       // Compacted the region to somewhere else? From-region is empty then.
 350       if (!cl.is_compact_same_region()) {
 351         empty_regions.append(from_region);
 352       }
 353       from_region = next_from_region(slice);
 354     }
 355     cl.finish_region();
 356 
 357     // Mark all remaining regions as empty
 358     for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
 359       ShenandoahHeapRegion* r = empty_regions.at(pos);
 360       r->set_new_top(r->bottom());
 361     }
 362   }
 363 };
 364 
 365 void ShenandoahMarkCompact::calculate_target_humongous_objects() {
 366   ShenandoahHeap* heap = ShenandoahHeap::heap();
 367 
 368   // Compute the new addresses for humongous objects. We need to do this after addresses
 369   // for regular objects are calculated, and we know what regions in heap suffix are
 370   // available for humongous moves.
 371   //
 372   // Scan the heap backwards, because we are compacting humongous regions towards the end.
 373   // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
 374   // humongous start there.
 375   //
 376   // The complication is potential non-movable regions during the scan. If such region is
 377   // detected, then sliding restarts towards that non-movable region.
 378 
 379   size_t to_begin = heap->num_regions();
 380   size_t to_end = heap->num_regions();
 381 
 382   for (size_t c = heap->num_regions(); c > 0; c--) {
 383     ShenandoahHeapRegion *r = heap->get_region(c - 1);
 384     if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
 385       // To-region candidate: record this, and continue scan
 386       to_begin = r->region_number();
 387       continue;
 388     }
 389 
 390     if (r->is_humongous_start() && r->is_move_allowed()) {
 391       // From-region candidate: movable humongous region
 392       oop old_obj = oop(r->bottom());
 393       size_t words_size = old_obj->size();
 394       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 395 
 396       size_t start = to_end - num_regions;
 397 
 398       if (start >= to_begin && start != r->region_number()) {
 399         // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
 400         old_obj->forward_to(oop(heap->get_region(start)->bottom()));
 401         to_end = start;
 402         continue;
 403       }
 404     }
 405 
 406     // Failed to fit. Scan starting from current region.
 407     to_begin = r->region_number();
 408     to_end = r->region_number();
 409   }
 410 }
 411 
 412 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
 413 private:
 414   ShenandoahHeap* const _heap;
 415 
 416 public:
 417   ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {}
 418   void heap_region_do(ShenandoahHeapRegion* r) {
 419     if (r->is_trash()) {
 420       r->recycle();
 421     }
 422     if (r->is_cset()) {
 423       r->make_regular_bypass();
 424     }
 425     if (r->is_empty_uncommitted()) {
 426       r->make_committed_bypass();
 427     }
 428     assert (r->is_committed(), "only committed regions in heap now, see region " SIZE_FORMAT, r->region_number());
 429 
 430     // Record current region occupancy: this communicates empty regions are free
 431     // to the rest of Full GC code.
 432     r->set_new_top(r->top());
 433   }
 434 };
 435 
 436 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure {
 437 private:
 438   ShenandoahHeap* const _heap;
 439   ShenandoahMarkingContext* const _ctx;
 440 
 441 public:
 442   ShenandoahTrashImmediateGarbageClosure() :
 443     _heap(ShenandoahHeap::heap()),
 444     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
 445 
 446   void heap_region_do(ShenandoahHeapRegion* r) {
 447     if (r->is_humongous_start()) {
 448       oop humongous_obj = oop(r->bottom());
 449       if (!_ctx->is_marked(humongous_obj)) {
 450         assert(!r->has_live(),
 451                "Region " SIZE_FORMAT " is not marked, should not have live", r->region_number());
 452         _heap->trash_humongous_region_at(r);
 453       } else {
 454         assert(r->has_live(),
 455                "Region " SIZE_FORMAT " should have live", r->region_number());
 456       }
 457     } else if (r->is_humongous_continuation()) {
 458       // If we hit continuation, the non-live humongous starts should have been trashed already
 459       assert(r->humongous_start_region()->has_live(),
 460              "Region " SIZE_FORMAT " should have live", r->region_number());
 461     } else if (r->is_regular()) {
 462       if (!r->has_live()) {
 463         r->make_trash_immediate();
 464       }
 465     }
 466   }
 467 };
 468 
 469 void ShenandoahMarkCompact::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
 470   GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
 471   ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
 472 
 473   ShenandoahHeap* heap = ShenandoahHeap::heap();
 474 
 475   {
 476     // Trash the immediately collectible regions before computing addresses
 477     ShenandoahTrashImmediateGarbageClosure tigcl;
 478     heap->heap_region_iterate(&tigcl);
 479 
 480     // Make sure regions are in good state: committed, active, clean.
 481     // This is needed because we are potentially sliding the data through them.
 482     ShenandoahEnsureHeapActiveClosure ecl;
 483     heap->heap_region_iterate(&ecl);
 484   }
 485 
 486   // Compute the new addresses for regular objects
 487   {
 488     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
 489     ShenandoahPrepareForCompactionTask prepare_task(this, worker_slices);
 490     heap->workers()->run_task(&prepare_task);
 491   }
 492 
 493   // Compute the new addresses for humongous objects
 494   {
 495     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);
 496     calculate_target_humongous_objects();
 497   }
 498 }
 499 
 500 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure {
 501 private:
 502   ShenandoahHeap* const _heap;
 503   ShenandoahMarkingContext* const _ctx;
 504 
 505   template <class T>
 506   inline void do_oop_work(T* p) {
 507     T o = RawAccess<>::oop_load(p);
 508     if (!CompressedOops::is_null(o)) {
 509       oop obj = CompressedOops::decode_not_null(o);
 510       assert(_ctx->is_marked(obj), "must be marked");
 511       if (obj->is_forwarded()) {
 512         oop forw = obj->forwardee();
 513         RawAccess<IS_NOT_NULL>::oop_store(p, forw);
 514       }
 515     }
 516   }
 517 
 518 public:
 519   ShenandoahAdjustPointersClosure() :
 520     _heap(ShenandoahHeap::heap()),
 521     _ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
 522 
 523   void do_oop(oop* p)       { do_oop_work(p); }
 524   void do_oop(narrowOop* p) { do_oop_work(p); }
 525 };
 526 
 527 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure {
 528 private:
 529   ShenandoahHeap* const _heap;
 530   ShenandoahAdjustPointersClosure _cl;
 531 
 532 public:
 533   ShenandoahAdjustPointersObjectClosure() :
 534     _heap(ShenandoahHeap::heap()) {
 535   }
 536   void do_object(oop p) {
 537     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 538     p->oop_iterate(&_cl);
 539   }
 540 };
 541 
 542 class ShenandoahAdjustPointersTask : public AbstractGangTask {
 543 private:
 544   ShenandoahHeap*          const _heap;
 545   ShenandoahRegionIterator       _regions;
 546 
 547 public:
 548   ShenandoahAdjustPointersTask() :
 549     AbstractGangTask("Shenandoah Adjust Pointers Task"),
 550     _heap(ShenandoahHeap::heap()) {
 551   }
 552 
 553   void work(uint worker_id) {
 554     ShenandoahAdjustPointersObjectClosure obj_cl;
 555     ShenandoahHeapRegion* r = _regions.next();
 556     while (r != NULL) {
 557       if (!r->is_humongous_continuation() && r->has_live()) {
 558         _heap->marked_object_iterate(r, &obj_cl);
 559       }
 560       r = _regions.next();
 561     }
 562   }
 563 };
 564 
 565 class ShenandoahAdjustRootPointersTask : public AbstractGangTask {
 566 private:
 567   ShenandoahRootProcessor* _rp;
 568 
 569 public:
 570   ShenandoahAdjustRootPointersTask(ShenandoahRootProcessor* rp) :
 571     AbstractGangTask("Shenandoah Adjust Root Pointers Task"),
 572     _rp(rp) {}
 573 
 574   void work(uint worker_id) {
 575     ShenandoahAdjustPointersClosure cl;
 576     CLDToOopClosure adjust_cld_closure(&cl, ClassLoaderData::_claim_strong);
 577     MarkingCodeBlobClosure adjust_code_closure(&cl,
 578                                              CodeBlobToOopClosure::FixRelocations);
 579 
 580     _rp->update_all_roots<AlwaysTrueClosure>(&cl,
 581                                              &adjust_cld_closure,
 582                                              &adjust_code_closure, NULL, worker_id);
 583   }
 584 };
 585 
 586 void ShenandoahMarkCompact::phase3_update_references() {
 587   GCTraceTime(Info, gc, phases) time("Phase 3: Adjust pointers", _gc_timer);
 588   ShenandoahGCPhase adjust_pointer_phase(ShenandoahPhaseTimings::full_gc_adjust_pointers);
 589 
 590   ShenandoahHeap* heap = ShenandoahHeap::heap();
 591 
 592   WorkGang* workers = heap->workers();
 593   uint nworkers = workers->active_workers();
 594   {
 595 #if COMPILER2_OR_JVMCI
 596     DerivedPointerTable::clear();
 597 #endif
 598     ShenandoahRootProcessor rp(heap, nworkers, ShenandoahPhaseTimings::full_gc_roots);
 599     ShenandoahAdjustRootPointersTask task(&rp);
 600     workers->run_task(&task);
 601 #if COMPILER2_OR_JVMCI
 602     DerivedPointerTable::update_pointers();
 603 #endif
 604   }
 605 
 606   ShenandoahAdjustPointersTask adjust_pointers_task;
 607   workers->run_task(&adjust_pointers_task);
 608 
 609   adjust_marks();
 610 }
 611 
 612 class ShenandoahCompactObjectsClosure : public ObjectClosure {
 613 private:
 614   ShenandoahHeap* const _heap;
 615   uint            const _worker_id;
 616 
 617 public:
 618   ShenandoahCompactObjectsClosure(uint worker_id) :
 619     _heap(ShenandoahHeap::heap()), _worker_id(worker_id) {}
 620 
 621   void do_object(oop p) {
 622     assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
 623     size_t size = (size_t)p->size();
 624     if (p->is_forwarded()) {
 625       HeapWord* compact_from = (HeapWord*) p;
 626       HeapWord* compact_to = (HeapWord*) p->forwardee();
 627       Copy::aligned_conjoint_words(compact_from, compact_to, size);
 628       oop new_obj = oop(compact_to);
 629       new_obj->init_mark();
 630     }
 631   }
 632 };
 633 
 634 class ShenandoahCompactObjectsTask : public AbstractGangTask {
 635 private:
 636   ShenandoahHeap* const _heap;
 637   ShenandoahHeapRegionSet** const _worker_slices;
 638 
 639 public:
 640   ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** worker_slices) :
 641     AbstractGangTask("Shenandoah Compact Objects Task"),
 642     _heap(ShenandoahHeap::heap()),
 643     _worker_slices(worker_slices) {
 644   }
 645 
 646   void work(uint worker_id) {
 647     ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
 648 
 649     ShenandoahCompactObjectsClosure cl(worker_id);
 650     ShenandoahHeapRegion* r = slice.next();
 651     while (r != NULL) {
 652       assert(!r->is_humongous(), "must not get humongous regions here");
 653       if (r->has_live()) {
 654         _heap->marked_object_iterate(r, &cl);
 655       }
 656       r->set_top(r->new_top());
 657       r = slice.next();
 658     }
 659   }
 660 };
 661 
 662 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
 663 private:
 664   ShenandoahHeap* const _heap;
 665   size_t _live;
 666 
 667 public:
 668   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()), _live(0) {
 669     _heap->free_set()->clear();
 670   }
 671 
 672   void heap_region_do(ShenandoahHeapRegion* r) {
 673     assert (!r->is_cset(), "cset regions should have been demoted already");
 674 
 675     // Need to reset the complete-top-at-mark-start pointer here because
 676     // the complete marking bitmap is no longer valid. This ensures
 677     // size-based iteration in marked_object_iterate().
 678     // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
 679     // pinned regions.
 680     if (!r->is_pinned()) {
 681       _heap->complete_marking_context()->reset_top_at_mark_start(r);
 682     }
 683 
 684     size_t live = r->used();
 685 
 686     // Make empty regions that have been allocated into regular
 687     if (r->is_empty() && live > 0) {
 688       r->make_regular_bypass();
 689     }
 690 
 691     // Reclaim regular regions that became empty
 692     if (r->is_regular() && live == 0) {
 693       r->make_trash();
 694     }
 695 
 696     // Recycle all trash regions
 697     if (r->is_trash()) {
 698       live = 0;
 699       r->recycle();
 700     }
 701 
 702     r->set_live_data(live);
 703     r->reset_alloc_metadata_to_shared();
 704     _live += live;
 705   }
 706 
 707   size_t get_live() {
 708     return _live;
 709   }
 710 };
 711 
 712 void ShenandoahMarkCompact::compact_humongous_objects() {
 713   // Compact humongous regions, based on their fwdptr objects.
 714   //
 715   // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
 716   // humongous regions are already compacted, and do not require further moves, which alleviates
 717   // sliding costs. We may consider doing this in parallel in future.
 718 
 719   ShenandoahHeap* heap = ShenandoahHeap::heap();
 720 
 721   for (size_t c = heap->num_regions(); c > 0; c--) {
 722     ShenandoahHeapRegion* r = heap->get_region(c - 1);
 723     if (r->is_humongous_start()) {
 724       oop old_obj = oop(r->bottom());
 725       if (!old_obj->is_forwarded()) {
 726         // No need to move the object, it stays at the same slot
 727         continue;
 728       }
 729       size_t words_size = old_obj->size();
 730       size_t num_regions = ShenandoahHeapRegion::required_regions(words_size * HeapWordSize);
 731 
 732       size_t old_start = r->region_number();
 733       size_t old_end   = old_start + num_regions - 1;
 734       size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
 735       size_t new_end   = new_start + num_regions - 1;
 736       assert(old_start != new_start, "must be real move");
 737       assert (r->is_move_allowed(), "should be movable");
 738 
 739       Copy::aligned_conjoint_words(heap->get_region(old_start)->bottom(),
 740                                    heap->get_region(new_start)->bottom(),
 741                                    ShenandoahHeapRegion::region_size_words()*num_regions);
 742 
 743       oop new_obj = oop(heap->get_region(new_start)->bottom());
 744       new_obj->init_mark();
 745 
 746       {
 747         for (size_t c = old_start; c <= old_end; c++) {
 748           ShenandoahHeapRegion* r = heap->get_region(c);
 749           r->make_regular_bypass();
 750           r->set_top(r->bottom());
 751         }
 752 
 753         for (size_t c = new_start; c <= new_end; c++) {
 754           ShenandoahHeapRegion* r = heap->get_region(c);
 755           if (c == new_start) {
 756             r->make_humongous_start_bypass();
 757           } else {
 758             r->make_humongous_cont_bypass();
 759           }
 760 
 761           // Trailing region may be non-full, record the remainder there
 762           size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
 763           if ((c == new_end) && (remainder != 0)) {
 764             r->set_top(r->bottom() + remainder);
 765           } else {
 766             r->set_top(r->end());
 767           }
 768 
 769           r->reset_alloc_metadata_to_shared();
 770         }
 771       }
 772     }
 773   }
 774 }
 775 
 776 // This is slightly different to ShHeap::reset_next_mark_bitmap:
 777 // we need to remain able to walk pinned regions.
 778 // Since pinned region do not move and don't get compacted, we will get holes with
 779 // unreachable objects in them (which may have pointers to unloaded Klasses and thus
 780 // cannot be iterated over using oop->size(). The only way to safely iterate over those is using
 781 // a valid marking bitmap and valid TAMS pointer. This class only resets marking
 782 // bitmaps for un-pinned regions, and later we only reset TAMS for unpinned regions.
 783 class ShenandoahMCResetCompleteBitmapTask : public AbstractGangTask {
 784 private:
 785   ShenandoahRegionIterator _regions;
 786 
 787 public:
 788   ShenandoahMCResetCompleteBitmapTask() :
 789     AbstractGangTask("Parallel Reset Bitmap Task") {
 790   }
 791 
 792   void work(uint worker_id) {
 793     ShenandoahHeapRegion* region = _regions.next();
 794     ShenandoahHeap* heap = ShenandoahHeap::heap();
 795     ShenandoahMarkingContext* const ctx = heap->complete_marking_context();
 796     while (region != NULL) {
 797       if (heap->is_bitmap_slice_committed(region) && !region->is_pinned() && region->has_live()) {
 798         ctx->clear_bitmap(region);
 799       }
 800       region = _regions.next();
 801     }
 802   }
 803 };
 804 
 805 void ShenandoahMarkCompact::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) {
 806   GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer);
 807   ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects);
 808 
 809   ShenandoahHeap* heap = ShenandoahHeap::heap();
 810 
 811   // Compact regular objects first
 812   {
 813     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular);
 814     ShenandoahCompactObjectsTask compact_task(worker_slices);
 815     heap->workers()->run_task(&compact_task);
 816   }
 817 
 818   // Compact humongous objects after regular object moves
 819   {
 820     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
 821     compact_humongous_objects();
 822   }
 823 
 824   restore_marks();
 825 
 826   // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
 827   // and must ensure the bitmap is in sync.
 828   {
 829     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
 830     ShenandoahMCResetCompleteBitmapTask task;
 831     heap->workers()->run_task(&task);
 832   }
 833 
 834   // Bring regions in proper states after the collection, and set heap properties.
 835   {
 836     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
 837 
 838     ShenandoahPostCompactClosure post_compact;
 839     heap->heap_region_iterate(&post_compact);
 840     heap->set_used(post_compact.get_live());
 841 
 842     heap->collection_set()->clear();
 843     heap->free_set()->rebuild();
 844   }
 845 
 846   heap->clear_cancelled_gc();
 847 }
 848 
 849 void ShenandoahMarkCompact::preserve_mark(oop obj) {
 850   markOop mark = obj->mark_raw();
 851   if (mark->must_be_preserved(obj)) {
 852     MutexLocker ml(&_preserved_mark_lock, Mutex::_no_safepoint_check_flag);
 853     assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),
 854            "inconsistent preserved oop stacks: oop_stack size: " SIZE_FORMAT ", mark_stack size: " SIZE_FORMAT, _preserved_oop_stack.size(), _preserved_mark_stack.size());
 855     _preserved_mark_stack.push(mark);
 856     _preserved_oop_stack.push(obj);
 857   }
 858 }
 859 
 860 void ShenandoahMarkCompact::restore_marks() {
 861   MutexLocker ml(&_preserved_mark_lock, Mutex::_no_safepoint_check_flag);
 862   assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),
 863          "inconsistent preserved oop stacks: oop_stack size: " SIZE_FORMAT ", mark_stack size: " SIZE_FORMAT, _preserved_oop_stack.size(), _preserved_mark_stack.size());
 864   while (!_preserved_oop_stack.is_empty()) {
 865     oop obj       = _preserved_oop_stack.pop();
 866     markOop mark  = _preserved_mark_stack.pop();
 867     obj->set_mark_raw(mark);
 868   }
 869 }
 870 
 871 void ShenandoahMarkCompact::adjust_marks() {
 872   MutexLocker ml(&_preserved_mark_lock, Mutex::_no_safepoint_check_flag);
 873   assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),
 874          "inconsistent preserved oop stacks: oop_stack size: " SIZE_FORMAT ", mark_stack size: " SIZE_FORMAT, _preserved_oop_stack.size(), _preserved_mark_stack.size());
 875   StackIterator<oop, mtGC> iter(_preserved_oop_stack);
 876   while (!iter.is_empty()) {
 877     oop* p = iter.next_addr();
 878     oop obj = *p;
 879     oop fwd = obj->forwardee();
 880     *p = fwd;
 881   }
 882 }