1 /*
   2  * Copyright (c) 2015, 2023, 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 #include "precompiled.hpp"
  25 #include "gc/shared/gc_globals.hpp"
  26 #include "gc/shared/suspendibleThreadSet.hpp"
  27 #include "gc/z/zAbort.inline.hpp"
  28 #include "gc/z/zAddress.inline.hpp"
  29 #include "gc/z/zAllocator.inline.hpp"
  30 #include "gc/z/zBarrier.inline.hpp"
  31 #include "gc/z/zCollectedHeap.hpp"
  32 #include "gc/z/zForwarding.inline.hpp"
  33 #include "gc/z/zGeneration.inline.hpp"
  34 #include "gc/z/zHeap.inline.hpp"
  35 #include "gc/z/zIndexDistributor.inline.hpp"
  36 #include "gc/z/zIterator.inline.hpp"
  37 #include "gc/z/zPage.inline.hpp"
  38 #include "gc/z/zPageAge.hpp"
  39 #include "gc/z/zRelocate.hpp"
  40 #include "gc/z/zRelocationSet.inline.hpp"
  41 #include "gc/z/zRootsIterator.hpp"
  42 #include "gc/z/zStackWatermark.hpp"
  43 #include "gc/z/zStat.hpp"
  44 #include "gc/z/zTask.hpp"
  45 #include "gc/z/zUncoloredRoot.inline.hpp"
  46 #include "gc/z/zVerify.hpp"
  47 #include "gc/z/zWorkers.hpp"
  48 #include "prims/jvmtiTagMap.hpp"
  49 #include "runtime/atomic.hpp"
  50 #include "utilities/debug.hpp"
  51 
  52 static const ZStatCriticalPhase ZCriticalPhaseRelocationStall("Relocation Stall");
  53 static const ZStatSubPhase ZSubPhaseConcurrentRelocateRememberedSetFlipPromotedYoung("Concurrent Relocate Remset FP", ZGenerationId::young);
  54 
  55 static uintptr_t forwarding_index(ZForwarding* forwarding, zoffset from_offset) {
  56   return (from_offset - forwarding->start()) >> forwarding->object_alignment_shift();
  57 }
  58 
  59 static zaddress forwarding_find(ZForwarding* forwarding, zoffset from_offset, ZForwardingCursor* cursor) {
  60   const uintptr_t from_index = forwarding_index(forwarding, from_offset);
  61   const ZForwardingEntry entry = forwarding->find(from_index, cursor);
  62   return entry.populated() ? ZOffset::address(to_zoffset(entry.to_offset())) : zaddress::null;
  63 }
  64 
  65 static zaddress forwarding_find(ZForwarding* forwarding, zaddress_unsafe from_addr, ZForwardingCursor* cursor) {
  66   return forwarding_find(forwarding, ZAddress::offset(from_addr), cursor);
  67 }
  68 
  69 static zaddress forwarding_find(ZForwarding* forwarding, zaddress from_addr, ZForwardingCursor* cursor) {
  70   return forwarding_find(forwarding, ZAddress::offset(from_addr), cursor);
  71 }
  72 
  73 static zaddress forwarding_insert(ZForwarding* forwarding, zoffset from_offset, zaddress to_addr, ZForwardingCursor* cursor) {
  74   const uintptr_t from_index = forwarding_index(forwarding, from_offset);
  75   const zoffset to_offset = ZAddress::offset(to_addr);
  76   const zoffset to_offset_final = forwarding->insert(from_index, to_offset, cursor);
  77   return ZOffset::address(to_offset_final);
  78 }
  79 
  80 static zaddress forwarding_insert(ZForwarding* forwarding, zaddress from_addr, zaddress to_addr, ZForwardingCursor* cursor) {
  81   return forwarding_insert(forwarding, ZAddress::offset(from_addr), to_addr, cursor);
  82 }
  83 
  84 ZRelocateQueue::ZRelocateQueue()
  85   : _lock(),
  86     _queue(),
  87     _nworkers(0),
  88     _nsynchronized(0),
  89     _synchronize(false),
  90     _needs_attention(0) {}
  91 
  92 bool ZRelocateQueue::needs_attention() const {
  93   return Atomic::load(&_needs_attention) != 0;
  94 }
  95 
  96 void ZRelocateQueue::inc_needs_attention() {
  97   const int needs_attention = Atomic::add(&_needs_attention, 1);
  98   assert(needs_attention == 1 || needs_attention == 2, "Invalid state");
  99 }
 100 
 101 void ZRelocateQueue::dec_needs_attention() {
 102   const int needs_attention = Atomic::sub(&_needs_attention, 1);
 103   assert(needs_attention == 0 || needs_attention == 1, "Invalid state");
 104 }
 105 
 106 void ZRelocateQueue::join(uint nworkers) {
 107   assert(nworkers != 0, "Must request at least one worker");
 108   assert(_nworkers == 0, "Invalid state");
 109   assert(_nsynchronized == 0, "Invalid state");
 110 
 111   log_debug(gc, reloc)("Joining workers: %u", nworkers);
 112 
 113   _nworkers = nworkers;
 114 }
 115 
 116 void ZRelocateQueue::resize_workers(uint nworkers) {
 117   assert(nworkers != 0, "Must request at least one worker");
 118   assert(_nworkers == 0, "Invalid state");
 119   assert(_nsynchronized == 0, "Invalid state");
 120 
 121   log_debug(gc, reloc)("Resize workers: %u", nworkers);
 122 
 123   ZLocker<ZConditionLock> locker(&_lock);
 124   _nworkers = nworkers;
 125 }
 126 
 127 void ZRelocateQueue::leave() {
 128   ZLocker<ZConditionLock> locker(&_lock);
 129   _nworkers--;
 130 
 131   assert(_nsynchronized <= _nworkers, "_nsynchronized: %u _nworkers: %u", _nsynchronized, _nworkers);
 132 
 133   log_debug(gc, reloc)("Leaving workers: left: %u _synchronize: %d _nsynchronized: %u", _nworkers, _synchronize, _nsynchronized);
 134 
 135   // Prune done forwardings
 136   const bool forwardings_done = prune();
 137 
 138   // Check if all workers synchronized
 139   const bool last_synchronized = _synchronize && _nworkers == _nsynchronized;
 140 
 141   if (forwardings_done || last_synchronized) {
 142     _lock.notify_all();
 143   }
 144 }
 145 
 146 void ZRelocateQueue::add_and_wait(ZForwarding* forwarding) {
 147   ZStatTimer timer(ZCriticalPhaseRelocationStall);
 148   ZLocker<ZConditionLock> locker(&_lock);
 149 
 150   if (forwarding->is_done()) {
 151     return;
 152   }
 153 
 154   _queue.append(forwarding);
 155   if (_queue.length() == 1) {
 156     // Queue became non-empty
 157     inc_needs_attention();
 158     _lock.notify_all();
 159   }
 160 
 161   while (!forwarding->is_done()) {
 162     _lock.wait();
 163   }
 164 }
 165 
 166 bool ZRelocateQueue::prune() {
 167   if (_queue.is_empty()) {
 168     return false;
 169   }
 170 
 171   bool done = false;
 172 
 173   for (int i = 0; i < _queue.length();) {
 174     const ZForwarding* const forwarding = _queue.at(i);
 175     if (forwarding->is_done()) {
 176       done = true;
 177 
 178       _queue.delete_at(i);
 179     } else {
 180       i++;
 181     }
 182   }
 183 
 184   if (_queue.is_empty()) {
 185     dec_needs_attention();
 186   }
 187 
 188   return done;
 189 }
 190 
 191 ZForwarding* ZRelocateQueue::prune_and_claim() {
 192   if (prune()) {
 193     _lock.notify_all();
 194   }
 195 
 196   for (int i = 0; i < _queue.length(); i++) {
 197     ZForwarding* const forwarding = _queue.at(i);
 198     if (forwarding->claim()) {
 199       return forwarding;
 200     }
 201   }
 202 
 203   return nullptr;
 204 }
 205 
 206 class ZRelocateQueueSynchronizeThread {
 207 private:
 208   ZRelocateQueue* const _queue;
 209 
 210 public:
 211   ZRelocateQueueSynchronizeThread(ZRelocateQueue* queue)
 212     : _queue(queue) {
 213     _queue->synchronize_thread();
 214   }
 215 
 216   ~ZRelocateQueueSynchronizeThread() {
 217     _queue->desynchronize_thread();
 218   }
 219 };
 220 
 221 void ZRelocateQueue::synchronize_thread() {
 222   _nsynchronized++;
 223 
 224   log_debug(gc, reloc)("Synchronize worker _nsynchronized %u", _nsynchronized);
 225 
 226   assert(_nsynchronized <= _nworkers, "_nsynchronized: %u _nworkers: %u", _nsynchronized, _nworkers);
 227   if (_nsynchronized == _nworkers) {
 228     // All workers synchronized
 229     _lock.notify_all();
 230   }
 231 }
 232 
 233 void ZRelocateQueue::desynchronize_thread() {
 234   _nsynchronized--;
 235 
 236   log_debug(gc, reloc)("Desynchronize worker _nsynchronized %u", _nsynchronized);
 237 
 238   assert(_nsynchronized < _nworkers, "_nsynchronized: %u _nworkers: %u", _nsynchronized, _nworkers);
 239 }
 240 
 241 ZForwarding* ZRelocateQueue::synchronize_poll() {
 242   // Fast path avoids locking
 243   if (!needs_attention()) {
 244     return nullptr;
 245   }
 246 
 247   // Slow path to get the next forwarding and/or synchronize
 248   ZLocker<ZConditionLock> locker(&_lock);
 249 
 250   {
 251     ZForwarding* const forwarding = prune_and_claim();
 252     if (forwarding != nullptr) {
 253       // Don't become synchronized while there are elements in the queue
 254       return forwarding;
 255     }
 256   }
 257 
 258   if (!_synchronize) {
 259     return nullptr;
 260   }
 261 
 262   ZRelocateQueueSynchronizeThread rqst(this);
 263 
 264   do {
 265     _lock.wait();
 266 
 267     ZForwarding* const forwarding = prune_and_claim();
 268     if (forwarding != nullptr) {
 269       return forwarding;
 270     }
 271   } while (_synchronize);
 272 
 273   return nullptr;
 274 }
 275 
 276 void ZRelocateQueue::clear() {
 277   assert(_nworkers == 0, "Invalid state");
 278 
 279   if (_queue.is_empty()) {
 280     return;
 281   }
 282 
 283   ZArrayIterator<ZForwarding*> iter(&_queue);
 284   for (ZForwarding* forwarding; iter.next(&forwarding);) {
 285     assert(forwarding->is_done(), "All should be done");
 286   }
 287 
 288   assert(false, "Clear was not empty");
 289 
 290   _queue.clear();
 291   dec_needs_attention();
 292 }
 293 
 294 void ZRelocateQueue::synchronize() {
 295   ZLocker<ZConditionLock> locker(&_lock);
 296   _synchronize = true;
 297 
 298   inc_needs_attention();
 299 
 300   log_debug(gc, reloc)("Synchronize all workers 1 _nworkers: %u _nsynchronized: %u", _nworkers, _nsynchronized);
 301 
 302   while (_nworkers != _nsynchronized) {
 303     _lock.wait();
 304     log_debug(gc, reloc)("Synchronize all workers 2 _nworkers: %u _nsynchronized: %u", _nworkers, _nsynchronized);
 305   }
 306 }
 307 
 308 void ZRelocateQueue::desynchronize() {
 309   ZLocker<ZConditionLock> locker(&_lock);
 310   _synchronize = false;
 311 
 312   log_debug(gc, reloc)("Desynchronize all workers _nworkers: %u _nsynchronized: %u", _nworkers, _nsynchronized);
 313 
 314   assert(_nsynchronized <= _nworkers, "_nsynchronized: %u _nworkers: %u", _nsynchronized, _nworkers);
 315 
 316   dec_needs_attention();
 317 
 318   _lock.notify_all();
 319 }
 320 
 321 ZRelocate::ZRelocate(ZGeneration* generation)
 322   : _generation(generation),
 323     _queue() {}
 324 
 325 ZWorkers* ZRelocate::workers() const {
 326   return _generation->workers();
 327 }
 328 
 329 void ZRelocate::start() {
 330   _queue.join(workers()->active_workers());
 331 }
 332 
 333 void ZRelocate::add_remset(volatile zpointer* p) {
 334   ZGeneration::young()->remember(p);
 335 }
 336 
 337 static zaddress relocate_object_inner(ZForwarding* forwarding, zaddress from_addr, ZForwardingCursor* cursor) {
 338   assert(ZHeap::heap()->is_object_live(from_addr), "Should be live");
 339 
 340   // Allocate object
 341   const size_t size = ZUtils::object_size(from_addr);
 342 
 343   ZAllocatorForRelocation* allocator = ZAllocator::relocation(forwarding->to_age());
 344 
 345   const zaddress to_addr = allocator->alloc_object(size);
 346 
 347   if (is_null(to_addr)) {
 348     // Allocation failed
 349     return zaddress::null;
 350   }
 351 
 352   // Copy object
 353   ZUtils::object_copy_disjoint(from_addr, to_addr, size);
 354 
 355   // Insert forwarding
 356   const zaddress to_addr_final = forwarding_insert(forwarding, from_addr, to_addr, cursor);
 357 
 358   if (to_addr_final != to_addr) {
 359     // Already relocated, try undo allocation
 360     allocator->undo_alloc_object(to_addr, size);
 361   }
 362 
 363   return to_addr_final;
 364 }
 365 
 366 zaddress ZRelocate::relocate_object(ZForwarding* forwarding, zaddress_unsafe from_addr) {
 367   ZForwardingCursor cursor;
 368 
 369   // Lookup forwarding
 370   zaddress to_addr = forwarding_find(forwarding, from_addr, &cursor);
 371   if (!is_null(to_addr)) {
 372     // Already relocated
 373     return to_addr;
 374   }
 375 
 376   // Relocate object
 377   if (forwarding->retain_page(&_queue)) {
 378     assert(_generation->is_phase_relocate(), "Must be");
 379     to_addr = relocate_object_inner(forwarding, safe(from_addr), &cursor);
 380     forwarding->release_page();
 381 
 382     if (!is_null(to_addr)) {
 383       // Success
 384       return to_addr;
 385     }
 386 
 387     // Failed to relocate object. Signal and wait for a worker thread to
 388     // complete relocation of this page, and then forward the object.
 389     _queue.add_and_wait(forwarding);
 390   }
 391 
 392   // Forward object
 393   return forward_object(forwarding, from_addr);
 394 }
 395 
 396 zaddress ZRelocate::forward_object(ZForwarding* forwarding, zaddress_unsafe from_addr) {
 397   ZForwardingCursor cursor;
 398   const zaddress to_addr = forwarding_find(forwarding, from_addr, &cursor);
 399   assert(!is_null(to_addr), "Should be forwarded: " PTR_FORMAT, untype(from_addr));
 400   return to_addr;
 401 }
 402 
 403 static ZPage* alloc_page(ZAllocatorForRelocation* allocator, ZPageType type, size_t size) {
 404   if (ZStressRelocateInPlace) {
 405     // Simulate failure to allocate a new page. This will
 406     // cause the page being relocated to be relocated in-place.
 407     return nullptr;
 408   }
 409 
 410   ZAllocationFlags flags;
 411   flags.set_non_blocking();
 412   flags.set_gc_relocation();
 413 
 414   return allocator->alloc_page_for_relocation(type, size, flags);
 415 }
 416 
 417 static void retire_target_page(ZGeneration* generation, ZPage* page) {
 418   if (generation->is_young() && page->is_old()) {
 419     generation->increase_promoted(page->used());
 420   } else {
 421     generation->increase_compacted(page->used());
 422   }
 423 
 424   // Free target page if it is empty. We can end up with an empty target
 425   // page if we allocated a new target page, and then lost the race to
 426   // relocate the remaining objects, leaving the target page empty when
 427   // relocation completed.
 428   if (page->used() == 0) {
 429     ZHeap::heap()->free_page(page);
 430   }
 431 }
 432 
 433 class ZRelocateSmallAllocator {
 434 private:
 435   ZGeneration* const _generation;
 436   volatile size_t    _in_place_count;
 437 
 438 public:
 439   ZRelocateSmallAllocator(ZGeneration* generation)
 440     : _generation(generation),
 441       _in_place_count(0) {}
 442 
 443   ZPage* alloc_and_retire_target_page(ZForwarding* forwarding, ZPage* target) {
 444     ZAllocatorForRelocation* const allocator = ZAllocator::relocation(forwarding->to_age());
 445     ZPage* const page = alloc_page(allocator, forwarding->type(), forwarding->size());
 446     if (page == nullptr) {
 447       Atomic::inc(&_in_place_count);
 448     }
 449 
 450     if (target != nullptr) {
 451       // Retire the old target page
 452       retire_target_page(_generation, target);
 453     }
 454 
 455     return page;
 456   }
 457 
 458   void share_target_page(ZPage* page) {
 459     // Does nothing
 460   }
 461 
 462   void free_target_page(ZPage* page) {
 463     if (page != nullptr) {
 464       retire_target_page(_generation, page);
 465     }
 466   }
 467 
 468   zaddress alloc_object(ZPage* page, size_t size) const {
 469     return (page != nullptr) ? page->alloc_object(size) : zaddress::null;
 470   }
 471 
 472   void undo_alloc_object(ZPage* page, zaddress addr, size_t size) const {
 473     page->undo_alloc_object(addr, size);
 474   }
 475 
 476   const size_t in_place_count() const {
 477     return _in_place_count;
 478   }
 479 };
 480 
 481 class ZRelocateMediumAllocator {
 482 private:
 483   ZGeneration* const _generation;
 484   ZConditionLock     _lock;
 485   ZPage*             _shared[ZAllocator::_relocation_allocators];
 486   bool               _in_place;
 487   volatile size_t    _in_place_count;
 488 
 489 public:
 490   ZRelocateMediumAllocator(ZGeneration* generation)
 491     : _generation(generation),
 492       _lock(),
 493       _shared(),
 494       _in_place(false),
 495       _in_place_count(0) {}
 496 
 497   ~ZRelocateMediumAllocator() {
 498     for (uint i = 0; i < ZAllocator::_relocation_allocators; ++i) {
 499       if (_shared[i] != nullptr) {
 500         retire_target_page(_generation, _shared[i]);
 501       }
 502     }
 503   }
 504 
 505   ZPage* shared(ZPageAge age) {
 506     return _shared[static_cast<uint>(age) - 1];
 507   }
 508 
 509   void set_shared(ZPageAge age, ZPage* page) {
 510     _shared[static_cast<uint>(age) - 1] = page;
 511   }
 512 
 513   ZPage* alloc_and_retire_target_page(ZForwarding* forwarding, ZPage* target) {
 514     ZLocker<ZConditionLock> locker(&_lock);
 515 
 516     // Wait for any ongoing in-place relocation to complete
 517     while (_in_place) {
 518       _lock.wait();
 519     }
 520 
 521     // Allocate a new page only if the shared page is the same as the
 522     // current target page. The shared page will be different from the
 523     // current target page if another thread shared a page, or allocated
 524     // a new page.
 525     const ZPageAge to_age = forwarding->to_age();
 526     if (shared(to_age) == target) {
 527       ZAllocatorForRelocation* const allocator = ZAllocator::relocation(forwarding->to_age());
 528       ZPage* const to_page = alloc_page(allocator, forwarding->type(), forwarding->size());
 529       set_shared(to_age, to_page);
 530       if (to_page == nullptr) {
 531         Atomic::inc(&_in_place_count);
 532         _in_place = true;
 533       }
 534 
 535       // This thread is responsible for retiring the shared target page
 536       if (target != nullptr) {
 537         retire_target_page(_generation, target);
 538       }
 539     }
 540 
 541     return shared(to_age);
 542   }
 543 
 544   void share_target_page(ZPage* page) {
 545     const ZPageAge age = page->age();
 546 
 547     ZLocker<ZConditionLock> locker(&_lock);
 548     assert(_in_place, "Invalid state");
 549     assert(shared(age) == nullptr, "Invalid state");
 550     assert(page != nullptr, "Invalid page");
 551 
 552     set_shared(age, page);
 553     _in_place = false;
 554 
 555     _lock.notify_all();
 556   }
 557 
 558   void free_target_page(ZPage* page) {
 559     // Does nothing
 560   }
 561 
 562   zaddress alloc_object(ZPage* page, size_t size) const {
 563     return (page != nullptr) ? page->alloc_object_atomic(size) : zaddress::null;
 564   }
 565 
 566   void undo_alloc_object(ZPage* page, zaddress addr, size_t size) const {
 567     page->undo_alloc_object_atomic(addr, size);
 568   }
 569 
 570   const size_t in_place_count() const {
 571     return _in_place_count;
 572   }
 573 };
 574 
 575 template <typename Allocator>
 576 class ZRelocateWork : public StackObj {
 577 private:
 578   Allocator* const   _allocator;
 579   ZForwarding*       _forwarding;
 580   ZPage*             _target[ZAllocator::_relocation_allocators];
 581   ZGeneration* const _generation;
 582   size_t             _other_promoted;
 583   size_t             _other_compacted;
 584 
 585   ZPage* target(ZPageAge age) {
 586     return _target[static_cast<uint>(age) - 1];
 587   }
 588 
 589   void set_target(ZPageAge age, ZPage* page) {
 590     _target[static_cast<uint>(age) - 1] = page;
 591   }
 592 
 593   size_t object_alignment() const {
 594     return (size_t)1 << _forwarding->object_alignment_shift();
 595   }
 596 
 597   void increase_other_forwarded(size_t unaligned_object_size) {
 598     const size_t aligned_size = align_up(unaligned_object_size, object_alignment());
 599     if (_forwarding->is_promotion()) {
 600       _other_promoted += aligned_size;
 601     } else {
 602       _other_compacted += aligned_size;
 603     }
 604   }
 605 
 606   zaddress try_relocate_object_inner(zaddress from_addr) {
 607     ZForwardingCursor cursor;
 608 
 609     const size_t size = ZUtils::object_size(from_addr);
 610     ZPage* const to_page = target(_forwarding->to_age());
 611 
 612     // Lookup forwarding
 613     {
 614       const zaddress to_addr = forwarding_find(_forwarding, from_addr, &cursor);
 615       if (!is_null(to_addr)) {
 616         // Already relocated
 617         increase_other_forwarded(size);
 618         return to_addr;
 619       }
 620     }
 621 
 622     // Allocate object
 623     const zaddress allocated_addr = _allocator->alloc_object(to_page, size);
 624     if (is_null(allocated_addr)) {
 625       // Allocation failed
 626       return zaddress::null;
 627     }
 628 
 629     // Copy object. Use conjoint copying if we are relocating
 630     // in-place and the new object overlaps with the old object.
 631     if (_forwarding->in_place_relocation() && allocated_addr + size > from_addr) {
 632       ZUtils::object_copy_conjoint(from_addr, allocated_addr, size);
 633     } else {
 634       ZUtils::object_copy_disjoint(from_addr, allocated_addr, size);
 635     }
 636 
 637     // Insert forwarding
 638     const zaddress to_addr = forwarding_insert(_forwarding, from_addr, allocated_addr, &cursor);
 639     if (to_addr != allocated_addr) {
 640       // Already relocated, undo allocation
 641       _allocator->undo_alloc_object(to_page, to_addr, size);
 642       increase_other_forwarded(size);
 643     }
 644 
 645     return to_addr;
 646   }
 647 
 648   void update_remset_old_to_old(zaddress from_addr, zaddress to_addr) const {
 649     // Old-to-old relocation - move existing remset bits
 650 
 651     // If this is called for an in-place relocated page, then this code has the
 652     // responsibility to clear the old remset bits. Extra care is needed because:
 653     //
 654     // 1) The to-object copy can overlap with the from-object copy
 655     // 2) Remset bits of old objects need to be cleared
 656     //
 657     // A watermark is used to keep track of how far the old remset bits have been removed.
 658 
 659     const bool in_place = _forwarding->in_place_relocation();
 660     ZPage* const from_page = _forwarding->page();
 661     const uintptr_t from_local_offset = from_page->local_offset(from_addr);
 662 
 663     // Note: even with in-place relocation, the to_page could be another page
 664     ZPage* const to_page = ZHeap::heap()->page(to_addr);
 665 
 666     // Uses _relaxed version to handle that in-place relocation resets _top
 667     assert(ZHeap::heap()->is_in_page_relaxed(from_page, from_addr), "Must be");
 668     assert(to_page->is_in(to_addr), "Must be");
 669 
 670 
 671     // Read the size from the to-object, since the from-object
 672     // could have been overwritten during in-place relocation.
 673     const size_t size = ZUtils::object_size(to_addr);
 674 
 675     // If a young generation collection started while the old generation
 676     // relocated  objects, the remember set bits were flipped from "current"
 677     // to "previous".
 678     //
 679     // We need to select the correct remembered sets bitmap to ensure that the
 680     // old remset bits are found.
 681     //
 682     // Note that if the young generation marking (remset scanning) finishes
 683     // before the old generation relocation has relocated this page, then the
 684     // young generation will visit this page's previous remembered set bits and
 685     // moved them over to the current bitmap.
 686     //
 687     // If the young generation runs multiple cycles while the old generation is
 688     // relocating, then the first cycle will have consume the the old remset,
 689     // bits and moved associated objects to a new old page. The old relocation
 690     // could find either the the two bitmaps. So, either it will find the original
 691     // remset bits for the page, or it will find an empty bitmap for the page. It
 692     // doesn't matter for correctness, because the young generation marking has
 693     // already taken care of the bits.
 694 
 695     const bool active_remset_is_current = ZGeneration::old()->active_remset_is_current();
 696 
 697     // When in-place relocation is done and the old remset bits are located in
 698     // the bitmap that is going to be used for the new remset bits, then we
 699     // need to clear the old bits before the new bits are inserted.
 700     const bool iterate_current_remset = active_remset_is_current && !in_place;
 701 
 702     BitMap::Iterator iter = iterate_current_remset
 703         ? from_page->remset_iterator_limited_current(from_local_offset, size)
 704         : from_page->remset_iterator_limited_previous(from_local_offset, size);
 705 
 706     for (BitMap::idx_t field_bit : iter) {
 707       const uintptr_t field_local_offset = ZRememberedSet::to_offset(field_bit);
 708 
 709       // Add remset entry in the to-page
 710       const uintptr_t offset = field_local_offset - from_local_offset;
 711       const zaddress to_field = to_addr + offset;
 712       log_trace(gc, reloc)("Remember: from: " PTR_FORMAT " to: " PTR_FORMAT " current: %d marking: %d page: " PTR_FORMAT " remset: " PTR_FORMAT,
 713           untype(from_page->start() + field_local_offset), untype(to_field), active_remset_is_current, ZGeneration::young()->is_phase_mark(), p2i(to_page), p2i(to_page->remset_current()));
 714 
 715       volatile zpointer* const p = (volatile zpointer*)to_field;
 716 
 717       if (ZGeneration::young()->is_phase_mark()) {
 718         // Young generation remembered set scanning needs to know about this
 719         // field. It will take responsibility to add a new remember set entry if needed.
 720         _forwarding->relocated_remembered_fields_register(p);
 721       } else {
 722         to_page->remember(p);
 723         if (in_place) {
 724           assert(to_page->is_remembered(p), "p: " PTR_FORMAT, p2i(p));
 725         }
 726       }
 727     }
 728   }
 729 
 730   static bool add_remset_if_young(volatile zpointer* p, zaddress addr) {
 731     if (ZHeap::heap()->is_young(addr)) {
 732       ZRelocate::add_remset(p);
 733       return true;
 734     }
 735 
 736     return false;
 737   }
 738 
 739   static void update_remset_promoted_filter_and_remap_per_field(volatile zpointer* p) {
 740     const zpointer ptr = Atomic::load(p);
 741 
 742     assert(ZPointer::is_old_load_good(ptr), "Should be at least old load good: " PTR_FORMAT, untype(ptr));
 743 
 744     if (ZPointer::is_store_good(ptr)) {
 745       // Already has a remset entry
 746       return;
 747     }
 748 
 749     if (ZPointer::is_load_good(ptr)) {
 750       if (!is_null_any(ptr)) {
 751         const zaddress addr = ZPointer::uncolor(ptr);
 752         add_remset_if_young(p, addr);
 753       }
 754       // No need to remap it is already load good
 755       return;
 756     }
 757 
 758     if (is_null_any(ptr)) {
 759       // Eagerly remap to skip adding a remset entry just to get deferred remapping
 760       ZBarrier::remap_young_relocated(p, ptr);
 761       return;
 762     }
 763 
 764     const zaddress_unsafe addr_unsafe = ZPointer::uncolor_unsafe(ptr);
 765     ZForwarding* const forwarding = ZGeneration::young()->forwarding(addr_unsafe);
 766 
 767     if (forwarding == nullptr) {
 768       // Object isn't being relocated
 769       const zaddress addr = safe(addr_unsafe);
 770       if (!add_remset_if_young(p, addr)) {
 771         // Not young - eagerly remap to skip adding a remset entry just to get deferred remapping
 772         ZBarrier::remap_young_relocated(p, ptr);
 773       }
 774       return;
 775     }
 776 
 777     const zaddress addr = forwarding->find(addr_unsafe);
 778 
 779     if (!is_null(addr)) {
 780       // Object has already been relocated
 781       if (!add_remset_if_young(p, addr)) {
 782         // Not young - eagerly remap to skip adding a remset entry just to get deferred remapping
 783         ZBarrier::remap_young_relocated(p, ptr);
 784       }
 785       return;
 786     }
 787 
 788     // Object has not been relocated yet
 789     // Don't want to eagerly relocate objects, so just add a remset
 790     ZRelocate::add_remset(p);
 791     return;
 792   }
 793 
 794   void update_remset_promoted(zaddress to_addr) const {
 795     ZIterator::basic_oop_iterate(to_oop(to_addr), update_remset_promoted_filter_and_remap_per_field);
 796   }
 797 
 798   void update_remset_for_fields(zaddress from_addr, zaddress to_addr) const {
 799     if (_forwarding->to_age() != ZPageAge::old) {
 800       // No remembered set in young pages
 801       return;
 802     }
 803 
 804     // Need to deal with remset when moving objects to the old generation
 805     if (_forwarding->from_age() == ZPageAge::old) {
 806       update_remset_old_to_old(from_addr, to_addr);
 807       return;
 808     }
 809 
 810     // Normal promotion
 811     update_remset_promoted(to_addr);
 812   }
 813 
 814   bool try_relocate_object(zaddress from_addr) {
 815     const zaddress to_addr = try_relocate_object_inner(from_addr);
 816 
 817     if (is_null(to_addr)) {
 818       return false;
 819     }
 820 
 821     update_remset_for_fields(from_addr, to_addr);
 822 
 823     return true;
 824   }
 825 
 826   void start_in_place_relocation_prepare_remset(ZPage* from_page) {
 827     if (_forwarding->from_age() != ZPageAge::old) {
 828       // Only old pages have use remset bits
 829       return;
 830     }
 831 
 832     if (ZGeneration::old()->active_remset_is_current()) {
 833       // We want to iterate over and clear the remset bits of the from-space page,
 834       // and insert current bits in the to-space page. However, with in-place
 835       // relocation, the from-space and to-space pages are the same. Clearing
 836       // is destructive, and is difficult to perform before or during the iteration.
 837       // However, clearing of the current bits has to be done before exposing the
 838       // to-space objects in the forwarding table.
 839       //
 840       // To solve this tricky dependency problem, we start by stashing away the
 841       // current bits in the previous bits, and clearing the current bits
 842       // (implemented by swapping the bits). This way, the current bits are
 843       // cleared before copying the objects (like a normal to-space page),
 844       // and the previous bits are representing a copy of the current bits
 845       // of the from-space page, and are used for iteration.
 846       from_page->swap_remset_bitmaps();
 847     }
 848   }
 849 
 850   ZPage* start_in_place_relocation(zoffset relocated_watermark) {
 851     _forwarding->in_place_relocation_claim_page();
 852     _forwarding->in_place_relocation_start(relocated_watermark);
 853 
 854     ZPage* const from_page = _forwarding->page();
 855 
 856     const ZPageAge to_age = _forwarding->to_age();
 857     const bool promotion = _forwarding->is_promotion();
 858 
 859     // Promotions happen through a new cloned page
 860     ZPage* const to_page = promotion ? from_page->clone_limited() : from_page;
 861     to_page->reset(to_age, ZPageResetType::InPlaceRelocation);
 862 
 863     // Clear remset bits for all objects that were relocated
 864     // before this page became an in-place relocated page.
 865     start_in_place_relocation_prepare_remset(from_page);
 866 
 867     if (promotion) {
 868       // Register the the promotion
 869       ZGeneration::young()->in_place_relocate_promote(from_page, to_page);
 870       ZGeneration::young()->register_in_place_relocate_promoted(from_page);
 871     }
 872 
 873     return to_page;
 874   }
 875 
 876   void relocate_object(oop obj) {
 877     const zaddress addr = to_zaddress(obj);
 878     assert(ZHeap::heap()->is_object_live(addr), "Should be live");
 879 
 880     while (!try_relocate_object(addr)) {
 881       // Allocate a new target page, or if that fails, use the page being
 882       // relocated as the new target, which will cause it to be relocated
 883       // in-place.
 884       const ZPageAge to_age = _forwarding->to_age();
 885       ZPage* to_page = _allocator->alloc_and_retire_target_page(_forwarding, target(to_age));
 886       set_target(to_age, to_page);
 887       if (to_page != nullptr) {
 888         continue;
 889       }
 890 
 891       // Start in-place relocation to block other threads from accessing
 892       // the page, or its forwarding table, until it has been released
 893       // (relocation completed).
 894       to_page = start_in_place_relocation(ZAddress::offset(addr));
 895       set_target(to_age, to_page);
 896     }
 897   }
 898 
 899 public:
 900   ZRelocateWork(Allocator* allocator, ZGeneration* generation)
 901     : _allocator(allocator),
 902       _forwarding(nullptr),
 903       _target(),
 904       _generation(generation),
 905       _other_promoted(0),
 906       _other_compacted(0) {}
 907 
 908   ~ZRelocateWork() {
 909     for (uint i = 0; i < ZAllocator::_relocation_allocators; ++i) {
 910       _allocator->free_target_page(_target[i]);
 911     }
 912     // Report statistics on-behalf of non-worker threads
 913     _generation->increase_promoted(_other_promoted);
 914     _generation->increase_compacted(_other_compacted);
 915   }
 916 
 917   bool active_remset_is_current() const {
 918     // Normal old-to-old relocation can treat the from-page remset as a
 919     // read-only copy, and then copy over the appropriate remset bits to the
 920     // cleared to-page's 'current' remset bitmap.
 921     //
 922     // In-place relocation is more complicated. Since, the same page is both
 923     // a from-page and a to-page, we need to remove the old remset bits, and
 924     // add remset bits that corresponds to the new locations of the relocated
 925     // objects.
 926     //
 927     // Depending on how long ago (in terms of number of young GC's and the
 928     // current young GC's phase), the page was allocated, the active
 929     // remembered set will be in either the 'current' or 'previous' bitmap.
 930     //
 931     // If the active bits are in the 'previous' bitmap, we know that the
 932     // 'current' bitmap was cleared at some earlier point in time, and we can
 933     // simply set new bits in 'current' bitmap, and later when relocation has
 934     // read all the old remset bits, we could just clear the 'previous' remset
 935     // bitmap.
 936     //
 937     // If, on the other hand, the active bits are in the 'current' bitmap, then
 938     // that bitmap will be used to both read the old remset bits, and the
 939     // destination for the remset bits that we copy when an object is copied
 940     // to it's new location within the page. We need to *carefully* remove all
 941     // all old remset bits, without clearing out the newly set bits.
 942     return ZGeneration::old()->active_remset_is_current();
 943   }
 944 
 945   void clear_remset_before_reuse(ZPage* page, bool in_place) {
 946     if (_forwarding->from_age() != ZPageAge::old) {
 947       // No remset bits
 948       return;
 949     }
 950 
 951     if (in_place) {
 952       // Clear 'previous' remset bits. For in-place relocated pages, the previous
 953       // remset bits are always used, even when active_remset_is_current().
 954       page->clear_remset_previous();
 955 
 956       return;
 957     }
 958 
 959     // Normal relocate
 960 
 961     // Clear active remset bits
 962     if (active_remset_is_current()) {
 963       page->clear_remset_current();
 964     } else {
 965       page->clear_remset_previous();
 966     }
 967 
 968     // Verify that inactive remset bits are all cleared
 969     if (active_remset_is_current()) {
 970       page->verify_remset_cleared_previous();
 971     } else {
 972       page->verify_remset_cleared_current();
 973     }
 974   }
 975 
 976   void finish_in_place_relocation() {
 977     // We are done with the from_space copy of the page
 978     _forwarding->in_place_relocation_finish();
 979   }
 980 
 981   void do_forwarding(ZForwarding* forwarding) {
 982     _forwarding = forwarding;
 983 
 984     _forwarding->page()->log_msg(" (relocate page)");
 985 
 986     ZVerify::before_relocation(_forwarding);
 987 
 988     // Relocate objects
 989     _forwarding->object_iterate([&](oop obj) { relocate_object(obj); });
 990 
 991     ZVerify::after_relocation(_forwarding);
 992 
 993     // Verify
 994     if (ZVerifyForwarding) {
 995       _forwarding->verify();
 996     }
 997 
 998     _generation->increase_freed(_forwarding->page()->size());
 999 
1000     // Deal with in-place relocation
1001     const bool in_place = _forwarding->in_place_relocation();
1002     if (in_place) {
1003       finish_in_place_relocation();
1004     }
1005 
1006     // Old from-space pages need to deal with remset bits
1007     if (_forwarding->from_age() == ZPageAge::old) {
1008       _forwarding->relocated_remembered_fields_after_relocate();
1009     }
1010 
1011     // Release relocated page
1012     _forwarding->release_page();
1013 
1014     if (in_place) {
1015       // Wait for all other threads to call release_page
1016       ZPage* const page = _forwarding->detach_page();
1017 
1018       // Ensure that previous remset bits are cleared
1019       clear_remset_before_reuse(page, true /* in_place */);
1020 
1021       page->log_msg(" (relocate page done in-place)");
1022 
1023       // Different pages when promoting
1024       ZPage* const target_page = target(_forwarding->to_age());
1025       _allocator->share_target_page(target_page);
1026 
1027     } else {
1028       // Wait for all other threads to call release_page
1029       ZPage* const page = _forwarding->detach_page();
1030 
1031       // Ensure that all remset bits are cleared
1032       // Note: cleared after detach_page, when we know that
1033       // the young generation isn't scanning the remset.
1034       clear_remset_before_reuse(page, false /* in_place */);
1035 
1036       page->log_msg(" (relocate page done normal)");
1037 
1038       // Free page
1039       ZHeap::heap()->free_page(page);
1040     }
1041   }
1042 };
1043 
1044 class ZRelocateStoreBufferInstallBasePointersThreadClosure : public ThreadClosure {
1045 public:
1046   virtual void do_thread(Thread* thread) {
1047     JavaThread* const jt = JavaThread::cast(thread);
1048     ZStoreBarrierBuffer* buffer = ZThreadLocalData::store_barrier_buffer(jt);
1049     buffer->install_base_pointers();
1050   }
1051 };
1052 
1053 // Installs the object base pointers (object starts), for the fields written
1054 // in the store buffer. The code that searches for the object start uses that
1055 // liveness information stored in the pages. That information is lost when the
1056 // pages have been relocated and then destroyed.
1057 class ZRelocateStoreBufferInstallBasePointersTask : public ZTask {
1058 private:
1059   ZJavaThreadsIterator _threads_iter;
1060 
1061 public:
1062   ZRelocateStoreBufferInstallBasePointersTask(ZGeneration* generation)
1063     : ZTask("ZRelocateStoreBufferInstallBasePointersTask"),
1064       _threads_iter(generation->id_optional()) {}
1065 
1066   virtual void work() {
1067     ZRelocateStoreBufferInstallBasePointersThreadClosure fix_store_buffer_cl;
1068     _threads_iter.apply(&fix_store_buffer_cl);
1069   }
1070 };
1071 
1072 class ZRelocateTask : public ZRestartableTask {
1073 private:
1074   ZRelocationSetParallelIterator _iter;
1075   ZGeneration* const             _generation;
1076   ZRelocateQueue* const          _queue;
1077   ZRelocateSmallAllocator        _small_allocator;
1078   ZRelocateMediumAllocator       _medium_allocator;
1079 
1080 public:
1081   ZRelocateTask(ZRelocationSet* relocation_set, ZRelocateQueue* queue)
1082     : ZRestartableTask("ZRelocateTask"),
1083       _iter(relocation_set),
1084       _generation(relocation_set->generation()),
1085       _queue(queue),
1086       _small_allocator(_generation),
1087       _medium_allocator(_generation) {}
1088 
1089   ~ZRelocateTask() {
1090     _generation->stat_relocation()->at_relocate_end(_small_allocator.in_place_count(), _medium_allocator.in_place_count());
1091   }
1092 
1093   virtual void work() {
1094     ZRelocateWork<ZRelocateSmallAllocator> small(&_small_allocator, _generation);
1095     ZRelocateWork<ZRelocateMediumAllocator> medium(&_medium_allocator, _generation);
1096 
1097     const auto do_forwarding = [&](ZForwarding* forwarding) {
1098       ZPage* const page = forwarding->page();
1099       if (page->is_small()) {
1100         small.do_forwarding(forwarding);
1101       } else {
1102         medium.do_forwarding(forwarding);
1103       }
1104 
1105       // Absolute last thing done while relocating a page.
1106       //
1107       // We don't use the SuspendibleThreadSet when relocating pages.
1108       // Instead the ZRelocateQueue is used as a pseudo STS joiner/leaver.
1109       //
1110       // After the mark_done call a safepointing could be completed and a
1111       // new GC phase could be entered.
1112       forwarding->mark_done();
1113     };
1114 
1115     const auto claim_and_do_forwarding = [&](ZForwarding* forwarding) {
1116       if (forwarding->claim()) {
1117         do_forwarding(forwarding);
1118       }
1119     };
1120 
1121     const auto do_forwarding_one_from_iter = [&]() {
1122       ZForwarding* forwarding;
1123 
1124       if (_iter.next(&forwarding)) {
1125         claim_and_do_forwarding(forwarding);
1126         return true;
1127       }
1128 
1129       return false;
1130     };
1131 
1132     for (;;) {
1133       // As long as there are requests in the relocate queue, there are threads
1134       // waiting in a VM state that does not allow them to be blocked. The
1135       // worker thread needs to finish relocate these pages, and allow the
1136       // other threads to continue and proceed to a blocking state. After that,
1137       // the worker threads are allowed to safepoint synchronize.
1138       for (ZForwarding* forwarding; (forwarding = _queue->synchronize_poll()) != nullptr;) {
1139         do_forwarding(forwarding);
1140       }
1141 
1142       if (!do_forwarding_one_from_iter()) {
1143         // No more work
1144         break;
1145       }
1146 
1147       if (_generation->should_worker_resize()) {
1148         break;
1149       }
1150     }
1151 
1152     _queue->leave();
1153   }
1154 
1155   virtual void resize_workers(uint nworkers) {
1156     _queue->resize_workers(nworkers);
1157   }
1158 };
1159 
1160 static void remap_and_maybe_add_remset(volatile zpointer* p) {
1161   const zpointer ptr = Atomic::load(p);
1162 
1163   if (ZPointer::is_store_good(ptr)) {
1164     // Already has a remset entry
1165     return;
1166   }
1167 
1168   // Remset entries are used for two reasons:
1169   // 1) Young marking old-to-young pointer roots
1170   // 2) Deferred remapping of stale old-to-young pointers
1171   //
1172   // This load barrier will up-front perform the remapping of (2),
1173   // and the code below only has to make sure we register up-to-date
1174   // old-to-young pointers for (1).
1175   const zaddress addr = ZBarrier::load_barrier_on_oop_field_preloaded(p, ptr);
1176 
1177   if (is_null(addr)) {
1178     // No need for remset entries for null pointers
1179     return;
1180   }
1181 
1182   if (ZHeap::heap()->is_old(addr)) {
1183     // No need for remset entries for pointers to old gen
1184     return;
1185   }
1186 
1187   ZRelocate::add_remset(p);
1188 }
1189 
1190 class ZRelocateAddRemsetForFlipPromoted : public ZRestartableTask {
1191 private:
1192   ZStatTimerYoung                _timer;
1193   ZArrayParallelIterator<ZPage*> _iter;
1194 
1195 public:
1196   ZRelocateAddRemsetForFlipPromoted(ZArray<ZPage*>* pages)
1197     : ZRestartableTask("ZRelocateAddRemsetForFlipPromoted"),
1198       _timer(ZSubPhaseConcurrentRelocateRememberedSetFlipPromotedYoung),
1199       _iter(pages) {}
1200 
1201   virtual void work() {
1202     SuspendibleThreadSetJoiner sts_joiner;
1203 
1204     for (ZPage* page; _iter.next(&page);) {
1205       page->object_iterate([&](oop obj) {
1206         ZIterator::basic_oop_iterate_safe(obj, remap_and_maybe_add_remset);
1207       });
1208 
1209       SuspendibleThreadSet::yield();
1210       if (ZGeneration::young()->should_worker_resize()) {
1211         return;
1212       }
1213     }
1214   }
1215 };
1216 
1217 void ZRelocate::relocate(ZRelocationSet* relocation_set) {
1218   {
1219     // Install the store buffer's base pointers before the
1220     // relocate task destroys the liveness information in
1221     // the relocated pages.
1222     ZRelocateStoreBufferInstallBasePointersTask buffer_task(_generation);
1223     workers()->run(&buffer_task);
1224   }
1225 
1226   {
1227     ZRelocateTask relocate_task(relocation_set, &_queue);
1228     workers()->run(&relocate_task);
1229   }
1230 
1231   if (relocation_set->generation()->is_young()) {
1232     ZRelocateAddRemsetForFlipPromoted task(relocation_set->flip_promoted_pages());
1233     workers()->run(&task);
1234   }
1235 
1236   _queue.clear();
1237 }
1238 
1239 ZPageAge ZRelocate::compute_to_age(ZPageAge from_age) {
1240   if (from_age == ZPageAge::old) {
1241     return ZPageAge::old;
1242   }
1243 
1244   const uint age = static_cast<uint>(from_age);
1245   if (age >= ZGeneration::young()->tenuring_threshold()) {
1246     return ZPageAge::old;
1247   }
1248 
1249   return static_cast<ZPageAge>(age + 1);
1250 }
1251 
1252 class ZFlipAgePagesTask : public ZTask {
1253 private:
1254   ZArrayParallelIterator<ZPage*> _iter;
1255 
1256 public:
1257   ZFlipAgePagesTask(const ZArray<ZPage*>* pages)
1258     : ZTask("ZPromotePagesTask"),
1259       _iter(pages) {}
1260 
1261   virtual void work() {
1262     SuspendibleThreadSetJoiner sts_joiner;
1263     ZArray<ZPage*> promoted_pages;
1264 
1265     for (ZPage* prev_page; _iter.next(&prev_page);) {
1266       const ZPageAge from_age = prev_page->age();
1267       const ZPageAge to_age = ZRelocate::compute_to_age(from_age);
1268       assert(from_age != ZPageAge::old, "invalid age for a young collection");
1269 
1270       // Figure out if this is proper promotion
1271       const bool promotion = to_age == ZPageAge::old;
1272 
1273       if (promotion) {
1274         // Before promoting an object (and before relocate start), we must ensure that all
1275         // contained zpointers are store good. The marking code ensures that for non-null
1276         // pointers, but null pointers are ignored. This code ensures that even null pointers
1277         // are made store good, for the promoted objects.
1278         prev_page->object_iterate([&](oop obj) {
1279           ZIterator::basic_oop_iterate_safe(obj, ZBarrier::promote_barrier_on_young_oop_field);
1280         });
1281       }
1282 
1283       // Logging
1284       prev_page->log_msg(promotion ? " (flip promoted)" : " (flip survived)");
1285 
1286       // Setup to-space page
1287       ZPage* const new_page = promotion ? prev_page->clone_limited_promote_flipped() : prev_page;
1288       new_page->reset(to_age, ZPageResetType::FlipAging);
1289 
1290       if (promotion) {
1291         ZGeneration::young()->flip_promote(prev_page, new_page);
1292         // Defer promoted page registration times the lock is taken
1293         promoted_pages.push(prev_page);
1294       }
1295 
1296       SuspendibleThreadSet::yield();
1297     }
1298 
1299     ZGeneration::young()->register_flip_promoted(promoted_pages);
1300   }
1301 };
1302 
1303 void ZRelocate::flip_age_pages(const ZArray<ZPage*>* pages) {
1304   ZFlipAgePagesTask flip_age_task(pages);
1305   workers()->run(&flip_age_task);
1306 }
1307 
1308 void ZRelocate::synchronize() {
1309   _queue.synchronize();
1310 }
1311 
1312 void ZRelocate::desynchronize() {
1313   _queue.desynchronize();
1314 }
1315 
1316 ZRelocateQueue* ZRelocate::queue() {
1317   return &_queue;
1318 }