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