1 /*
   2  * Copyright (c) 2005, 2025, Oracle and/or its affiliates. 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 "classfile/classLoaderDataGraph.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/stringTable.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "compiler/oopMap.hpp"
  33 #include "gc/parallel/objectStartArray.inline.hpp"
  34 #include "gc/parallel/parallelArguments.hpp"
  35 #include "gc/parallel/parallelScavengeHeap.inline.hpp"
  36 #include "gc/parallel/parMarkBitMap.inline.hpp"
  37 #include "gc/parallel/psAdaptiveSizePolicy.hpp"
  38 #include "gc/parallel/psCompactionManager.inline.hpp"
  39 #include "gc/parallel/psOldGen.hpp"
  40 #include "gc/parallel/psParallelCompact.inline.hpp"
  41 #include "gc/parallel/psPromotionManager.inline.hpp"
  42 #include "gc/parallel/psRootType.hpp"
  43 #include "gc/parallel/psScavenge.hpp"
  44 #include "gc/parallel/psStringDedup.hpp"
  45 #include "gc/parallel/psYoungGen.hpp"
  46 #include "gc/shared/classUnloadingContext.hpp"
  47 #include "gc/shared/fullGCForwarding.inline.hpp"
  48 #include "gc/shared/gcCause.hpp"
  49 #include "gc/shared/gcHeapSummary.hpp"
  50 #include "gc/shared/gcId.hpp"
  51 #include "gc/shared/gcLocker.hpp"
  52 #include "gc/shared/gcTimer.hpp"
  53 #include "gc/shared/gcTrace.hpp"
  54 #include "gc/shared/gcTraceTime.inline.hpp"
  55 #include "gc/shared/gcVMOperations.hpp"
  56 #include "gc/shared/isGCActiveMark.hpp"
  57 #include "gc/shared/oopStorage.inline.hpp"
  58 #include "gc/shared/oopStorageSet.inline.hpp"
  59 #include "gc/shared/oopStorageSetParState.inline.hpp"
  60 #include "gc/shared/preservedMarks.inline.hpp"
  61 #include "gc/shared/referencePolicy.hpp"
  62 #include "gc/shared/referenceProcessor.hpp"
  63 #include "gc/shared/referenceProcessorPhaseTimes.hpp"
  64 #include "gc/shared/spaceDecorator.hpp"
  65 #include "gc/shared/taskTerminator.hpp"
  66 #include "gc/shared/weakProcessor.inline.hpp"
  67 #include "gc/shared/workerPolicy.hpp"
  68 #include "gc/shared/workerThread.hpp"
  69 #include "gc/shared/workerUtils.hpp"
  70 #include "logging/log.hpp"
  71 #include "memory/iterator.inline.hpp"
  72 #include "memory/memoryReserver.hpp"
  73 #include "memory/metaspaceUtils.hpp"
  74 #include "memory/resourceArea.hpp"
  75 #include "memory/universe.hpp"
  76 #include "nmt/memTracker.hpp"
  77 #include "oops/access.inline.hpp"
  78 #include "oops/flatArrayKlass.inline.hpp"
  79 #include "oops/instanceClassLoaderKlass.inline.hpp"
  80 #include "oops/instanceKlass.inline.hpp"
  81 #include "oops/instanceMirrorKlass.inline.hpp"
  82 #include "oops/methodData.hpp"
  83 #include "oops/objArrayKlass.inline.hpp"
  84 #include "oops/oop.inline.hpp"
  85 #include "runtime/atomicAccess.hpp"
  86 #include "runtime/handles.inline.hpp"
  87 #include "runtime/java.hpp"
  88 #include "runtime/safepoint.hpp"
  89 #include "runtime/threads.hpp"
  90 #include "runtime/vmThread.hpp"
  91 #include "services/memoryService.hpp"
  92 #include "utilities/align.hpp"
  93 #include "utilities/debug.hpp"
  94 #include "utilities/events.hpp"
  95 #include "utilities/formatBuffer.hpp"
  96 #include "utilities/macros.hpp"
  97 #include "utilities/stack.inline.hpp"
  98 #if INCLUDE_JVMCI
  99 #include "jvmci/jvmci.hpp"
 100 #endif
 101 
 102 #include <math.h>
 103 
 104 // All sizes are in HeapWords.
 105 const size_t ParallelCompactData::Log2RegionSize  = 16; // 64K words
 106 const size_t ParallelCompactData::RegionSize      = (size_t)1 << Log2RegionSize;
 107 static_assert(ParallelCompactData::RegionSize >= BitsPerWord, "region-start bit word-aligned");
 108 const size_t ParallelCompactData::RegionSizeBytes =
 109   RegionSize << LogHeapWordSize;
 110 const size_t ParallelCompactData::RegionSizeOffsetMask = RegionSize - 1;
 111 const size_t ParallelCompactData::RegionAddrOffsetMask = RegionSizeBytes - 1;
 112 const size_t ParallelCompactData::RegionAddrMask       = ~RegionAddrOffsetMask;
 113 
 114 const ParallelCompactData::RegionData::region_sz_t
 115 ParallelCompactData::RegionData::dc_shift = 27;
 116 
 117 const ParallelCompactData::RegionData::region_sz_t
 118 ParallelCompactData::RegionData::dc_mask = ~0U << dc_shift;
 119 
 120 const ParallelCompactData::RegionData::region_sz_t
 121 ParallelCompactData::RegionData::dc_one = 0x1U << dc_shift;
 122 
 123 const ParallelCompactData::RegionData::region_sz_t
 124 ParallelCompactData::RegionData::los_mask = ~dc_mask;
 125 
 126 const ParallelCompactData::RegionData::region_sz_t
 127 ParallelCompactData::RegionData::dc_claimed = 0x8U << dc_shift;
 128 
 129 const ParallelCompactData::RegionData::region_sz_t
 130 ParallelCompactData::RegionData::dc_completed = 0xcU << dc_shift;
 131 
 132 bool ParallelCompactData::RegionData::is_clear() {
 133   return (_destination == nullptr) &&
 134          (_source_region == 0) &&
 135          (_partial_obj_addr == nullptr) &&
 136          (_partial_obj_size == 0) &&
 137          (_dc_and_los == 0) &&
 138          (_shadow_state == 0);
 139 }
 140 
 141 #ifdef ASSERT
 142 void ParallelCompactData::RegionData::verify_clear() {
 143   assert(_destination == nullptr, "inv");
 144   assert(_source_region == 0, "inv");
 145   assert(_partial_obj_addr == nullptr, "inv");
 146   assert(_partial_obj_size == 0, "inv");
 147   assert(_dc_and_los == 0, "inv");
 148   assert(_shadow_state == 0, "inv");
 149 }
 150 #endif
 151 
 152 SpaceInfo PSParallelCompact::_space_info[PSParallelCompact::last_space_id];
 153 
 154 SpanSubjectToDiscoveryClosure PSParallelCompact::_span_based_discoverer;
 155 ReferenceProcessor* PSParallelCompact::_ref_processor = nullptr;
 156 
 157 void SplitInfo::record(size_t split_region_idx, HeapWord* split_point, size_t preceding_live_words) {
 158   assert(split_region_idx != 0, "precondition");
 159 
 160   // Obj denoted by split_point will be deferred to the next space.
 161   assert(split_point != nullptr, "precondition");
 162 
 163   const ParallelCompactData& sd = PSParallelCompact::summary_data();
 164 
 165   PSParallelCompact::RegionData* split_region_ptr = sd.region(split_region_idx);
 166   assert(preceding_live_words < split_region_ptr->data_size(), "inv");
 167 
 168   HeapWord* preceding_destination = split_region_ptr->destination();
 169   assert(preceding_destination != nullptr, "inv");
 170 
 171   // How many regions does the preceding part occupy
 172   uint preceding_destination_count;
 173   if (preceding_live_words == 0) {
 174     preceding_destination_count = 0;
 175   } else {
 176     // -1 so that the ending address doesn't fall on the region-boundary
 177     if (sd.region_align_down(preceding_destination) ==
 178         sd.region_align_down(preceding_destination + preceding_live_words - 1)) {
 179       preceding_destination_count = 1;
 180     } else {
 181       preceding_destination_count = 2;
 182     }
 183   }
 184 
 185   _split_region_idx = split_region_idx;
 186   _split_point = split_point;
 187   _preceding_live_words = preceding_live_words;
 188   _preceding_destination = preceding_destination;
 189   _preceding_destination_count = preceding_destination_count;
 190 }
 191 
 192 void SplitInfo::clear()
 193 {
 194   _split_region_idx = 0;
 195   _split_point = nullptr;
 196   _preceding_live_words = 0;
 197   _preceding_destination = nullptr;
 198   _preceding_destination_count = 0;
 199   assert(!is_valid(), "sanity");
 200 }
 201 
 202 #ifdef  ASSERT
 203 void SplitInfo::verify_clear()
 204 {
 205   assert(_split_region_idx == 0, "not clear");
 206   assert(_split_point == nullptr, "not clear");
 207   assert(_preceding_live_words == 0, "not clear");
 208   assert(_preceding_destination == nullptr, "not clear");
 209   assert(_preceding_destination_count == 0, "not clear");
 210 }
 211 #endif  // #ifdef ASSERT
 212 
 213 
 214 void PSParallelCompact::print_on(outputStream* st) {
 215   _mark_bitmap.print_on(st);
 216 }
 217 
 218 ParallelCompactData::ParallelCompactData() :
 219   _heap_start(nullptr),
 220   DEBUG_ONLY(_heap_end(nullptr) COMMA)
 221   _region_vspace(nullptr),
 222   _reserved_byte_size(0),
 223   _region_data(nullptr),
 224   _region_count(0) {}
 225 
 226 bool ParallelCompactData::initialize(MemRegion reserved_heap)
 227 {
 228   _heap_start = reserved_heap.start();
 229   const size_t heap_size = reserved_heap.word_size();
 230   DEBUG_ONLY(_heap_end = _heap_start + heap_size;)
 231 
 232   assert(region_align_down(_heap_start) == _heap_start,
 233          "region start not aligned");
 234 
 235   return initialize_region_data(heap_size);
 236 }
 237 
 238 PSVirtualSpace*
 239 ParallelCompactData::create_vspace(size_t count, size_t element_size)
 240 {
 241   const size_t raw_bytes = count * element_size;
 242   const size_t page_sz = os::page_size_for_region_aligned(raw_bytes, 10);
 243   const size_t granularity = os::vm_allocation_granularity();
 244   const size_t rs_align = MAX2(page_sz, granularity);
 245 
 246   _reserved_byte_size = align_up(raw_bytes, rs_align);
 247 
 248   ReservedSpace rs = MemoryReserver::reserve(_reserved_byte_size,
 249                                              rs_align,
 250                                              page_sz,
 251                                              mtGC);
 252 
 253   if (!rs.is_reserved()) {
 254     // Failed to reserve memory.
 255     return nullptr;
 256   }
 257 
 258   os::trace_page_sizes("Parallel Compact Data", raw_bytes, raw_bytes, rs.base(),
 259                        rs.size(), page_sz);
 260 
 261   MemTracker::record_virtual_memory_tag(rs, mtGC);
 262 
 263   PSVirtualSpace* vspace = new PSVirtualSpace(rs, page_sz);
 264 
 265   if (!vspace->expand_by(_reserved_byte_size)) {
 266     // Failed to commit memory.
 267 
 268     delete vspace;
 269 
 270     // Release memory reserved in the space.
 271     MemoryReserver::release(rs);
 272 
 273     return nullptr;
 274   }
 275 
 276   return vspace;
 277 }
 278 
 279 bool ParallelCompactData::initialize_region_data(size_t heap_size)
 280 {
 281   assert(is_aligned(heap_size, RegionSize), "precondition");
 282 
 283   const size_t count = heap_size >> Log2RegionSize;
 284   _region_vspace = create_vspace(count, sizeof(RegionData));
 285   if (_region_vspace != nullptr) {
 286     _region_data = (RegionData*)_region_vspace->reserved_low_addr();
 287     _region_count = count;
 288     return true;
 289   }
 290   return false;
 291 }
 292 
 293 void ParallelCompactData::clear_range(size_t beg_region, size_t end_region) {
 294   assert(beg_region <= _region_count, "beg_region out of range");
 295   assert(end_region <= _region_count, "end_region out of range");
 296 
 297   const size_t region_cnt = end_region - beg_region;
 298   memset(_region_data + beg_region, 0, region_cnt * sizeof(RegionData));
 299 }
 300 
 301 // The total live words on src_region would overflow the target space, so find
 302 // the overflowing object and record the split point. The invariant is that an
 303 // obj should not cross space boundary.
 304 HeapWord* ParallelCompactData::summarize_split_space(size_t src_region,
 305                                                      SplitInfo& split_info,
 306                                                      HeapWord* const destination,
 307                                                      HeapWord* const target_end,
 308                                                      HeapWord** target_next) {
 309   assert(destination <= target_end, "sanity");
 310   assert(destination + _region_data[src_region].data_size() > target_end,
 311     "region should not fit into target space");
 312   assert(is_region_aligned(target_end), "sanity");
 313 
 314   size_t partial_obj_size = _region_data[src_region].partial_obj_size();
 315 
 316   if (destination + partial_obj_size > target_end) {
 317     assert(partial_obj_size > 0, "inv");
 318     // The overflowing obj is from a previous region.
 319     //
 320     // source-regions:
 321     //
 322     // ***************
 323     // |     A|AA    |
 324     // ***************
 325     //       ^
 326     //       | split-point
 327     //
 328     // dest-region:
 329     //
 330     // ********
 331     // |~~~~A |
 332     // ********
 333     //       ^^
 334     //       || target-space-end
 335     //       |
 336     //       | destination
 337     //
 338     // AAA would overflow target-space.
 339     //
 340     HeapWord* overflowing_obj = _region_data[src_region].partial_obj_addr();
 341     size_t split_region = addr_to_region_idx(overflowing_obj);
 342 
 343     // The number of live words before the overflowing object on this split region
 344     size_t preceding_live_words;
 345     if (is_region_aligned(overflowing_obj)) {
 346       preceding_live_words = 0;
 347     } else {
 348       // Words accounted by the overflowing object on the split region
 349       size_t overflowing_size = pointer_delta(region_align_up(overflowing_obj), overflowing_obj);
 350       preceding_live_words = region(split_region)->data_size() - overflowing_size;
 351     }
 352 
 353     split_info.record(split_region, overflowing_obj, preceding_live_words);
 354 
 355     // The [overflowing_obj, src_region_start) part has been accounted for, so
 356     // must move back the new_top, now that this overflowing obj is deferred.
 357     HeapWord* new_top = destination - pointer_delta(region_to_addr(src_region), overflowing_obj);
 358 
 359     // If the overflowing obj was relocated to its original destination,
 360     // those destination regions would have their source_region set. Now that
 361     // this overflowing obj is relocated somewhere else, reset the
 362     // source_region.
 363     {
 364       size_t range_start = addr_to_region_idx(region_align_up(new_top));
 365       size_t range_end = addr_to_region_idx(region_align_up(destination));
 366       for (size_t i = range_start; i < range_end; ++i) {
 367         region(i)->set_source_region(0);
 368       }
 369     }
 370 
 371     // Update new top of target space
 372     *target_next = new_top;
 373 
 374     return overflowing_obj;
 375   }
 376 
 377   // Obj-iteration to locate the overflowing obj
 378   HeapWord* region_start = region_to_addr(src_region);
 379   HeapWord* region_end = region_start + RegionSize;
 380   HeapWord* cur_addr = region_start + partial_obj_size;
 381   size_t live_words = partial_obj_size;
 382 
 383   while (true) {
 384     assert(cur_addr < region_end, "inv");
 385     cur_addr = PSParallelCompact::mark_bitmap()->find_obj_beg(cur_addr, region_end);
 386     // There must be an overflowing obj in this region
 387     assert(cur_addr < region_end, "inv");
 388 
 389     oop obj = cast_to_oop(cur_addr);
 390     size_t obj_size = obj->size();
 391     if (destination + live_words + obj_size > target_end) {
 392       // Found the overflowing obj
 393       split_info.record(src_region, cur_addr, live_words);
 394       *target_next = destination + live_words;
 395       return cur_addr;
 396     }
 397 
 398     live_words += obj_size;
 399     cur_addr += obj_size;
 400   }
 401 }
 402 
 403 size_t ParallelCompactData::live_words_in_space(const MutableSpace* space,
 404                                                 HeapWord** full_region_prefix_end) {
 405   size_t cur_region = addr_to_region_idx(space->bottom());
 406   const size_t end_region = addr_to_region_idx(region_align_up(space->top()));
 407   size_t live_words = 0;
 408   if (full_region_prefix_end == nullptr) {
 409     for (/* empty */; cur_region < end_region; ++cur_region) {
 410       live_words += _region_data[cur_region].data_size();
 411     }
 412   } else {
 413     bool first_set = false;
 414     for (/* empty */; cur_region < end_region; ++cur_region) {
 415       size_t live_words_in_region = _region_data[cur_region].data_size();
 416       if (!first_set && live_words_in_region < RegionSize) {
 417         *full_region_prefix_end = region_to_addr(cur_region);
 418         first_set = true;
 419       }
 420       live_words += live_words_in_region;
 421     }
 422     if (!first_set) {
 423       // All regions are full of live objs.
 424       assert(is_region_aligned(space->top()), "inv");
 425       *full_region_prefix_end = space->top();
 426     }
 427     assert(*full_region_prefix_end != nullptr, "postcondition");
 428     assert(is_region_aligned(*full_region_prefix_end), "inv");
 429     assert(*full_region_prefix_end >= space->bottom(), "in-range");
 430     assert(*full_region_prefix_end <= space->top(), "in-range");
 431   }
 432   return live_words;
 433 }
 434 
 435 bool ParallelCompactData::summarize(SplitInfo& split_info,
 436                                     HeapWord* source_beg, HeapWord* source_end,
 437                                     HeapWord** source_next,
 438                                     HeapWord* target_beg, HeapWord* target_end,
 439                                     HeapWord** target_next)
 440 {
 441   HeapWord* const source_next_val = source_next == nullptr ? nullptr : *source_next;
 442   log_develop_trace(gc, compaction)(
 443       "sb=" PTR_FORMAT " se=" PTR_FORMAT " sn=" PTR_FORMAT
 444       "tb=" PTR_FORMAT " te=" PTR_FORMAT " tn=" PTR_FORMAT,
 445       p2i(source_beg), p2i(source_end), p2i(source_next_val),
 446       p2i(target_beg), p2i(target_end), p2i(*target_next));
 447 
 448   size_t cur_region = addr_to_region_idx(source_beg);
 449   const size_t end_region = addr_to_region_idx(region_align_up(source_end));
 450 
 451   HeapWord *dest_addr = target_beg;
 452   for (/* empty */; cur_region < end_region; cur_region++) {
 453     size_t words = _region_data[cur_region].data_size();
 454 
 455     // Skip empty ones
 456     if (words == 0) {
 457       continue;
 458     }
 459 
 460     if (split_info.is_split(cur_region)) {
 461       assert(words > split_info.preceding_live_words(), "inv");
 462       words -= split_info.preceding_live_words();
 463     }
 464 
 465     _region_data[cur_region].set_destination(dest_addr);
 466 
 467     // If cur_region does not fit entirely into the target space, find a point
 468     // at which the source space can be 'split' so that part is copied to the
 469     // target space and the rest is copied elsewhere.
 470     if (dest_addr + words > target_end) {
 471       assert(source_next != nullptr, "source_next is null when splitting");
 472       *source_next = summarize_split_space(cur_region, split_info, dest_addr,
 473                                            target_end, target_next);
 474       return false;
 475     }
 476 
 477     uint destination_count = split_info.is_split(cur_region)
 478                              ? split_info.preceding_destination_count()
 479                              : 0;
 480 
 481     HeapWord* const last_addr = dest_addr + words - 1;
 482     const size_t dest_region_1 = addr_to_region_idx(dest_addr);
 483     const size_t dest_region_2 = addr_to_region_idx(last_addr);
 484 
 485     // Initially assume that the destination regions will be the same and
 486     // adjust the value below if necessary.  Under this assumption, if
 487     // cur_region == dest_region_2, then cur_region will be compacted
 488     // completely into itself.
 489     destination_count += cur_region == dest_region_2 ? 0 : 1;
 490     if (dest_region_1 != dest_region_2) {
 491       // Destination regions differ; adjust destination_count.
 492       destination_count += 1;
 493       // Data from cur_region will be copied to the start of dest_region_2.
 494       _region_data[dest_region_2].set_source_region(cur_region);
 495     } else if (is_region_aligned(dest_addr)) {
 496       // Data from cur_region will be copied to the start of the destination
 497       // region.
 498       _region_data[dest_region_1].set_source_region(cur_region);
 499     }
 500 
 501     _region_data[cur_region].set_destination_count(destination_count);
 502     dest_addr += words;
 503   }
 504 
 505   *target_next = dest_addr;
 506   return true;
 507 }
 508 
 509 #ifdef ASSERT
 510 void ParallelCompactData::verify_clear() {
 511   for (uint cur_idx = 0; cur_idx < region_count(); ++cur_idx) {
 512     if (!region(cur_idx)->is_clear()) {
 513       log_warning(gc)("Uncleared Region: %u", cur_idx);
 514       region(cur_idx)->verify_clear();
 515     }
 516   }
 517 }
 518 #endif  // #ifdef ASSERT
 519 
 520 STWGCTimer          PSParallelCompact::_gc_timer;
 521 ParallelOldTracer   PSParallelCompact::_gc_tracer;
 522 elapsedTimer        PSParallelCompact::_accumulated_time;
 523 unsigned int        PSParallelCompact::_maximum_compaction_gc_num = 0;
 524 CollectorCounters*  PSParallelCompact::_counters = nullptr;
 525 ParMarkBitMap       PSParallelCompact::_mark_bitmap;
 526 ParallelCompactData PSParallelCompact::_summary_data;
 527 
 528 PSParallelCompact::IsAliveClosure PSParallelCompact::_is_alive_closure;
 529 
 530 class PCAdjustPointerClosure: public BasicOopIterateClosure {
 531   template <typename T>
 532   void do_oop_work(T* p) { PSParallelCompact::adjust_pointer(p); }
 533 
 534 public:
 535   virtual void do_oop(oop* p)                { do_oop_work(p); }
 536   virtual void do_oop(narrowOop* p)          { do_oop_work(p); }
 537 
 538   virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; }
 539 };
 540 
 541 static PCAdjustPointerClosure pc_adjust_pointer_closure;
 542 
 543 bool PSParallelCompact::IsAliveClosure::do_object_b(oop p) { return mark_bitmap()->is_marked(p); }
 544 
 545 void PSParallelCompact::post_initialize() {
 546   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 547   _span_based_discoverer.set_span(heap->reserved_region());
 548   _ref_processor =
 549     new ReferenceProcessor(&_span_based_discoverer,
 550                            ParallelGCThreads,   // mt processing degree
 551                            ParallelGCThreads,   // mt discovery degree
 552                            false,               // concurrent_discovery
 553                            &_is_alive_closure); // non-header is alive closure
 554 
 555   _counters = new CollectorCounters("Parallel full collection pauses", 1);
 556 
 557   // Initialize static fields in ParCompactionManager.
 558   ParCompactionManager::initialize(mark_bitmap());
 559 }
 560 
 561 bool PSParallelCompact::initialize_aux_data() {
 562   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 563   MemRegion mr = heap->reserved_region();
 564   assert(mr.byte_size() != 0, "heap should be reserved");
 565 
 566   initialize_space_info();
 567 
 568   if (!_mark_bitmap.initialize(mr)) {
 569     vm_shutdown_during_initialization(
 570       err_msg("Unable to allocate %zuKB bitmaps for parallel "
 571       "garbage collection for the requested %zuKB heap.",
 572       _mark_bitmap.reserved_byte_size()/K, mr.byte_size()/K));
 573     return false;
 574   }
 575 
 576   if (!_summary_data.initialize(mr)) {
 577     vm_shutdown_during_initialization(
 578       err_msg("Unable to allocate %zuKB card tables for parallel "
 579       "garbage collection for the requested %zuKB heap.",
 580       _summary_data.reserved_byte_size()/K, mr.byte_size()/K));
 581     return false;
 582   }
 583 
 584   return true;
 585 }
 586 
 587 void PSParallelCompact::initialize_space_info()
 588 {
 589   memset(&_space_info, 0, sizeof(_space_info));
 590 
 591   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 592   PSYoungGen* young_gen = heap->young_gen();
 593 
 594   _space_info[old_space_id].set_space(heap->old_gen()->object_space());
 595   _space_info[eden_space_id].set_space(young_gen->eden_space());
 596   _space_info[from_space_id].set_space(young_gen->from_space());
 597   _space_info[to_space_id].set_space(young_gen->to_space());
 598 
 599   _space_info[old_space_id].set_start_array(heap->old_gen()->start_array());
 600 }
 601 
 602 void
 603 PSParallelCompact::clear_data_covering_space(SpaceId id)
 604 {
 605   // At this point, top is the value before GC, new_top() is the value that will
 606   // be set at the end of GC.  The marking bitmap is cleared to top; nothing
 607   // should be marked above top.  The summary data is cleared to the larger of
 608   // top & new_top.
 609   MutableSpace* const space = _space_info[id].space();
 610   HeapWord* const bot = space->bottom();
 611   HeapWord* const top = space->top();
 612   HeapWord* const max_top = MAX2(top, _space_info[id].new_top());
 613 
 614   _mark_bitmap.clear_range(bot, top);
 615 
 616   const size_t beg_region = _summary_data.addr_to_region_idx(bot);
 617   const size_t end_region =
 618     _summary_data.addr_to_region_idx(_summary_data.region_align_up(max_top));
 619   _summary_data.clear_range(beg_region, end_region);
 620 
 621   // Clear the data used to 'split' regions.
 622   SplitInfo& split_info = _space_info[id].split_info();
 623   if (split_info.is_valid()) {
 624     split_info.clear();
 625   }
 626   DEBUG_ONLY(split_info.verify_clear();)
 627 }
 628 
 629 void PSParallelCompact::pre_compact()
 630 {
 631   // Update the from & to space pointers in space_info, since they are swapped
 632   // at each young gen gc.  Do the update unconditionally (even though a
 633   // promotion failure does not swap spaces) because an unknown number of young
 634   // collections will have swapped the spaces an unknown number of times.
 635   GCTraceTime(Debug, gc, phases) tm("Pre Compact", &_gc_timer);
 636   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 637   _space_info[from_space_id].set_space(heap->young_gen()->from_space());
 638   _space_info[to_space_id].set_space(heap->young_gen()->to_space());
 639 
 640   heap->increment_total_collections(true);
 641 
 642   CodeCache::on_gc_marking_cycle_start();
 643 
 644   heap->print_before_gc();
 645   heap->trace_heap_before_gc(&_gc_tracer);
 646 
 647   // Fill in TLABs
 648   heap->ensure_parsability(true);  // retire TLABs
 649 
 650   if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) {
 651     Universe::verify("Before GC");
 652   }
 653 
 654   DEBUG_ONLY(mark_bitmap()->verify_clear();)
 655   DEBUG_ONLY(summary_data().verify_clear();)
 656 }
 657 
 658 void PSParallelCompact::post_compact()
 659 {
 660   GCTraceTime(Info, gc, phases) tm("Post Compact", &_gc_timer);
 661   ParCompactionManager::remove_all_shadow_regions();
 662 
 663   CodeCache::on_gc_marking_cycle_finish();
 664   CodeCache::arm_all_nmethods();
 665 
 666   // Need to clear claim bits for the next full-gc (marking and adjust-pointers).
 667   ClassLoaderDataGraph::clear_claimed_marks();
 668 
 669   for (unsigned int id = old_space_id; id < last_space_id; ++id) {
 670     // Clear the marking bitmap, summary data and split info.
 671     clear_data_covering_space(SpaceId(id));
 672     {
 673       MutableSpace* space = _space_info[id].space();
 674       HeapWord* top = space->top();
 675       HeapWord* new_top = _space_info[id].new_top();
 676       if (ZapUnusedHeapArea && new_top < top) {
 677         space->mangle_region(MemRegion(new_top, top));
 678       }
 679       // Update top().  Must be done after clearing the bitmap and summary data.
 680       space->set_top(new_top);
 681     }
 682   }
 683 
 684 #ifdef ASSERT
 685   {
 686     mark_bitmap()->verify_clear();
 687     summary_data().verify_clear();
 688   }
 689 #endif
 690 
 691   ParCompactionManager::flush_all_string_dedup_requests();
 692 
 693   MutableSpace* const eden_space = _space_info[eden_space_id].space();
 694   MutableSpace* const from_space = _space_info[from_space_id].space();
 695   MutableSpace* const to_space   = _space_info[to_space_id].space();
 696 
 697   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 698   bool eden_empty = eden_space->is_empty();
 699 
 700   // Update heap occupancy information which is used as input to the soft ref
 701   // clearing policy at the next gc.
 702   Universe::heap()->update_capacity_and_used_at_gc();
 703 
 704   bool young_gen_empty = eden_empty && from_space->is_empty() &&
 705     to_space->is_empty();
 706 
 707   PSCardTable* ct = heap->card_table();
 708   MemRegion old_mr = heap->old_gen()->committed();
 709   if (young_gen_empty) {
 710     ct->clear_MemRegion(old_mr);
 711   } else {
 712     ct->dirty_MemRegion(old_mr);
 713   }
 714 
 715   heap->prune_scavengable_nmethods();
 716 
 717 #if COMPILER2_OR_JVMCI
 718   DerivedPointerTable::update_pointers();
 719 #endif
 720 
 721   // Signal that we have completed a visit to all live objects.
 722   Universe::heap()->record_whole_heap_examined_timestamp();
 723 }
 724 
 725 HeapWord* PSParallelCompact::compute_dense_prefix_for_old_space(MutableSpace* old_space,
 726                                                                 HeapWord* full_region_prefix_end) {
 727   const size_t region_size = ParallelCompactData::RegionSize;
 728   const ParallelCompactData& sd = summary_data();
 729 
 730   // Iteration starts with the region *after* the full-region-prefix-end.
 731   const RegionData* const start_region = sd.addr_to_region_ptr(full_region_prefix_end);
 732   // If final region is not full, iteration stops before that region,
 733   // because fill_dense_prefix_end assumes that prefix_end <= top.
 734   const RegionData* const end_region = sd.addr_to_region_ptr(old_space->top());
 735   assert(start_region <= end_region, "inv");
 736 
 737   size_t max_waste = old_space->capacity_in_words() * (MarkSweepDeadRatio / 100.0);
 738   const RegionData* cur_region = start_region;
 739   for (/* empty */; cur_region < end_region; ++cur_region) {
 740     assert(region_size >= cur_region->data_size(), "inv");
 741     size_t dead_size = region_size - cur_region->data_size();
 742     if (max_waste < dead_size) {
 743       break;
 744     }
 745     max_waste -= dead_size;
 746   }
 747 
 748   HeapWord* const prefix_end = sd.region_to_addr(cur_region);
 749   assert(sd.is_region_aligned(prefix_end), "postcondition");
 750   assert(prefix_end >= full_region_prefix_end, "in-range");
 751   assert(prefix_end <= old_space->top(), "in-range");
 752   return prefix_end;
 753 }
 754 
 755 void PSParallelCompact::fill_dense_prefix_end(SpaceId id) {
 756   // Comparing two sizes to decide if filling is required:
 757   //
 758   // The size of the filler (min-obj-size) is 2 heap words with the default
 759   // MinObjAlignment, since both markword and klass take 1 heap word.
 760   // With +UseCompactObjectHeaders, the minimum filler size is only one word,
 761   // because the Klass* gets encoded in the mark-word.
 762   //
 763   // The size of the gap (if any) right before dense-prefix-end is
 764   // MinObjAlignment.
 765   //
 766   // Need to fill in the gap only if it's smaller than min-obj-size, and the
 767   // filler obj will extend to next region.
 768 
 769   if (MinObjAlignment >= checked_cast<int>(CollectedHeap::min_fill_size())) {
 770     return;
 771   }
 772 
 773   assert(!UseCompactObjectHeaders, "Compact headers can allocate small objects");
 774   assert(CollectedHeap::min_fill_size() == 2, "inv");
 775   HeapWord* const dense_prefix_end = dense_prefix(id);
 776   assert(_summary_data.is_region_aligned(dense_prefix_end), "precondition");
 777   assert(dense_prefix_end <= space(id)->top(), "precondition");
 778   if (dense_prefix_end == space(id)->top()) {
 779     // Must not have single-word gap right before prefix-end/top.
 780     return;
 781   }
 782   RegionData* const region_after_dense_prefix = _summary_data.addr_to_region_ptr(dense_prefix_end);
 783 
 784   if (region_after_dense_prefix->partial_obj_size() != 0 ||
 785       _mark_bitmap.is_marked(dense_prefix_end)) {
 786     // The region after the dense prefix starts with live bytes.
 787     return;
 788   }
 789 
 790   HeapWord* block_start = start_array(id)->block_start_reaching_into_card(dense_prefix_end);
 791   if (block_start == dense_prefix_end - 1) {
 792     assert(!_mark_bitmap.is_marked(block_start), "inv");
 793     // There is exactly one heap word gap right before the dense prefix end, so we need a filler object.
 794     // The filler object will extend into region_after_dense_prefix.
 795     const size_t obj_len = 2; // min-fill-size
 796     HeapWord* const obj_beg = dense_prefix_end - 1;
 797     CollectedHeap::fill_with_object(obj_beg, obj_len);
 798     _mark_bitmap.mark_obj(obj_beg);
 799     _summary_data.addr_to_region_ptr(obj_beg)->add_live_obj(1);
 800     region_after_dense_prefix->set_partial_obj_size(1);
 801     region_after_dense_prefix->set_partial_obj_addr(obj_beg);
 802     assert(start_array(id) != nullptr, "sanity");
 803     start_array(id)->update_for_block(obj_beg, obj_beg + obj_len);
 804   }
 805 }
 806 
 807 bool PSParallelCompact::check_maximum_compaction(bool should_do_max_compaction,
 808                                                  size_t total_live_words,
 809                                                  MutableSpace* const old_space,
 810                                                  HeapWord* full_region_prefix_end) {
 811 
 812   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 813 
 814   // Check System.GC
 815   bool is_max_on_system_gc = UseMaximumCompactionOnSystemGC
 816                           && GCCause::is_user_requested_gc(heap->gc_cause());
 817 
 818   // Check if all live objs are too much for old-gen.
 819   const bool is_old_gen_too_full = (total_live_words >= old_space->capacity_in_words());
 820 
 821   // If all regions in old-gen are full
 822   const bool is_region_full =
 823     full_region_prefix_end >= _summary_data.region_align_down(old_space->top());
 824 
 825   return should_do_max_compaction
 826       || is_max_on_system_gc
 827       || is_old_gen_too_full
 828       || is_region_full;
 829 }
 830 
 831 void PSParallelCompact::summary_phase(bool should_do_max_compaction)
 832 {
 833   GCTraceTime(Info, gc, phases) tm("Summary Phase", &_gc_timer);
 834 
 835   MutableSpace* const old_space = _space_info[old_space_id].space();
 836   {
 837     size_t total_live_words = 0;
 838     HeapWord* full_region_prefix_end = nullptr;
 839     {
 840       // old-gen
 841       size_t live_words = _summary_data.live_words_in_space(old_space,
 842                                                             &full_region_prefix_end);
 843       total_live_words += live_words;
 844     }
 845     // young-gen
 846     for (uint i = eden_space_id; i < last_space_id; ++i) {
 847       const MutableSpace* space = _space_info[i].space();
 848       size_t live_words = _summary_data.live_words_in_space(space);
 849       total_live_words += live_words;
 850       _space_info[i].set_new_top(space->bottom() + live_words);
 851       _space_info[i].set_dense_prefix(space->bottom());
 852     }
 853 
 854     should_do_max_compaction = check_maximum_compaction(should_do_max_compaction,
 855                                                         total_live_words,
 856                                                         old_space,
 857                                                         full_region_prefix_end);
 858     {
 859       GCTraceTime(Info, gc, phases) tm("Summary Phase: expand", &_gc_timer);
 860       // Try to expand old-gen in order to fit all live objs and waste.
 861       size_t target_capacity_bytes = total_live_words * HeapWordSize
 862                                    + old_space->capacity_in_bytes() * (MarkSweepDeadRatio / 100);
 863       ParallelScavengeHeap::heap()->old_gen()->try_expand_till_size(target_capacity_bytes);
 864     }
 865 
 866     HeapWord* dense_prefix_end = should_do_max_compaction
 867                                  ? full_region_prefix_end
 868                                  : compute_dense_prefix_for_old_space(old_space,
 869                                                                       full_region_prefix_end);
 870     SpaceId id = old_space_id;
 871     _space_info[id].set_dense_prefix(dense_prefix_end);
 872 
 873     if (dense_prefix_end != old_space->bottom()) {
 874       fill_dense_prefix_end(id);
 875     }
 876 
 877     // Compacting objs in [dense_prefix_end, old_space->top())
 878     _summary_data.summarize(_space_info[id].split_info(),
 879                             dense_prefix_end, old_space->top(), nullptr,
 880                             dense_prefix_end, old_space->end(),
 881                             _space_info[id].new_top_addr());
 882   }
 883 
 884   // Summarize the remaining spaces in the young gen.  The initial target space
 885   // is the old gen.  If a space does not fit entirely into the target, then the
 886   // remainder is compacted into the space itself and that space becomes the new
 887   // target.
 888   SpaceId dst_space_id = old_space_id;
 889   HeapWord* dst_space_end = old_space->end();
 890   HeapWord** new_top_addr = _space_info[dst_space_id].new_top_addr();
 891   for (unsigned int id = eden_space_id; id < last_space_id; ++id) {
 892     const MutableSpace* space = _space_info[id].space();
 893     const size_t live = pointer_delta(_space_info[id].new_top(),
 894                                       space->bottom());
 895     const size_t available = pointer_delta(dst_space_end, *new_top_addr);
 896 
 897     if (live > 0 && live <= available) {
 898       // All the live data will fit.
 899       bool done = _summary_data.summarize(_space_info[id].split_info(),
 900                                           space->bottom(), space->top(),
 901                                           nullptr,
 902                                           *new_top_addr, dst_space_end,
 903                                           new_top_addr);
 904       assert(done, "space must fit into old gen");
 905 
 906       // Reset the new_top value for the space.
 907       _space_info[id].set_new_top(space->bottom());
 908     } else if (live > 0) {
 909       // Attempt to fit part of the source space into the target space.
 910       HeapWord* next_src_addr = nullptr;
 911       bool done = _summary_data.summarize(_space_info[id].split_info(),
 912                                           space->bottom(), space->top(),
 913                                           &next_src_addr,
 914                                           *new_top_addr, dst_space_end,
 915                                           new_top_addr);
 916       assert(!done, "space should not fit into old gen");
 917       assert(next_src_addr != nullptr, "sanity");
 918 
 919       // The source space becomes the new target, so the remainder is compacted
 920       // within the space itself.
 921       dst_space_id = SpaceId(id);
 922       dst_space_end = space->end();
 923       new_top_addr = _space_info[id].new_top_addr();
 924       done = _summary_data.summarize(_space_info[id].split_info(),
 925                                      next_src_addr, space->top(),
 926                                      nullptr,
 927                                      space->bottom(), dst_space_end,
 928                                      new_top_addr);
 929       assert(done, "space must fit when compacted into itself");
 930       assert(*new_top_addr <= space->top(), "usage should not grow");
 931     }
 932   }
 933 }
 934 
 935 bool PSParallelCompact::invoke(bool clear_all_soft_refs, bool should_do_max_compaction) {
 936   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
 937   assert(Thread::current() == (Thread*)VMThread::vm_thread(),
 938          "should be in vm thread");
 939   assert(ref_processor() != nullptr, "Sanity");
 940 
 941   SvcGCMarker sgcm(SvcGCMarker::FULL);
 942   IsSTWGCActiveMark mark;
 943 
 944   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 945 
 946   GCIdMark gc_id_mark;
 947   _gc_timer.register_gc_start();
 948   _gc_tracer.report_gc_start(heap->gc_cause(), _gc_timer.gc_start());
 949 
 950   GCCause::Cause gc_cause = heap->gc_cause();
 951   PSOldGen* old_gen = heap->old_gen();
 952   PSAdaptiveSizePolicy* size_policy = heap->size_policy();
 953 
 954   // Make sure data structures are sane, make the heap parsable, and do other
 955   // miscellaneous bookkeeping.
 956   pre_compact();
 957 
 958   const PreGenGCValues pre_gc_values = heap->get_pre_gc_values();
 959 
 960   {
 961     const uint active_workers =
 962       WorkerPolicy::calc_active_workers(ParallelScavengeHeap::heap()->workers().max_workers(),
 963                                         ParallelScavengeHeap::heap()->workers().active_workers(),
 964                                         Threads::number_of_non_daemon_threads());
 965     ParallelScavengeHeap::heap()->workers().set_active_workers(active_workers);
 966 
 967     GCTraceCPUTime tcpu(&_gc_tracer);
 968     GCTraceTime(Info, gc) tm("Pause Full", nullptr, gc_cause, true);
 969 
 970     heap->pre_full_gc_dump(&_gc_timer);
 971 
 972     TraceCollectorStats tcs(counters());
 973     TraceMemoryManagerStats tms(heap->old_gc_manager(), gc_cause, "end of major GC");
 974 
 975     if (log_is_enabled(Debug, gc, heap, exit)) {
 976       accumulated_time()->start();
 977     }
 978 
 979     // Let the size policy know we're starting
 980     size_policy->major_collection_begin();
 981 
 982 #if COMPILER2_OR_JVMCI
 983     DerivedPointerTable::clear();
 984 #endif
 985 
 986     ref_processor()->start_discovery(clear_all_soft_refs);
 987 
 988     marking_phase(&_gc_tracer);
 989 
 990     summary_phase(should_do_max_compaction);
 991 
 992 #if COMPILER2_OR_JVMCI
 993     assert(DerivedPointerTable::is_active(), "Sanity");
 994     DerivedPointerTable::set_active(false);
 995 #endif
 996 
 997     forward_to_new_addr();
 998 
 999     adjust_pointers();
1000 
1001     compact();
1002 
1003     ParCompactionManager::_preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers());
1004 
1005     ParCompactionManager::verify_all_region_stack_empty();
1006 
1007     // Reset the mark bitmap, summary data, and do other bookkeeping.  Must be
1008     // done before resizing.
1009     post_compact();
1010 
1011     size_policy->major_collection_end();
1012 
1013     size_policy->sample_old_gen_used_bytes(MAX2(pre_gc_values.old_gen_used(), old_gen->used_in_bytes()));
1014 
1015     if (UseAdaptiveSizePolicy) {
1016       heap->resize_after_full_gc();
1017     }
1018 
1019     heap->resize_all_tlabs();
1020 
1021     // Resize the metaspace capacity after a collection
1022     MetaspaceGC::compute_new_size();
1023 
1024     if (log_is_enabled(Debug, gc, heap, exit)) {
1025       accumulated_time()->stop();
1026     }
1027 
1028     heap->print_heap_change(pre_gc_values);
1029 
1030     // Track memory usage and detect low memory
1031     MemoryService::track_memory_usage();
1032     heap->update_counters();
1033 
1034     heap->post_full_gc_dump(&_gc_timer);
1035 
1036     size_policy->record_gc_pause_end_instant();
1037   }
1038 
1039   heap->gc_epilogue(true);
1040 
1041   if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) {
1042     Universe::verify("After GC");
1043   }
1044 
1045   heap->print_after_gc();
1046   heap->trace_heap_after_gc(&_gc_tracer);
1047 
1048   _gc_timer.register_gc_end();
1049 
1050   _gc_tracer.report_dense_prefix(dense_prefix(old_space_id));
1051   _gc_tracer.report_gc_end(_gc_timer.gc_end(), _gc_timer.time_partitions());
1052 
1053   return true;
1054 }
1055 
1056 class PCAddThreadRootsMarkingTaskClosure : public ThreadClosure {
1057   ParCompactionManager* _cm;
1058 
1059 public:
1060   PCAddThreadRootsMarkingTaskClosure(ParCompactionManager* cm) : _cm(cm) { }
1061   void do_thread(Thread* thread) {
1062     ResourceMark rm;
1063 
1064     MarkingNMethodClosure mark_and_push_in_blobs(&_cm->_mark_and_push_closure);
1065 
1066     thread->oops_do(&_cm->_mark_and_push_closure, &mark_and_push_in_blobs);
1067 
1068     // Do the real work
1069     _cm->follow_marking_stacks();
1070   }
1071 };
1072 
1073 void steal_marking_work(TaskTerminator& terminator, uint worker_id) {
1074   assert(ParallelScavengeHeap::heap()->is_stw_gc_active(), "called outside gc");
1075 
1076   ParCompactionManager* cm =
1077     ParCompactionManager::gc_thread_compaction_manager(worker_id);
1078 
1079   do {
1080     ScannerTask task;
1081     if (ParCompactionManager::steal(worker_id, task)) {
1082       cm->follow_contents(task, true);
1083     }
1084     cm->follow_marking_stacks();
1085   } while (!terminator.offer_termination());
1086 }
1087 
1088 class MarkFromRootsTask : public WorkerTask {
1089   NMethodMarkingScope _nmethod_marking_scope;
1090   ThreadsClaimTokenScope _threads_claim_token_scope;
1091   OopStorageSetStrongParState<false /* concurrent */, false /* is_const */> _oop_storage_set_par_state;
1092   TaskTerminator _terminator;
1093   uint _active_workers;
1094 
1095 public:
1096   MarkFromRootsTask(uint active_workers) :
1097       WorkerTask("MarkFromRootsTask"),
1098       _nmethod_marking_scope(),
1099       _threads_claim_token_scope(),
1100       _terminator(active_workers, ParCompactionManager::marking_stacks()),
1101       _active_workers(active_workers) {}
1102 
1103   virtual void work(uint worker_id) {
1104     ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id);
1105     cm->create_marking_stats_cache();
1106     {
1107       CLDToOopClosure cld_closure(&cm->_mark_and_push_closure, ClassLoaderData::_claim_stw_fullgc_mark);
1108       ClassLoaderDataGraph::always_strong_cld_do(&cld_closure);
1109 
1110       // Do the real work
1111       cm->follow_marking_stacks();
1112     }
1113 
1114     {
1115       PCAddThreadRootsMarkingTaskClosure closure(cm);
1116       Threads::possibly_parallel_threads_do(_active_workers > 1 /* is_par */, &closure);
1117     }
1118 
1119     // Mark from OopStorages
1120     {
1121       _oop_storage_set_par_state.oops_do(&cm->_mark_and_push_closure);
1122       // Do the real work
1123       cm->follow_marking_stacks();
1124     }
1125 
1126     if (_active_workers > 1) {
1127       steal_marking_work(_terminator, worker_id);
1128     }
1129   }
1130 };
1131 
1132 class ParallelCompactRefProcProxyTask : public RefProcProxyTask {
1133   TaskTerminator _terminator;
1134 
1135 public:
1136   ParallelCompactRefProcProxyTask(uint max_workers)
1137     : RefProcProxyTask("ParallelCompactRefProcProxyTask", max_workers),
1138       _terminator(_max_workers, ParCompactionManager::marking_stacks()) {}
1139 
1140   void work(uint worker_id) override {
1141     assert(worker_id < _max_workers, "sanity");
1142     ParCompactionManager* cm = (_tm == RefProcThreadModel::Single) ? ParCompactionManager::get_vmthread_cm() : ParCompactionManager::gc_thread_compaction_manager(worker_id);
1143     BarrierEnqueueDiscoveredFieldClosure enqueue;
1144     ParCompactionManager::FollowStackClosure complete_gc(cm, (_tm == RefProcThreadModel::Single) ? nullptr : &_terminator, worker_id);
1145     _rp_task->rp_work(worker_id, PSParallelCompact::is_alive_closure(), &cm->_mark_and_push_closure, &enqueue, &complete_gc);
1146   }
1147 
1148   void prepare_run_task_hook() override {
1149     _terminator.reset_for_reuse(_queue_count);
1150   }
1151 };
1152 
1153 static void flush_marking_stats_cache(const uint num_workers) {
1154   for (uint i = 0; i < num_workers; ++i) {
1155     ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(i);
1156     cm->flush_and_destroy_marking_stats_cache();
1157   }
1158 }
1159 
1160 void PSParallelCompact::marking_phase(ParallelOldTracer *gc_tracer) {
1161   // Recursively traverse all live objects and mark them
1162   GCTraceTime(Info, gc, phases) tm("Marking Phase", &_gc_timer);
1163 
1164   uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers();
1165 
1166   ClassLoaderDataGraph::verify_claimed_marks_cleared(ClassLoaderData::_claim_stw_fullgc_mark);
1167   {
1168     GCTraceTime(Debug, gc, phases) tm("Par Mark", &_gc_timer);
1169 
1170     MarkFromRootsTask task(active_gc_threads);
1171     ParallelScavengeHeap::heap()->workers().run_task(&task);
1172   }
1173 
1174   // Process reference objects found during marking
1175   {
1176     GCTraceTime(Debug, gc, phases) tm("Reference Processing", &_gc_timer);
1177 
1178     ReferenceProcessorStats stats;
1179     ReferenceProcessorPhaseTimes pt(&_gc_timer, ref_processor()->max_num_queues());
1180 
1181     ParallelCompactRefProcProxyTask task(ref_processor()->max_num_queues());
1182     stats = ref_processor()->process_discovered_references(task, &ParallelScavengeHeap::heap()->workers(), pt);
1183 
1184     gc_tracer->report_gc_reference_stats(stats);
1185     pt.print_all_references();
1186   }
1187 
1188   {
1189     GCTraceTime(Debug, gc, phases) tm("Flush Marking Stats", &_gc_timer);
1190 
1191     flush_marking_stats_cache(active_gc_threads);
1192   }
1193 
1194   // This is the point where the entire marking should have completed.
1195   ParCompactionManager::verify_all_marking_stack_empty();
1196 
1197   {
1198     GCTraceTime(Debug, gc, phases) tm("Weak Processing", &_gc_timer);
1199     WeakProcessor::weak_oops_do(&ParallelScavengeHeap::heap()->workers(),
1200                                 is_alive_closure(),
1201                                 &do_nothing_cl,
1202                                 1);
1203   }
1204 
1205   {
1206     GCTraceTime(Debug, gc, phases) tm_m("Class Unloading", &_gc_timer);
1207 
1208     ClassUnloadingContext ctx(1 /* num_nmethod_unlink_workers */,
1209                               false /* unregister_nmethods_during_purge */,
1210                               false /* lock_nmethod_free_separately */);
1211 
1212     bool unloading_occurred;
1213     {
1214       CodeCache::UnlinkingScope scope(is_alive_closure());
1215 
1216       // Follow system dictionary roots and unload classes.
1217       unloading_occurred = SystemDictionary::do_unloading(&_gc_timer);
1218 
1219       // Unload nmethods.
1220       CodeCache::do_unloading(unloading_occurred);
1221     }
1222 
1223     {
1224       GCTraceTime(Debug, gc, phases) t("Purge Unlinked NMethods", gc_timer());
1225       // Release unloaded nmethod's memory.
1226       ctx.purge_nmethods();
1227     }
1228     {
1229       GCTraceTime(Debug, gc, phases) ur("Unregister NMethods", &_gc_timer);
1230       ParallelScavengeHeap::heap()->prune_unlinked_nmethods();
1231     }
1232     {
1233       GCTraceTime(Debug, gc, phases) t("Free Code Blobs", gc_timer());
1234       ctx.free_nmethods();
1235     }
1236 
1237     // Prune dead klasses from subklass/sibling/implementor lists.
1238     Klass::clean_weak_klass_links(unloading_occurred);
1239 
1240     // Clean JVMCI metadata handles.
1241     JVMCI_ONLY(JVMCI::do_unloading(unloading_occurred));
1242     {
1243       // Delete metaspaces for unloaded class loaders and clean up loader_data graph
1244       GCTraceTime(Debug, gc, phases) t("Purge Class Loader Data", gc_timer());
1245       ClassLoaderDataGraph::purge(true /* at_safepoint */);
1246       DEBUG_ONLY(MetaspaceUtils::verify();)
1247     }
1248   }
1249 
1250   {
1251     GCTraceTime(Debug, gc, phases) tm("Report Object Count", &_gc_timer);
1252     _gc_tracer.report_object_count_after_gc(is_alive_closure(), &ParallelScavengeHeap::heap()->workers());
1253   }
1254 #if TASKQUEUE_STATS
1255   ParCompactionManager::print_and_reset_taskqueue_stats();
1256 #endif
1257 }
1258 
1259 template<typename Func>
1260 void PSParallelCompact::adjust_in_space_helper(SpaceId id, volatile uint* claim_counter, Func&& on_stripe) {
1261   MutableSpace* sp = PSParallelCompact::space(id);
1262   HeapWord* const bottom = sp->bottom();
1263   HeapWord* const top = sp->top();
1264   if (bottom == top) {
1265     return;
1266   }
1267 
1268   const uint num_regions_per_stripe = 2;
1269   const size_t region_size = ParallelCompactData::RegionSize;
1270   const size_t stripe_size = num_regions_per_stripe * region_size;
1271 
1272   while (true) {
1273     uint counter = AtomicAccess::fetch_then_add(claim_counter, num_regions_per_stripe);
1274     HeapWord* cur_stripe = bottom + counter * region_size;
1275     if (cur_stripe >= top) {
1276       break;
1277     }
1278     HeapWord* stripe_end = MIN2(cur_stripe + stripe_size, top);
1279     on_stripe(cur_stripe, stripe_end);
1280   }
1281 }
1282 
1283 void PSParallelCompact::adjust_in_old_space(volatile uint* claim_counter) {
1284   // Regions in old-space shouldn't be split.
1285   assert(!_space_info[old_space_id].split_info().is_valid(), "inv");
1286 
1287   auto scan_obj_with_limit = [&] (HeapWord* obj_start, HeapWord* left, HeapWord* right) {
1288     assert(mark_bitmap()->is_marked(obj_start), "inv");
1289     oop obj = cast_to_oop(obj_start);
1290     return obj->oop_iterate_size(&pc_adjust_pointer_closure, MemRegion(left, right));
1291   };
1292 
1293   adjust_in_space_helper(old_space_id, claim_counter, [&] (HeapWord* stripe_start, HeapWord* stripe_end) {
1294     assert(_summary_data.is_region_aligned(stripe_start), "inv");
1295     RegionData* cur_region = _summary_data.addr_to_region_ptr(stripe_start);
1296     HeapWord* obj_start;
1297     if (cur_region->partial_obj_size() != 0) {
1298       obj_start = cur_region->partial_obj_addr();
1299       obj_start += scan_obj_with_limit(obj_start, stripe_start, stripe_end);
1300     } else {
1301       obj_start = stripe_start;
1302     }
1303 
1304     while (obj_start < stripe_end) {
1305       obj_start = mark_bitmap()->find_obj_beg(obj_start, stripe_end);
1306       if (obj_start >= stripe_end) {
1307         break;
1308       }
1309       obj_start += scan_obj_with_limit(obj_start, stripe_start, stripe_end);
1310     }
1311   });
1312 }
1313 
1314 void PSParallelCompact::adjust_in_young_space(SpaceId id, volatile uint* claim_counter) {
1315   adjust_in_space_helper(id, claim_counter, [](HeapWord* stripe_start, HeapWord* stripe_end) {
1316     HeapWord* obj_start = stripe_start;
1317     while (obj_start < stripe_end) {
1318       obj_start = mark_bitmap()->find_obj_beg(obj_start, stripe_end);
1319       if (obj_start >= stripe_end) {
1320         break;
1321       }
1322       oop obj = cast_to_oop(obj_start);
1323       obj_start += obj->oop_iterate_size(&pc_adjust_pointer_closure);
1324     }
1325   });
1326 }
1327 
1328 void PSParallelCompact::adjust_pointers_in_spaces(uint worker_id, volatile uint* claim_counters) {
1329   auto start_time = Ticks::now();
1330   adjust_in_old_space(&claim_counters[0]);
1331   for (uint id = eden_space_id; id < last_space_id; ++id) {
1332     adjust_in_young_space(SpaceId(id), &claim_counters[id]);
1333   }
1334   log_trace(gc, phases)("adjust_pointers_in_spaces worker %u: %.3f ms", worker_id, (Ticks::now() - start_time).seconds() * 1000);
1335 }
1336 
1337 class PSAdjustTask final : public WorkerTask {
1338   SubTasksDone                               _sub_tasks;
1339   WeakProcessor::Task                        _weak_proc_task;
1340   OopStorageSetStrongParState<false, false>  _oop_storage_iter;
1341   uint                                       _nworkers;
1342   volatile uint _claim_counters[PSParallelCompact::last_space_id] = {};
1343 
1344   enum PSAdjustSubTask {
1345     PSAdjustSubTask_code_cache,
1346 
1347     PSAdjustSubTask_num_elements
1348   };
1349 
1350 public:
1351   PSAdjustTask(uint nworkers) :
1352     WorkerTask("PSAdjust task"),
1353     _sub_tasks(PSAdjustSubTask_num_elements),
1354     _weak_proc_task(nworkers),
1355     _nworkers(nworkers) {
1356 
1357     ClassLoaderDataGraph::verify_claimed_marks_cleared(ClassLoaderData::_claim_stw_fullgc_adjust);
1358     Threads::change_thread_claim_token();
1359   }
1360 
1361   ~PSAdjustTask() {
1362     Threads::assert_all_threads_claimed();
1363   }
1364 
1365   void work(uint worker_id) {
1366     ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id);
1367     cm->preserved_marks()->adjust_during_full_gc();
1368     {
1369       // adjust pointers in all spaces
1370       PSParallelCompact::adjust_pointers_in_spaces(worker_id, _claim_counters);
1371     }
1372     {
1373       ResourceMark rm;
1374       Threads::possibly_parallel_oops_do(_nworkers > 1, &pc_adjust_pointer_closure, nullptr);
1375     }
1376     _oop_storage_iter.oops_do(&pc_adjust_pointer_closure);
1377     {
1378       CLDToOopClosure cld_closure(&pc_adjust_pointer_closure, ClassLoaderData::_claim_stw_fullgc_adjust);
1379       ClassLoaderDataGraph::cld_do(&cld_closure);
1380     }
1381     {
1382       AlwaysTrueClosure always_alive;
1383       _weak_proc_task.work(worker_id, &always_alive, &pc_adjust_pointer_closure);
1384     }
1385     if (_sub_tasks.try_claim_task(PSAdjustSubTask_code_cache)) {
1386       NMethodToOopClosure adjust_code(&pc_adjust_pointer_closure, NMethodToOopClosure::FixRelocations);
1387       CodeCache::nmethods_do(&adjust_code);
1388     }
1389     _sub_tasks.all_tasks_claimed();
1390   }
1391 };
1392 
1393 void PSParallelCompact::adjust_pointers() {
1394   // Adjust the pointers to reflect the new locations
1395   GCTraceTime(Info, gc, phases) tm("Adjust Pointers", &_gc_timer);
1396   uint nworkers = ParallelScavengeHeap::heap()->workers().active_workers();
1397   PSAdjustTask task(nworkers);
1398   ParallelScavengeHeap::heap()->workers().run_task(&task);
1399 }
1400 
1401 // Split [start, end) evenly for a number of workers and return the
1402 // range for worker_id.
1403 static void split_regions_for_worker(size_t start, size_t end,
1404                                      uint worker_id, uint num_workers,
1405                                      size_t* worker_start, size_t* worker_end) {
1406   assert(start < end, "precondition");
1407   assert(num_workers > 0, "precondition");
1408   assert(worker_id < num_workers, "precondition");
1409 
1410   size_t num_regions = end - start;
1411   size_t num_regions_per_worker = num_regions / num_workers;
1412   size_t remainder = num_regions % num_workers;
1413   // The first few workers will get one extra.
1414   *worker_start = start + worker_id * num_regions_per_worker
1415                   + MIN2(checked_cast<size_t>(worker_id), remainder);
1416   *worker_end = *worker_start + num_regions_per_worker
1417                 + (worker_id < remainder ? 1 : 0);
1418 }
1419 
1420 void PSParallelCompact::forward_to_new_addr() {
1421   GCTraceTime(Info, gc, phases) tm("Forward", &_gc_timer);
1422   uint nworkers = ParallelScavengeHeap::heap()->workers().active_workers();
1423 
1424   struct ForwardTask final : public WorkerTask {
1425     uint _num_workers;
1426 
1427     explicit ForwardTask(uint num_workers) :
1428       WorkerTask("PSForward task"),
1429       _num_workers(num_workers) {}
1430 
1431     static void forward_objs_in_range(ParCompactionManager* cm,
1432                                       HeapWord* start,
1433                                       HeapWord* end,
1434                                       HeapWord* destination) {
1435       HeapWord* cur_addr = start;
1436       HeapWord* new_addr = destination;
1437 
1438       while (cur_addr < end) {
1439         cur_addr = mark_bitmap()->find_obj_beg(cur_addr, end);
1440         if (cur_addr >= end) {
1441           return;
1442         }
1443         assert(mark_bitmap()->is_marked(cur_addr), "inv");
1444         oop obj = cast_to_oop(cur_addr);
1445         if (new_addr != cur_addr) {
1446           cm->preserved_marks()->push_if_necessary(obj, obj->mark());
1447           FullGCForwarding::forward_to(obj, cast_to_oop(new_addr));
1448         }
1449         size_t obj_size = obj->size();
1450         new_addr += obj_size;
1451         cur_addr += obj_size;
1452       }
1453     }
1454 
1455     void work(uint worker_id) override {
1456       ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id);
1457       for (uint id = old_space_id; id < last_space_id; ++id) {
1458         MutableSpace* sp = PSParallelCompact::space(SpaceId(id));
1459         HeapWord* dense_prefix_addr = dense_prefix(SpaceId(id));
1460         HeapWord* top = sp->top();
1461 
1462         if (dense_prefix_addr == top) {
1463           // Empty space
1464           continue;
1465         }
1466 
1467         const SplitInfo& split_info = _space_info[SpaceId(id)].split_info();
1468         size_t dense_prefix_region = _summary_data.addr_to_region_idx(dense_prefix_addr);
1469         size_t top_region = _summary_data.addr_to_region_idx(_summary_data.region_align_up(top));
1470         size_t start_region;
1471         size_t end_region;
1472         split_regions_for_worker(dense_prefix_region, top_region,
1473                                  worker_id, _num_workers,
1474                                  &start_region, &end_region);
1475         for (size_t cur_region = start_region; cur_region < end_region; ++cur_region) {
1476           RegionData* region_ptr = _summary_data.region(cur_region);
1477           size_t partial_obj_size = region_ptr->partial_obj_size();
1478 
1479           if (partial_obj_size == ParallelCompactData::RegionSize) {
1480             // No obj-start
1481             continue;
1482           }
1483 
1484           HeapWord* region_start = _summary_data.region_to_addr(cur_region);
1485           HeapWord* region_end = region_start + ParallelCompactData::RegionSize;
1486 
1487           if (split_info.is_split(cur_region)) {
1488             // Part 1: will be relocated to space-1
1489             HeapWord* preceding_destination = split_info.preceding_destination();
1490             HeapWord* split_point = split_info.split_point();
1491             forward_objs_in_range(cm, region_start + partial_obj_size, split_point, preceding_destination + partial_obj_size);
1492 
1493             // Part 2: will be relocated to space-2
1494             HeapWord* destination = region_ptr->destination();
1495             forward_objs_in_range(cm, split_point, region_end, destination);
1496           } else {
1497             HeapWord* destination = region_ptr->destination();
1498             forward_objs_in_range(cm, region_start + partial_obj_size, region_end, destination + partial_obj_size);
1499           }
1500         }
1501       }
1502     }
1503   } task(nworkers);
1504 
1505   ParallelScavengeHeap::heap()->workers().run_task(&task);
1506   DEBUG_ONLY(verify_forward();)
1507 }
1508 
1509 #ifdef ASSERT
1510 void PSParallelCompact::verify_forward() {
1511   HeapWord* const old_dense_prefix_addr = dense_prefix(SpaceId(old_space_id));
1512   // The destination addr for the first live obj after dense-prefix.
1513   HeapWord* bump_ptr = old_dense_prefix_addr
1514                      + _summary_data.addr_to_region_ptr(old_dense_prefix_addr)->partial_obj_size();
1515   SpaceId bump_ptr_space = old_space_id;
1516 
1517   for (uint id = old_space_id; id < last_space_id; ++id) {
1518     MutableSpace* sp = PSParallelCompact::space(SpaceId(id));
1519     // Only verify objs after dense-prefix, because those before dense-prefix are not moved (forwarded).
1520     HeapWord* cur_addr = dense_prefix(SpaceId(id));
1521     HeapWord* top = sp->top();
1522 
1523     while (cur_addr < top) {
1524       cur_addr = mark_bitmap()->find_obj_beg(cur_addr, top);
1525       if (cur_addr >= top) {
1526         break;
1527       }
1528       assert(mark_bitmap()->is_marked(cur_addr), "inv");
1529       assert(bump_ptr <= _space_info[bump_ptr_space].new_top(), "inv");
1530       // Move to the space containing cur_addr
1531       if (bump_ptr == _space_info[bump_ptr_space].new_top()) {
1532         bump_ptr = space(space_id(cur_addr))->bottom();
1533         bump_ptr_space = space_id(bump_ptr);
1534       }
1535       oop obj = cast_to_oop(cur_addr);
1536       if (cur_addr == bump_ptr) {
1537         assert(!FullGCForwarding::is_forwarded(obj), "inv");
1538       } else {
1539         assert(FullGCForwarding::forwardee(obj) == cast_to_oop(bump_ptr), "inv");
1540       }
1541       bump_ptr += obj->size();
1542       cur_addr += obj->size();
1543     }
1544   }
1545 }
1546 #endif
1547 
1548 // Helper class to print 8 region numbers per line and then print the total at the end.
1549 class FillableRegionLogger : public StackObj {
1550 private:
1551   Log(gc, compaction) log;
1552   static const int LineLength = 8;
1553   size_t _regions[LineLength];
1554   int _next_index;
1555   bool _enabled;
1556   size_t _total_regions;
1557 public:
1558   FillableRegionLogger() : _next_index(0), _enabled(log_develop_is_enabled(Trace, gc, compaction)), _total_regions(0) { }
1559   ~FillableRegionLogger() {
1560     log.trace("%zu initially fillable regions", _total_regions);
1561   }
1562 
1563   void print_line() {
1564     if (!_enabled || _next_index == 0) {
1565       return;
1566     }
1567     FormatBuffer<> line("Fillable: ");
1568     for (int i = 0; i < _next_index; i++) {
1569       line.append(" %7zu", _regions[i]);
1570     }
1571     log.trace("%s", line.buffer());
1572     _next_index = 0;
1573   }
1574 
1575   void handle(size_t region) {
1576     if (!_enabled) {
1577       return;
1578     }
1579     _regions[_next_index++] = region;
1580     if (_next_index == LineLength) {
1581       print_line();
1582     }
1583     _total_regions++;
1584   }
1585 };
1586 
1587 void PSParallelCompact::prepare_region_draining_tasks(uint parallel_gc_threads)
1588 {
1589   GCTraceTime(Trace, gc, phases) tm("Drain Task Setup", &_gc_timer);
1590 
1591   // Find the threads that are active
1592   uint worker_id = 0;
1593 
1594   // Find all regions that are available (can be filled immediately) and
1595   // distribute them to the thread stacks.  The iteration is done in reverse
1596   // order (high to low) so the regions will be removed in ascending order.
1597 
1598   const ParallelCompactData& sd = PSParallelCompact::summary_data();
1599 
1600   // id + 1 is used to test termination so unsigned  can
1601   // be used with an old_space_id == 0.
1602   FillableRegionLogger region_logger;
1603   for (unsigned int id = last_space_id - 1; id + 1 > old_space_id; --id) {
1604     SpaceInfo* const space_info = _space_info + id;
1605     HeapWord* const new_top = space_info->new_top();
1606 
1607     const size_t beg_region = sd.addr_to_region_idx(space_info->dense_prefix());
1608     const size_t end_region =
1609       sd.addr_to_region_idx(sd.region_align_up(new_top));
1610 
1611     for (size_t cur = end_region - 1; cur + 1 > beg_region; --cur) {
1612       if (sd.region(cur)->claim_unsafe()) {
1613         ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id);
1614         bool result = sd.region(cur)->mark_normal();
1615         assert(result, "Must succeed at this point.");
1616         cm->region_stack()->push(cur);
1617         region_logger.handle(cur);
1618         // Assign regions to tasks in round-robin fashion.
1619         if (++worker_id == parallel_gc_threads) {
1620           worker_id = 0;
1621         }
1622       }
1623     }
1624     region_logger.print_line();
1625   }
1626 }
1627 
1628 static void compaction_with_stealing_work(TaskTerminator* terminator, uint worker_id) {
1629   assert(ParallelScavengeHeap::heap()->is_stw_gc_active(), "called outside gc");
1630 
1631   ParCompactionManager* cm =
1632     ParCompactionManager::gc_thread_compaction_manager(worker_id);
1633 
1634   // Drain the stacks that have been preloaded with regions
1635   // that are ready to fill.
1636 
1637   cm->drain_region_stacks();
1638 
1639   guarantee(cm->region_stack()->is_empty(), "Not empty");
1640 
1641   size_t region_index = 0;
1642 
1643   while (true) {
1644     if (ParCompactionManager::steal(worker_id, region_index)) {
1645       PSParallelCompact::fill_and_update_region(cm, region_index);
1646       cm->drain_region_stacks();
1647     } else if (PSParallelCompact::steal_unavailable_region(cm, region_index)) {
1648       // Fill and update an unavailable region with the help of a shadow region
1649       PSParallelCompact::fill_and_update_shadow_region(cm, region_index);
1650       cm->drain_region_stacks();
1651     } else {
1652       if (terminator->offer_termination()) {
1653         break;
1654       }
1655       // Go around again.
1656     }
1657   }
1658 }
1659 
1660 class FillDensePrefixAndCompactionTask: public WorkerTask {
1661   TaskTerminator _terminator;
1662 
1663 public:
1664   FillDensePrefixAndCompactionTask(uint active_workers) :
1665       WorkerTask("FillDensePrefixAndCompactionTask"),
1666       _terminator(active_workers, ParCompactionManager::region_task_queues()) {
1667   }
1668 
1669   virtual void work(uint worker_id) {
1670     if (worker_id == 0) {
1671       auto start = Ticks::now();
1672       PSParallelCompact::fill_dead_objs_in_dense_prefix();
1673       log_trace(gc, phases)("Fill dense prefix by worker 0: %.3f ms", (Ticks::now() - start).seconds() * 1000);
1674     }
1675     compaction_with_stealing_work(&_terminator, worker_id);
1676   }
1677 };
1678 
1679 void PSParallelCompact::fill_range_in_dense_prefix(HeapWord* start, HeapWord* end) {
1680 #ifdef ASSERT
1681   {
1682     assert(start < end, "precondition");
1683     assert(mark_bitmap()->find_obj_beg(start, end) == end, "precondition");
1684     HeapWord* bottom = _space_info[old_space_id].space()->bottom();
1685     if (start != bottom) {
1686       // The preceding live obj.
1687       HeapWord* obj_start = mark_bitmap()->find_obj_beg_reverse(bottom, start);
1688       HeapWord* obj_end = obj_start + cast_to_oop(obj_start)->size();
1689       assert(obj_end == start, "precondition");
1690     }
1691   }
1692 #endif
1693 
1694   CollectedHeap::fill_with_objects(start, pointer_delta(end, start));
1695   HeapWord* addr = start;
1696   do {
1697     size_t size = cast_to_oop(addr)->size();
1698     start_array(old_space_id)->update_for_block(addr, addr + size);
1699     addr += size;
1700   } while (addr < end);
1701 }
1702 
1703 void PSParallelCompact::fill_dead_objs_in_dense_prefix() {
1704   ParMarkBitMap* bitmap = mark_bitmap();
1705 
1706   HeapWord* const bottom = _space_info[old_space_id].space()->bottom();
1707   HeapWord* const prefix_end = dense_prefix(old_space_id);
1708 
1709   const size_t region_size = ParallelCompactData::RegionSize;
1710 
1711   // Fill dead space in [start_addr, end_addr)
1712   HeapWord* const start_addr = bottom;
1713   HeapWord* const end_addr   = prefix_end;
1714 
1715   for (HeapWord* cur_addr = start_addr; cur_addr < end_addr; /* empty */) {
1716     RegionData* cur_region_ptr = _summary_data.addr_to_region_ptr(cur_addr);
1717     if (cur_region_ptr->data_size() == region_size) {
1718       // Full; no dead space. Next region.
1719       if (_summary_data.is_region_aligned(cur_addr)) {
1720         cur_addr += region_size;
1721       } else {
1722         cur_addr = _summary_data.region_align_up(cur_addr);
1723       }
1724       continue;
1725     }
1726 
1727     // Fill dead space inside cur_region.
1728     if (_summary_data.is_region_aligned(cur_addr)) {
1729       cur_addr += cur_region_ptr->partial_obj_size();
1730     }
1731 
1732     HeapWord* region_end_addr = _summary_data.region_align_up(cur_addr + 1);
1733     assert(region_end_addr <= end_addr, "inv");
1734     while (cur_addr < region_end_addr) {
1735       // Use end_addr to allow filler-obj to cross region boundary.
1736       HeapWord* live_start = bitmap->find_obj_beg(cur_addr, end_addr);
1737       if (cur_addr != live_start) {
1738         // Found dead space [cur_addr, live_start).
1739         fill_range_in_dense_prefix(cur_addr, live_start);
1740       }
1741       if (live_start >= region_end_addr) {
1742         cur_addr = live_start;
1743         break;
1744       }
1745       assert(bitmap->is_marked(live_start), "inv");
1746       cur_addr = live_start + cast_to_oop(live_start)->size();
1747     }
1748   }
1749 }
1750 
1751 void PSParallelCompact::compact() {
1752   GCTraceTime(Info, gc, phases) tm("Compaction Phase", &_gc_timer);
1753 
1754   uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers();
1755 
1756   initialize_shadow_regions(active_gc_threads);
1757   prepare_region_draining_tasks(active_gc_threads);
1758 
1759   {
1760     GCTraceTime(Trace, gc, phases) tm("Par Compact", &_gc_timer);
1761 
1762     FillDensePrefixAndCompactionTask task(active_gc_threads);
1763     ParallelScavengeHeap::heap()->workers().run_task(&task);
1764 
1765 #ifdef  ASSERT
1766     verify_filler_in_dense_prefix();
1767 
1768     // Verify that all regions have been processed.
1769     for (unsigned int id = old_space_id; id < last_space_id; ++id) {
1770       verify_complete(SpaceId(id));
1771     }
1772 #endif
1773   }
1774 }
1775 
1776 #ifdef  ASSERT
1777 void PSParallelCompact::verify_filler_in_dense_prefix() {
1778   HeapWord* bottom = _space_info[old_space_id].space()->bottom();
1779   HeapWord* dense_prefix_end = dense_prefix(old_space_id);
1780 
1781   const size_t region_size = ParallelCompactData::RegionSize;
1782 
1783   for (HeapWord* cur_addr = bottom; cur_addr < dense_prefix_end; /* empty */) {
1784     RegionData* cur_region_ptr = _summary_data.addr_to_region_ptr(cur_addr);
1785     if (cur_region_ptr->data_size() == region_size) {
1786       // Full; no dead space. Next region.
1787       if (_summary_data.is_region_aligned(cur_addr)) {
1788         cur_addr += region_size;
1789       } else {
1790         cur_addr = _summary_data.region_align_up(cur_addr);
1791       }
1792       continue;
1793     }
1794 
1795     // This region contains filler objs.
1796     if (_summary_data.is_region_aligned(cur_addr)) {
1797       cur_addr += cur_region_ptr->partial_obj_size();
1798     }
1799 
1800     HeapWord* region_end_addr = _summary_data.region_align_up(cur_addr + 1);
1801     assert(region_end_addr <= dense_prefix_end, "inv");
1802 
1803     while (cur_addr < region_end_addr) {
1804       oop obj = cast_to_oop(cur_addr);
1805       oopDesc::verify(obj);
1806       if (!mark_bitmap()->is_marked(cur_addr)) {
1807         Klass* k = cast_to_oop(cur_addr)->klass();
1808         assert(k == Universe::fillerArrayKlass() || k == vmClasses::FillerObject_klass(), "inv");
1809       }
1810       cur_addr += obj->size();
1811     }
1812   }
1813 }
1814 
1815 void PSParallelCompact::verify_complete(SpaceId space_id) {
1816   // All Regions served as compaction targets, from dense_prefix() to
1817   // new_top(), should be marked as filled and all Regions between new_top()
1818   // and top() should be available (i.e., should have been emptied).
1819   ParallelCompactData& sd = summary_data();
1820   SpaceInfo si = _space_info[space_id];
1821   HeapWord* new_top_addr = sd.region_align_up(si.new_top());
1822   HeapWord* old_top_addr = sd.region_align_up(si.space()->top());
1823   const size_t beg_region = sd.addr_to_region_idx(si.dense_prefix());
1824   const size_t new_top_region = sd.addr_to_region_idx(new_top_addr);
1825   const size_t old_top_region = sd.addr_to_region_idx(old_top_addr);
1826 
1827   size_t cur_region;
1828   for (cur_region = beg_region; cur_region < new_top_region; ++cur_region) {
1829     const RegionData* const c = sd.region(cur_region);
1830     assert(c->completed(), "region %zu not filled: destination_count=%u",
1831            cur_region, c->destination_count());
1832   }
1833 
1834   for (cur_region = new_top_region; cur_region < old_top_region; ++cur_region) {
1835     const RegionData* const c = sd.region(cur_region);
1836     assert(c->available(), "region %zu not empty: destination_count=%u",
1837            cur_region, c->destination_count());
1838   }
1839 }
1840 #endif  // #ifdef ASSERT
1841 
1842 // Return the SpaceId for the space containing addr.  If addr is not in the
1843 // heap, last_space_id is returned.  In debug mode it expects the address to be
1844 // in the heap and asserts such.
1845 PSParallelCompact::SpaceId PSParallelCompact::space_id(HeapWord* addr) {
1846   assert(ParallelScavengeHeap::heap()->is_in_reserved(addr), "addr not in the heap");
1847 
1848   for (unsigned int id = old_space_id; id < last_space_id; ++id) {
1849     if (_space_info[id].space()->contains(addr)) {
1850       return SpaceId(id);
1851     }
1852   }
1853 
1854   assert(false, "no space contains the addr");
1855   return last_space_id;
1856 }
1857 
1858 // Skip over count live words starting from beg, and return the address of the
1859 // next live word. Callers must also ensure that there are enough live words in
1860 // the range [beg, end) to skip.
1861 HeapWord* PSParallelCompact::skip_live_words(HeapWord* beg, HeapWord* end, size_t count)
1862 {
1863   ParMarkBitMap* m = mark_bitmap();
1864   HeapWord* cur_addr = beg;
1865   while (true) {
1866     cur_addr = m->find_obj_beg(cur_addr, end);
1867     assert(cur_addr < end, "inv");
1868     size_t obj_size = cast_to_oop(cur_addr)->size();
1869     // Strictly greater-than
1870     if (obj_size > count) {
1871       return cur_addr + count;
1872     }
1873     count -= obj_size;
1874     cur_addr += obj_size;
1875   }
1876 }
1877 
1878 // On starting to fill a destination region (dest-region), we need to know the
1879 // location of the word that will be at the start of the dest-region after
1880 // compaction. A dest-region can have one or more source regions, but only the
1881 // first source-region contains this location. This location is retrieved by
1882 // calling `first_src_addr` on a dest-region.
1883 // Conversely, a source-region has a dest-region which holds the destination of
1884 // the first live word on this source-region, based on which the destination
1885 // for the rest of live words can be derived.
1886 //
1887 // Note:
1888 // There is some complication due to space-boundary-fragmentation (an obj can't
1889 // cross space-boundary) -- a source-region may be split and behave like two
1890 // distinct regions with their own dest-region, as depicted below.
1891 //
1892 // source-region: region-n
1893 //
1894 // **********************
1895 // |     A|A~~~~B|B     |
1896 // **********************
1897 //    n-1     n     n+1
1898 //
1899 // AA, BB denote two live objs. ~~~~ denotes unknown number of live objs.
1900 //
1901 // Assuming the dest-region for region-n is the final region before
1902 // old-space-end and its first-live-word is the middle of AA, the heap content
1903 // will look like the following after compaction:
1904 //
1905 // **************                  *************
1906 //      A|A~~~~ |                  |BB    |
1907 // **************                  *************
1908 //              ^                  ^
1909 //              | old-space-end    | eden-space-start
1910 //
1911 // Therefore, in this example, region-n will have two dest-regions:
1912 // 1. the final region in old-space
1913 // 2. the first region in eden-space.
1914 // To handle this special case, we introduce the concept of split-region, whose
1915 // contents are relocated to two spaces. `SplitInfo` captures all necessary
1916 // info about the split, the first part, spliting-point, and the second part.
1917 HeapWord* PSParallelCompact::first_src_addr(HeapWord* const dest_addr,
1918                                             SpaceId src_space_id,
1919                                             size_t src_region_idx)
1920 {
1921   const size_t RegionSize = ParallelCompactData::RegionSize;
1922   const ParallelCompactData& sd = summary_data();
1923   assert(sd.is_region_aligned(dest_addr), "precondition");
1924 
1925   const RegionData* const src_region_ptr = sd.region(src_region_idx);
1926   assert(src_region_ptr->data_size() > 0, "src region cannot be empty");
1927 
1928   const size_t partial_obj_size = src_region_ptr->partial_obj_size();
1929   HeapWord* const src_region_destination = src_region_ptr->destination();
1930 
1931   HeapWord* const region_start = sd.region_to_addr(src_region_idx);
1932   HeapWord* const region_end = sd.region_to_addr(src_region_idx) + RegionSize;
1933 
1934   // Identify the actual destination for the first live words on this region,
1935   // taking split-region into account.
1936   HeapWord* region_start_destination;
1937   const SplitInfo& split_info = _space_info[src_space_id].split_info();
1938   if (split_info.is_split(src_region_idx)) {
1939     // The second part of this split region; use the recorded split point.
1940     if (dest_addr == src_region_destination) {
1941       return split_info.split_point();
1942     }
1943     region_start_destination = split_info.preceding_destination();
1944   } else {
1945     region_start_destination = src_region_destination;
1946   }
1947 
1948   // Calculate the offset to be skipped
1949   size_t words_to_skip = pointer_delta(dest_addr, region_start_destination);
1950 
1951   HeapWord* result;
1952   if (partial_obj_size > words_to_skip) {
1953     result = region_start + words_to_skip;
1954   } else {
1955     words_to_skip -= partial_obj_size;
1956     result = skip_live_words(region_start + partial_obj_size, region_end, words_to_skip);
1957   }
1958 
1959   if (split_info.is_split(src_region_idx)) {
1960     assert(result < split_info.split_point(), "postcondition");
1961   } else {
1962     assert(result < region_end, "postcondition");
1963   }
1964 
1965   return result;
1966 }
1967 
1968 void PSParallelCompact::decrement_destination_counts(ParCompactionManager* cm,
1969                                                      SpaceId src_space_id,
1970                                                      size_t beg_region,
1971                                                      HeapWord* end_addr)
1972 {
1973   ParallelCompactData& sd = summary_data();
1974 
1975 #ifdef ASSERT
1976   MutableSpace* const src_space = _space_info[src_space_id].space();
1977   HeapWord* const beg_addr = sd.region_to_addr(beg_region);
1978   assert(src_space->contains(beg_addr) || beg_addr == src_space->end(),
1979          "src_space_id does not match beg_addr");
1980   assert(src_space->contains(end_addr) || end_addr == src_space->end(),
1981          "src_space_id does not match end_addr");
1982 #endif // #ifdef ASSERT
1983 
1984   RegionData* const beg = sd.region(beg_region);
1985   RegionData* const end = sd.addr_to_region_ptr(sd.region_align_up(end_addr));
1986 
1987   // Regions up to new_top() are enqueued if they become available.
1988   HeapWord* const new_top = _space_info[src_space_id].new_top();
1989   RegionData* const enqueue_end =
1990     sd.addr_to_region_ptr(sd.region_align_up(new_top));
1991 
1992   for (RegionData* cur = beg; cur < end; ++cur) {
1993     assert(cur->data_size() > 0, "region must have live data");
1994     cur->decrement_destination_count();
1995     if (cur < enqueue_end && cur->available() && cur->claim()) {
1996       if (cur->mark_normal()) {
1997         cm->push_region(sd.region(cur));
1998       } else if (cur->mark_copied()) {
1999         // Try to copy the content of the shadow region back to its corresponding
2000         // heap region if the shadow region is filled. Otherwise, the GC thread
2001         // fills the shadow region will copy the data back (see
2002         // MoveAndUpdateShadowClosure::complete_region).
2003         copy_back(sd.region_to_addr(cur->shadow_region()), sd.region_to_addr(cur));
2004         ParCompactionManager::push_shadow_region_mt_safe(cur->shadow_region());
2005         cur->set_completed();
2006       }
2007     }
2008   }
2009 }
2010 
2011 size_t PSParallelCompact::next_src_region(MoveAndUpdateClosure& closure,
2012                                           SpaceId& src_space_id,
2013                                           HeapWord*& src_space_top,
2014                                           HeapWord* end_addr)
2015 {
2016   ParallelCompactData& sd = PSParallelCompact::summary_data();
2017 
2018   size_t src_region_idx = 0;
2019 
2020   // Skip empty regions (if any) up to the top of the space.
2021   HeapWord* const src_aligned_up = sd.region_align_up(end_addr);
2022   RegionData* src_region_ptr = sd.addr_to_region_ptr(src_aligned_up);
2023   HeapWord* const top_aligned_up = sd.region_align_up(src_space_top);
2024   const RegionData* const top_region_ptr = sd.addr_to_region_ptr(top_aligned_up);
2025 
2026   while (src_region_ptr < top_region_ptr && src_region_ptr->data_size() == 0) {
2027     ++src_region_ptr;
2028   }
2029 
2030   if (src_region_ptr < top_region_ptr) {
2031     // Found the first non-empty region in the same space.
2032     src_region_idx = sd.region(src_region_ptr);
2033     closure.set_source(sd.region_to_addr(src_region_idx));
2034     return src_region_idx;
2035   }
2036 
2037   // Switch to a new source space and find the first non-empty region.
2038   uint space_id = src_space_id + 1;
2039   assert(space_id < last_space_id, "not enough spaces");
2040 
2041   for (/* empty */; space_id < last_space_id; ++space_id) {
2042     HeapWord* bottom = _space_info[space_id].space()->bottom();
2043     HeapWord* top = _space_info[space_id].space()->top();
2044     // Skip empty space
2045     if (bottom == top) {
2046       continue;
2047     }
2048 
2049     // Identify the first region that contains live words in this space
2050     size_t cur_region = sd.addr_to_region_idx(bottom);
2051     size_t end_region = sd.addr_to_region_idx(sd.region_align_up(top));
2052 
2053     for (/* empty */ ; cur_region < end_region; ++cur_region) {
2054       RegionData* cur = sd.region(cur_region);
2055       if (cur->live_obj_size() > 0) {
2056         HeapWord* region_start_addr = sd.region_to_addr(cur_region);
2057 
2058         src_space_id = SpaceId(space_id);
2059         src_space_top = top;
2060         closure.set_source(region_start_addr);
2061         return cur_region;
2062       }
2063     }
2064   }
2065 
2066   ShouldNotReachHere();
2067 }
2068 
2069 HeapWord* PSParallelCompact::partial_obj_end(HeapWord* region_start_addr) {
2070   ParallelCompactData& sd = summary_data();
2071   assert(sd.is_region_aligned(region_start_addr), "precondition");
2072 
2073   // Use per-region partial_obj_size to locate the end of the obj, that extends
2074   // to region_start_addr.
2075   size_t start_region_idx = sd.addr_to_region_idx(region_start_addr);
2076   size_t end_region_idx = sd.region_count();
2077   size_t accumulated_size = 0;
2078   for (size_t region_idx = start_region_idx; region_idx < end_region_idx; ++region_idx) {
2079     size_t cur_partial_obj_size = sd.region(region_idx)->partial_obj_size();
2080     accumulated_size += cur_partial_obj_size;
2081     if (cur_partial_obj_size != ParallelCompactData::RegionSize) {
2082       break;
2083     }
2084   }
2085   return region_start_addr + accumulated_size;
2086 }
2087 
2088 // Use region_idx as the destination region, and evacuate all live objs on its
2089 // source regions to this destination region.
2090 void PSParallelCompact::fill_region(ParCompactionManager* cm, MoveAndUpdateClosure& closure, size_t region_idx)
2091 {
2092   ParMarkBitMap* const bitmap = mark_bitmap();
2093   ParallelCompactData& sd = summary_data();
2094   RegionData* const region_ptr = sd.region(region_idx);
2095 
2096   // Get the source region and related info.
2097   size_t src_region_idx = region_ptr->source_region();
2098   SpaceId src_space_id = space_id(sd.region_to_addr(src_region_idx));
2099   HeapWord* src_space_top = _space_info[src_space_id].space()->top();
2100   HeapWord* dest_addr = sd.region_to_addr(region_idx);
2101 
2102   closure.set_source(first_src_addr(dest_addr, src_space_id, src_region_idx));
2103 
2104   // Adjust src_region_idx to prepare for decrementing destination counts (the
2105   // destination count is not decremented when a region is copied to itself).
2106   if (src_region_idx == region_idx) {
2107     src_region_idx += 1;
2108   }
2109 
2110   // source-region:
2111   //
2112   // **********
2113   // |   ~~~  |
2114   // **********
2115   //      ^
2116   //      |-- closure.source() / first_src_addr
2117   //
2118   //
2119   // ~~~ : live words
2120   //
2121   // destination-region:
2122   //
2123   // **********
2124   // |        |
2125   // **********
2126   // ^
2127   // |-- region-start
2128   if (bitmap->is_unmarked(closure.source())) {
2129     // An object overflows the previous destination region, so this
2130     // destination region should copy the remainder of the object or as much as
2131     // will fit.
2132     HeapWord* const old_src_addr = closure.source();
2133     {
2134       HeapWord* region_start = sd.region_align_down(closure.source());
2135       HeapWord* obj_start = bitmap->find_obj_beg_reverse(region_start, closure.source());
2136       HeapWord* obj_end;
2137       if (obj_start != closure.source()) {
2138         assert(bitmap->is_marked(obj_start), "inv");
2139         // Found the actual obj-start, try to find the obj-end using either
2140         // size() if this obj is completely contained in the current region.
2141         HeapWord* next_region_start = region_start + ParallelCompactData::RegionSize;
2142         HeapWord* partial_obj_start = (next_region_start >= src_space_top)
2143                                       ? nullptr
2144                                       : sd.addr_to_region_ptr(next_region_start)->partial_obj_addr();
2145         // This obj extends to next region iff partial_obj_addr of the *next*
2146         // region is the same as obj-start.
2147         if (partial_obj_start == obj_start) {
2148           // This obj extends to next region.
2149           obj_end = partial_obj_end(next_region_start);
2150         } else {
2151           // Completely contained in this region; safe to use size().
2152           obj_end = obj_start + cast_to_oop(obj_start)->size();
2153         }
2154       } else {
2155         // This obj extends to current region.
2156         obj_end = partial_obj_end(region_start);
2157       }
2158       size_t partial_obj_size = pointer_delta(obj_end, closure.source());
2159       closure.copy_partial_obj(partial_obj_size);
2160     }
2161 
2162     if (closure.is_full()) {
2163       decrement_destination_counts(cm, src_space_id, src_region_idx, closure.source());
2164       closure.complete_region(dest_addr, region_ptr);
2165       return;
2166     }
2167 
2168     // Finished copying without using up the current destination-region
2169     HeapWord* const end_addr = sd.region_align_down(closure.source());
2170     if (sd.region_align_down(old_src_addr) != end_addr) {
2171       assert(sd.region_align_up(old_src_addr) == end_addr, "only one region");
2172       // The partial object was copied from more than one source region.
2173       decrement_destination_counts(cm, src_space_id, src_region_idx, end_addr);
2174 
2175       // Move to the next source region, possibly switching spaces as well.  All
2176       // args except end_addr may be modified.
2177       src_region_idx = next_src_region(closure, src_space_id, src_space_top, end_addr);
2178     }
2179   }
2180 
2181   // Handle the rest obj-by-obj, where we know obj-start.
2182   do {
2183     HeapWord* cur_addr = closure.source();
2184     HeapWord* const end_addr = MIN2(sd.region_align_up(cur_addr + 1),
2185                                     src_space_top);
2186     // To handle the case where the final obj in source region extends to next region.
2187     HeapWord* final_obj_start = (end_addr == src_space_top)
2188                                 ? nullptr
2189                                 : sd.addr_to_region_ptr(end_addr)->partial_obj_addr();
2190     // Apply closure on objs inside [cur_addr, end_addr)
2191     do {
2192       cur_addr = bitmap->find_obj_beg(cur_addr, end_addr);
2193       if (cur_addr == end_addr) {
2194         break;
2195       }
2196       size_t obj_size;
2197       if (final_obj_start == cur_addr) {
2198         obj_size = pointer_delta(partial_obj_end(end_addr), cur_addr);
2199       } else {
2200         // This obj doesn't extend into next region; size() is safe to use.
2201         obj_size = cast_to_oop(cur_addr)->size();
2202       }
2203       closure.do_addr(cur_addr, obj_size);
2204       cur_addr += obj_size;
2205     } while (cur_addr < end_addr && !closure.is_full());
2206 
2207     if (closure.is_full()) {
2208       decrement_destination_counts(cm, src_space_id, src_region_idx, closure.source());
2209       closure.complete_region(dest_addr, region_ptr);
2210       return;
2211     }
2212 
2213     decrement_destination_counts(cm, src_space_id, src_region_idx, end_addr);
2214 
2215     // Move to the next source region, possibly switching spaces as well.  All
2216     // args except end_addr may be modified.
2217     src_region_idx = next_src_region(closure, src_space_id, src_space_top, end_addr);
2218   } while (true);
2219 }
2220 
2221 void PSParallelCompact::fill_and_update_region(ParCompactionManager* cm, size_t region_idx)
2222 {
2223   MoveAndUpdateClosure cl(mark_bitmap(), region_idx);
2224   fill_region(cm, cl, region_idx);
2225 }
2226 
2227 void PSParallelCompact::fill_and_update_shadow_region(ParCompactionManager* cm, size_t region_idx)
2228 {
2229   // Get a shadow region first
2230   ParallelCompactData& sd = summary_data();
2231   RegionData* const region_ptr = sd.region(region_idx);
2232   size_t shadow_region = ParCompactionManager::pop_shadow_region_mt_safe(region_ptr);
2233   // The InvalidShadow return value indicates the corresponding heap region is available,
2234   // so use MoveAndUpdateClosure to fill the normal region. Otherwise, use
2235   // MoveAndUpdateShadowClosure to fill the acquired shadow region.
2236   if (shadow_region == ParCompactionManager::InvalidShadow) {
2237     MoveAndUpdateClosure cl(mark_bitmap(), region_idx);
2238     region_ptr->shadow_to_normal();
2239     return fill_region(cm, cl, region_idx);
2240   } else {
2241     MoveAndUpdateShadowClosure cl(mark_bitmap(), region_idx, shadow_region);
2242     return fill_region(cm, cl, region_idx);
2243   }
2244 }
2245 
2246 void PSParallelCompact::copy_back(HeapWord *shadow_addr, HeapWord *region_addr)
2247 {
2248   Copy::aligned_conjoint_words(shadow_addr, region_addr, _summary_data.RegionSize);
2249 }
2250 
2251 bool PSParallelCompact::steal_unavailable_region(ParCompactionManager* cm, size_t &region_idx)
2252 {
2253   size_t next = cm->next_shadow_region();
2254   ParallelCompactData& sd = summary_data();
2255   size_t old_new_top = sd.addr_to_region_idx(_space_info[old_space_id].new_top());
2256   uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers();
2257 
2258   while (next < old_new_top) {
2259     if (sd.region(next)->mark_shadow()) {
2260       region_idx = next;
2261       return true;
2262     }
2263     next = cm->move_next_shadow_region_by(active_gc_threads);
2264   }
2265 
2266   return false;
2267 }
2268 
2269 // The shadow region is an optimization to address region dependencies in full GC. The basic
2270 // idea is making more regions available by temporally storing their live objects in empty
2271 // shadow regions to resolve dependencies between them and the destination regions. Therefore,
2272 // GC threads need not wait destination regions to be available before processing sources.
2273 //
2274 // A typical workflow would be:
2275 // After draining its own stack and failing to steal from others, a GC worker would pick an
2276 // unavailable region (destination count > 0) and get a shadow region. Then the worker fills
2277 // the shadow region by copying live objects from source regions of the unavailable one. Once
2278 // the unavailable region becomes available, the data in the shadow region will be copied back.
2279 // Shadow regions are empty regions in the to-space and regions between top and end of other spaces.
2280 void PSParallelCompact::initialize_shadow_regions(uint parallel_gc_threads)
2281 {
2282   const ParallelCompactData& sd = PSParallelCompact::summary_data();
2283 
2284   for (unsigned int id = old_space_id; id < last_space_id; ++id) {
2285     SpaceInfo* const space_info = _space_info + id;
2286     MutableSpace* const space = space_info->space();
2287 
2288     const size_t beg_region =
2289       sd.addr_to_region_idx(sd.region_align_up(MAX2(space_info->new_top(), space->top())));
2290     const size_t end_region =
2291       sd.addr_to_region_idx(sd.region_align_down(space->end()));
2292 
2293     for (size_t cur = beg_region; cur < end_region; ++cur) {
2294       ParCompactionManager::push_shadow_region(cur);
2295     }
2296   }
2297 
2298   size_t beg_region = sd.addr_to_region_idx(_space_info[old_space_id].dense_prefix());
2299   for (uint i = 0; i < parallel_gc_threads; i++) {
2300     ParCompactionManager *cm = ParCompactionManager::gc_thread_compaction_manager(i);
2301     cm->set_next_shadow_region(beg_region + i);
2302   }
2303 }
2304 
2305 void MoveAndUpdateClosure::copy_partial_obj(size_t partial_obj_size)
2306 {
2307   size_t words = MIN2(partial_obj_size, words_remaining());
2308 
2309   // This test is necessary; if omitted, the pointer updates to a partial object
2310   // that crosses the dense prefix boundary could be overwritten.
2311   if (source() != copy_destination()) {
2312     DEBUG_ONLY(PSParallelCompact::check_new_location(source(), destination());)
2313     Copy::aligned_conjoint_words(source(), copy_destination(), words);
2314   }
2315   update_state(words);
2316 }
2317 
2318 void MoveAndUpdateClosure::complete_region(HeapWord* dest_addr, PSParallelCompact::RegionData* region_ptr) {
2319   assert(region_ptr->shadow_state() == ParallelCompactData::RegionData::NormalRegion, "Region should be finished");
2320   region_ptr->set_completed();
2321 }
2322 
2323 void MoveAndUpdateClosure::do_addr(HeapWord* addr, size_t words) {
2324   assert(destination() != nullptr, "sanity");
2325   _source = addr;
2326 
2327   // The start_array must be updated even if the object is not moving.
2328   if (_start_array != nullptr) {
2329     _start_array->update_for_block(destination(), destination() + words);
2330   }
2331 
2332   // Avoid overflow
2333   words = MIN2(words, words_remaining());
2334   assert(words > 0, "inv");
2335 
2336   if (copy_destination() != source()) {
2337     DEBUG_ONLY(PSParallelCompact::check_new_location(source(), destination());)
2338     assert(source() != destination(), "inv");
2339     assert(FullGCForwarding::is_forwarded(cast_to_oop(source())), "inv");
2340     assert(FullGCForwarding::forwardee(cast_to_oop(source())) == cast_to_oop(destination()), "inv");
2341     Copy::aligned_conjoint_words(source(), copy_destination(), words);
2342     cast_to_oop(copy_destination())->reinit_mark();
2343   }
2344 
2345   update_state(words);
2346 }
2347 
2348 void MoveAndUpdateShadowClosure::complete_region(HeapWord* dest_addr, PSParallelCompact::RegionData* region_ptr) {
2349   assert(region_ptr->shadow_state() == ParallelCompactData::RegionData::ShadowRegion, "Region should be shadow");
2350   // Record the shadow region index
2351   region_ptr->set_shadow_region(_shadow);
2352   // Mark the shadow region as filled to indicate the data is ready to be
2353   // copied back
2354   region_ptr->mark_filled();
2355   // Try to copy the content of the shadow region back to its corresponding
2356   // heap region if available; the GC thread that decreases the destination
2357   // count to zero will do the copying otherwise (see
2358   // PSParallelCompact::decrement_destination_counts).
2359   if (((region_ptr->available() && region_ptr->claim()) || region_ptr->claimed()) && region_ptr->mark_copied()) {
2360     region_ptr->set_completed();
2361     PSParallelCompact::copy_back(PSParallelCompact::summary_data().region_to_addr(_shadow), dest_addr);
2362     ParCompactionManager::push_shadow_region_mt_safe(_shadow);
2363   }
2364 }