1 /*
   2  * Copyright (c) 2013, 2017, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "classfile/stringTable.hpp"
  26 #include "gc/shared/gcTimer.hpp"
  27 #include "gc/shared/parallelCleaning.hpp"
  28 #include "gc/shared/referenceProcessor.hpp"
  29 #include "gc/shared/strongRootsScope.hpp"
  30 #include "gc/shared/suspendibleThreadSet.hpp"
  31 #include "gc/shenandoah/brooksPointer.hpp"
  32 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
  33 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  34 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
  35 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  36 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
  37 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  38 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
  39 #include "gc/shenandoah/shenandoah_specialized_oop_closures.hpp"
  40 #include "gc/shenandoah/shenandoahTaskqueue.hpp"
  41 #include "gc/shenandoah/shenandoahUtils.hpp"
  42 #include "gc/shared/weakProcessor.hpp"
  43 #include "code/codeCache.hpp"
  44 #include "classfile/symbolTable.hpp"
  45 #include "classfile/systemDictionary.hpp"
  46 #include "memory/iterator.inline.hpp"
  47 #include "oops/oop.inline.hpp"
  48 #include "gc/shared/taskqueue.inline.hpp"
  49 #include "logging/logStream.hpp"
  50 
  51 template<UpdateRefsMode UPDATE_REFS>
  52 class ShenandoahInitMarkRootsClosure : public OopClosure {
  53 private:
  54   ShenandoahObjToScanQueue* _queue;
  55   ShenandoahHeap* _heap;
  56 
  57   template <class T>
  58   inline void do_oop_nv(T* p) {
  59     ShenandoahConcurrentMark::mark_through_ref<T, UPDATE_REFS, false /* string dedup */>(p, _heap, _queue);
  60   }
  61 
  62 public:
  63   ShenandoahInitMarkRootsClosure(ShenandoahObjToScanQueue* q) :
  64     _queue(q), _heap(ShenandoahHeap::heap()) {};
  65 
  66   void do_oop(narrowOop* p) { do_oop_nv(p); }
  67   void do_oop(oop* p)       { do_oop_nv(p); }
  68 };
  69 
  70 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ReferenceProcessor* rp) :
  71   MetadataAwareOopClosure(rp),
  72   _queue(q),
  73   _dedup_queue(NULL),
  74   _heap(ShenandoahHeap::heap())
  75 { }
  76 
  77 
  78 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahStrDedupQueue* dq, ReferenceProcessor* rp) :
  79   MetadataAwareOopClosure(rp),
  80   _queue(q),
  81   _dedup_queue(dq),
  82   _heap(ShenandoahHeap::heap())
  83 { }
  84 
  85 
  86 template<UpdateRefsMode UPDATE_REFS>
  87 class ShenandoahInitMarkRootsTask : public AbstractGangTask {
  88 private:
  89   ShenandoahRootProcessor* _rp;
  90   bool _process_refs;
  91 public:
  92   ShenandoahInitMarkRootsTask(ShenandoahRootProcessor* rp, bool process_refs) :
  93     AbstractGangTask("Shenandoah init mark roots task"),
  94     _rp(rp),
  95     _process_refs(process_refs) {
  96   }
  97 
  98   void work(uint worker_id) {
  99     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 100 
 101     ShenandoahHeap* heap = ShenandoahHeap::heap();
 102     ShenandoahObjToScanQueueSet* queues = heap->concurrentMark()->task_queues();
 103     assert(queues->get_reserved() > worker_id, "Queue has not been reserved for worker id: %d", worker_id);
 104 
 105     ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 106     ShenandoahInitMarkRootsClosure<UPDATE_REFS> mark_cl(q);
 107     CLDToOopClosure cldCl(&mark_cl);
 108     MarkingCodeBlobClosure blobsCl(&mark_cl, ! CodeBlobToOopClosure::FixRelocations);
 109 
 110     // The rationale for selecting the roots to scan is as follows:
 111     //   a. With unload_classes = true, we only want to scan the actual strong roots from the
 112     //      code cache. This will allow us to identify the dead classes, unload them, *and*
 113     //      invalidate the relevant code cache blobs. This could be only done together with
 114     //      class unloading.
 115     //   b. With unload_classes = false, we have to nominally retain all the references from code
 116     //      cache, because there could be the case of embedded class/oop in the generated code,
 117     //      which we will never visit during mark. Without code cache invalidation, as in (a),
 118     //      we risk executing that code cache blob, and crashing.
 119     //   c. With ShenandoahConcurrentScanCodeRoots, we avoid scanning the entire code cache here,
 120     //      and instead do that in concurrent phase under the relevant lock. This saves init mark
 121     //      pause time.
 122 
 123     ResourceMark m;
 124     if (heap->concurrentMark()->unload_classes()) {
 125       _rp->process_strong_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, NULL, &blobsCl, NULL, worker_id);
 126     } else {
 127       if (ShenandoahConcurrentScanCodeRoots) {
 128         CodeBlobClosure* code_blobs = NULL;
 129 #ifdef ASSERT
 130         ShenandoahAssertToSpaceClosure assert_to_space_oops;
 131         CodeBlobToOopClosure assert_to_space(&assert_to_space_oops, !CodeBlobToOopClosure::FixRelocations);
 132         // If conc code cache evac is disabled, code cache should have only to-space ptrs.
 133         // Otherwise, it should have to-space ptrs only if mark does not update refs.
 134         if (!ShenandoahConcurrentEvacCodeRoots && !heap->has_forwarded_objects()) {
 135           code_blobs = &assert_to_space;
 136         }
 137 #endif
 138         _rp->process_all_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, code_blobs, NULL, worker_id);
 139       } else {
 140         _rp->process_all_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, &blobsCl, NULL, worker_id);
 141       }
 142     }
 143   }
 144 };
 145 
 146 class ShenandoahUpdateRootsTask : public AbstractGangTask {
 147 private:
 148   ShenandoahRootProcessor* _rp;
 149   const bool _update_code_cache;
 150 public:
 151   ShenandoahUpdateRootsTask(ShenandoahRootProcessor* rp, bool update_code_cache) :
 152     AbstractGangTask("Shenandoah update roots task"),
 153     _rp(rp),
 154     _update_code_cache(update_code_cache) {
 155   }
 156 
 157   void work(uint worker_id) {
 158     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 159 
 160     ShenandoahHeap* heap = ShenandoahHeap::heap();
 161     ShenandoahUpdateRefsClosure cl;
 162     CLDToOopClosure cldCl(&cl);
 163 
 164     CodeBlobClosure* code_blobs;
 165     CodeBlobToOopClosure update_blobs(&cl, CodeBlobToOopClosure::FixRelocations);
 166 #ifdef ASSERT
 167     ShenandoahAssertToSpaceClosure assert_to_space_oops;
 168     CodeBlobToOopClosure assert_to_space(&assert_to_space_oops, !CodeBlobToOopClosure::FixRelocations);
 169 #endif
 170     if (_update_code_cache) {
 171       code_blobs = &update_blobs;
 172     } else {
 173       code_blobs =
 174         DEBUG_ONLY(&assert_to_space)
 175         NOT_DEBUG(NULL);
 176     }
 177     _rp->process_all_roots(&cl, &cl, &cldCl, code_blobs, NULL, worker_id);
 178   }
 179 };
 180 
 181 class ShenandoahConcurrentMarkingTask : public AbstractGangTask {
 182 private:
 183   ShenandoahConcurrentMark* _cm;
 184   ParallelTaskTerminator* _terminator;
 185   bool _update_refs;
 186 
 187 public:
 188   ShenandoahConcurrentMarkingTask(ShenandoahConcurrentMark* cm, ParallelTaskTerminator* terminator, bool update_refs) :
 189     AbstractGangTask("Root Region Scan"), _cm(cm), _terminator(terminator), _update_refs(update_refs) {
 190   }
 191 
 192 
 193   void work(uint worker_id) {
 194     SuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 195     ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id);
 196     jushort* live_data = _cm->get_liveness(worker_id);
 197     ReferenceProcessor* rp;
 198     if (_cm->process_references()) {
 199       rp = ShenandoahHeap::heap()->ref_processor();
 200       shenandoah_assert_rp_isalive_installed();
 201     } else {
 202       rp = NULL;
 203     }
 204 
 205     _cm->concurrent_scan_code_roots(worker_id, rp, _update_refs);
 206     _cm->mark_loop(worker_id, _terminator, rp,
 207                    true, // cancellable
 208                    _cm->unload_classes(),
 209                    _update_refs,
 210                    ShenandoahStringDedup::is_enabled()); // perform string dedup
 211   }
 212 };
 213 
 214 class ShenandoahFinalMarkingTask : public AbstractGangTask {
 215 private:
 216   ShenandoahConcurrentMark* _cm;
 217   ParallelTaskTerminator* _terminator;
 218   bool _update_refs;
 219   bool _unload_classes;
 220   bool _dedup_string;
 221 
 222 public:
 223   ShenandoahFinalMarkingTask(ShenandoahConcurrentMark* cm, ParallelTaskTerminator* terminator,
 224                              bool update_refs, bool unload_classes, bool dedup_string) :
 225     AbstractGangTask("Shenandoah Final Marking"), _cm(cm), _terminator(terminator),
 226     _update_refs(update_refs), _unload_classes(unload_classes), _dedup_string(dedup_string) {
 227   }
 228 
 229   void work(uint worker_id) {
 230     // First drain remaining SATB buffers.
 231     // Notice that this is not strictly necessary for mark-compact. But since
 232     // it requires a StrongRootsScope around the task, we need to claim the
 233     // threads, and performance-wise it doesn't really matter. Adds about 1ms to
 234     // full-gc.
 235     ShenandoahObjToScanQueue* q = get_queue(worker_id);
 236     ShenandoahSATBBufferClosure cl(q);
 237     SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 238     while (satb_mq_set.apply_closure_to_completed_buffer(&cl));
 239     ShenandoahSATBThreadsClosure tc(&cl);
 240     Threads::threads_do(&tc);
 241 
 242     ReferenceProcessor* rp;
 243     if (_cm->process_references()) {
 244       rp = ShenandoahHeap::heap()->ref_processor();
 245       shenandoah_assert_rp_isalive_installed();
 246     } else {
 247       rp = NULL;
 248     }
 249 
 250     // Degenerated cycle may bypass concurrent cycle, so code roots might not be scanned,
 251     // let's check here.
 252     _cm->concurrent_scan_code_roots(worker_id, rp, _update_refs);
 253     _cm->mark_loop(worker_id, _terminator, rp,
 254                    false, // not cancellable
 255                    _unload_classes,
 256                    _update_refs,
 257                    _dedup_string);
 258 
 259     assert(_cm->task_queues()->is_empty(), "Should be empty");
 260   }
 261 };
 262 
 263 void ShenandoahConcurrentMark::mark_roots(ShenandoahPhaseTimings::Phase root_phase) {
 264   assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
 265   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 266 
 267   ShenandoahHeap* heap = ShenandoahHeap::heap();
 268 
 269   ShenandoahGCPhase phase(root_phase);
 270 
 271   WorkGang* workers = heap->workers();
 272   uint nworkers = workers->active_workers();
 273 
 274   assert(nworkers <= task_queues()->size(), "Just check");
 275 
 276   ShenandoahRootProcessor root_proc(heap, nworkers, root_phase);
 277   TASKQUEUE_STATS_ONLY(reset_taskqueue_stats());
 278   task_queues()->reserve(nworkers);
 279 
 280   if (heap->has_forwarded_objects()) {
 281     ShenandoahInitMarkRootsTask<RESOLVE> mark_roots(&root_proc, process_references());
 282     workers->run_task(&mark_roots);
 283   } else {
 284     // No need to update references, which means the heap is stable.
 285     // Can save time not walking through forwarding pointers.
 286     ShenandoahInitMarkRootsTask<NONE> mark_roots(&root_proc, process_references());
 287     workers->run_task(&mark_roots);
 288   }
 289 
 290   if (ShenandoahConcurrentScanCodeRoots) {
 291     clear_claim_codecache();
 292   }
 293 }
 294 
 295 void ShenandoahConcurrentMark::init_mark_roots() {
 296   assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
 297   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 298 
 299   mark_roots(ShenandoahPhaseTimings::scan_roots);
 300 }
 301 
 302 void ShenandoahConcurrentMark::update_roots(ShenandoahPhaseTimings::Phase root_phase) {
 303   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 304 
 305   bool update_code_cache = true; // initialize to safer value
 306   switch (root_phase) {
 307     case ShenandoahPhaseTimings::update_roots:
 308     case ShenandoahPhaseTimings::final_update_refs_roots:
 309       // If code cache was evacuated concurrently, we need to update code cache roots.
 310       update_code_cache = ShenandoahConcurrentEvacCodeRoots;
 311       break;
 312     case ShenandoahPhaseTimings::full_gc_roots:
 313       update_code_cache = true;
 314       break;
 315     default:
 316       ShouldNotReachHere();
 317   }
 318 
 319   ShenandoahHeap* heap = ShenandoahHeap::heap();
 320 
 321   ShenandoahGCPhase phase(root_phase);
 322 
 323 #if defined(COMPILER2) || INCLUDE_JVMCI
 324   DerivedPointerTable::clear();
 325 #endif
 326 
 327   uint nworkers = heap->workers()->active_workers();
 328 
 329   ShenandoahRootProcessor root_proc(heap, nworkers, root_phase);
 330   ShenandoahUpdateRootsTask update_roots(&root_proc, update_code_cache);
 331   heap->workers()->run_task(&update_roots);
 332 
 333 #if defined(COMPILER2) || INCLUDE_JVMCI
 334   DerivedPointerTable::update_pointers();
 335 #endif
 336 }
 337 
 338 void ShenandoahConcurrentMark::initialize(uint workers) {
 339   _heap = ShenandoahHeap::heap();
 340 
 341   uint num_queues = MAX2(workers, 1U);
 342 
 343   _task_queues = new ShenandoahObjToScanQueueSet((int) num_queues);
 344 
 345   for (uint i = 0; i < num_queues; ++i) {
 346     ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue();
 347     task_queue->initialize();
 348     _task_queues->register_queue(i, task_queue);
 349   }
 350 
 351   ShenandoahBarrierSet::satb_mark_queue_set().set_buffer_size(ShenandoahSATBBufferSize);
 352 
 353   size_t num_regions = ShenandoahHeap::heap()->num_regions();
 354   _liveness_local = NEW_C_HEAP_ARRAY(jushort*, workers, mtGC);
 355   for (uint worker = 0; worker < workers; worker++) {
 356      _liveness_local[worker] = NEW_C_HEAP_ARRAY(jushort, num_regions, mtGC);
 357   }
 358 }
 359 
 360 void ShenandoahConcurrentMark::concurrent_scan_code_roots(uint worker_id, ReferenceProcessor* rp, bool update_refs) {
 361   if (ShenandoahConcurrentScanCodeRoots && claim_codecache()) {
 362     ShenandoahObjToScanQueue* q = task_queues()->queue(worker_id);
 363     if (!unload_classes()) {
 364       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 365       if (update_refs) {
 366         ShenandoahMarkResolveRefsClosure cl(q, rp);
 367         CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations);
 368         CodeCache::blobs_do(&blobs);
 369       } else {
 370         ShenandoahMarkRefsClosure cl(q, rp);
 371         CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations);
 372         CodeCache::blobs_do(&blobs);
 373       }
 374     }
 375   }
 376 }
 377 
 378 void ShenandoahConcurrentMark::mark_from_roots() {
 379   ShenandoahHeap* sh = ShenandoahHeap::heap();
 380   WorkGang* workers = sh->workers();
 381   uint nworkers = workers->active_workers();
 382 
 383   bool update_refs = sh->has_forwarded_objects();
 384 
 385   ShenandoahGCPhase conc_mark_phase(ShenandoahPhaseTimings::conc_mark);
 386 
 387   if (process_references()) {
 388     ReferenceProcessor* rp = sh->ref_processor();
 389     rp->set_active_mt_degree(nworkers);
 390 
 391     // enable ("weak") refs discovery
 392     rp->enable_discovery(true /*verify_no_refs*/);
 393     rp->setup_policy(sh->is_full_gc_in_progress()); // snapshot the soft ref policy to be used in this cycle
 394   }
 395 
 396   shenandoah_assert_rp_isalive_not_installed();
 397   ReferenceProcessorIsAliveMutator fix_isalive(sh->ref_processor(), sh->is_alive_closure());
 398 
 399   task_queues()->reserve(nworkers);
 400 
 401   if (UseShenandoahOWST) {
 402     ShenandoahTaskTerminator terminator(nworkers, task_queues());
 403     ShenandoahConcurrentMarkingTask markingTask = ShenandoahConcurrentMarkingTask(this, &terminator, update_refs);
 404     workers->run_task(&markingTask);
 405   } else {
 406     ParallelTaskTerminator terminator(nworkers, task_queues());
 407     ShenandoahConcurrentMarkingTask markingTask = ShenandoahConcurrentMarkingTask(this, &terminator, update_refs);
 408     workers->run_task(&markingTask);
 409   }
 410 
 411   assert(task_queues()->is_empty() || sh->cancelled_gc(), "Should be empty when not cancelled");
 412 }
 413 
 414 void ShenandoahConcurrentMark::finish_mark_from_roots() {
 415   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 416 
 417   ShenandoahHeap* sh = ShenandoahHeap::heap();
 418 
 419   shared_finish_mark_from_roots(/* full_gc = */ false);
 420 
 421   if (sh->has_forwarded_objects()) {
 422     update_roots(ShenandoahPhaseTimings::update_roots);
 423   }
 424 
 425   TASKQUEUE_STATS_ONLY(print_taskqueue_stats());
 426   TASKQUEUE_STATS_ONLY(reset_taskqueue_stats());
 427 }
 428 
 429 void ShenandoahConcurrentMark::shared_finish_mark_from_roots(bool full_gc) {
 430   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 431 
 432   ShenandoahHeap* sh = ShenandoahHeap::heap();
 433 
 434   uint nworkers = sh->workers()->active_workers();
 435 
 436   // Finally mark everything else we've got in our queues during the previous steps.
 437   // It does two different things for concurrent vs. mark-compact GC:
 438   // - For concurrent GC, it starts with empty task queues, drains the remaining
 439   //   SATB buffers, and then completes the marking closure.
 440   // - For mark-compact GC, it starts out with the task queues seeded by initial
 441   //   root scan, and completes the closure, thus marking through all live objects
 442   // The implementation is the same, so it's shared here.
 443   {
 444     ShenandoahGCPhase phase(full_gc ?
 445                                ShenandoahPhaseTimings::full_gc_mark_finish_queues :
 446                                ShenandoahPhaseTimings::finish_queues);
 447     task_queues()->reserve(nworkers);
 448 
 449     shenandoah_assert_rp_isalive_not_installed();
 450     ReferenceProcessorIsAliveMutator fix_isalive(sh->ref_processor(), sh->is_alive_closure());
 451 
 452     StrongRootsScope scope(nworkers);
 453     if (UseShenandoahOWST) {
 454       ShenandoahTaskTerminator terminator(nworkers, task_queues());
 455       ShenandoahFinalMarkingTask task(this, &terminator, sh->has_forwarded_objects(),
 456         unload_classes(), full_gc && ShenandoahStringDedup::is_enabled());
 457       sh->workers()->run_task(&task);
 458     } else {
 459       ParallelTaskTerminator terminator(nworkers, task_queues());
 460       ShenandoahFinalMarkingTask task(this, &terminator, sh->has_forwarded_objects(),
 461         unload_classes(), full_gc && ShenandoahStringDedup::is_enabled());
 462       sh->workers()->run_task(&task);
 463     }
 464   }
 465 
 466   assert(task_queues()->is_empty(), "Should be empty");
 467 
 468   // When we're done marking everything, we process weak references.
 469   if (process_references()) {
 470     weak_refs_work(full_gc);
 471   }
 472 
 473   // And finally finish class unloading
 474   if (unload_classes()) {
 475     sh->unload_classes_and_cleanup_tables(full_gc);
 476   }
 477 
 478   assert(task_queues()->is_empty(), "Should be empty");
 479 
 480 }
 481 
 482 class ShenandoahSATBThreadsClosure : public ThreadClosure {
 483   ShenandoahSATBBufferClosure* _satb_cl;
 484   int _thread_parity;
 485 
 486  public:
 487   ShenandoahSATBThreadsClosure(ShenandoahSATBBufferClosure* satb_cl) :
 488     _satb_cl(satb_cl),
 489     _thread_parity(Threads::thread_claim_parity()) {}
 490 
 491   void do_thread(Thread* thread) {
 492     if (thread->is_Java_thread()) {
 493       if (thread->claim_oops_do(true, _thread_parity)) {
 494         JavaThread* jt = (JavaThread*)thread;
 495         ShenandoahThreadLocalData::satb_mark_queue(jt).apply_closure_and_empty(_satb_cl);
 496       }
 497     } else if (thread->is_VM_thread()) {
 498       if (thread->claim_oops_do(true, _thread_parity)) {
 499         ShenandoahBarrierSet::satb_mark_queue_set().shared_satb_queue()->apply_closure_and_empty(_satb_cl);
 500       }
 501     }
 502   }
 503 };
 504 
 505 #if TASKQUEUE_STATS
 506 void ShenandoahConcurrentMark::print_taskqueue_stats_hdr(outputStream* const st) {
 507   st->print_raw_cr("GC Task Stats");
 508   st->print_raw("thr "); TaskQueueStats::print_header(1, st); st->cr();
 509   st->print_raw("--- "); TaskQueueStats::print_header(2, st); st->cr();
 510 }
 511 
 512 void ShenandoahConcurrentMark::print_taskqueue_stats() const {
 513   if (!log_develop_is_enabled(Trace, gc, task, stats)) {
 514     return;
 515   }
 516   Log(gc, task, stats) log;
 517   ResourceMark rm;
 518   LogStream ls(log.trace());
 519   outputStream* st = &ls;
 520   print_taskqueue_stats_hdr(st);
 521 
 522   TaskQueueStats totals;
 523   const uint n = _task_queues->size();
 524   for (uint i = 0; i < n; ++i) {
 525     st->print(UINT32_FORMAT_W(3), i);
 526     _task_queues->queue(i)->stats.print(st);
 527     st->cr();
 528     totals += _task_queues->queue(i)->stats;
 529   }
 530   st->print("tot "); totals.print(st); st->cr();
 531   DEBUG_ONLY(totals.verify());
 532 
 533 }
 534 
 535 void ShenandoahConcurrentMark::reset_taskqueue_stats() {
 536   const uint n = task_queues()->size();
 537   for (uint i = 0; i < n; ++i) {
 538     task_queues()->queue(i)->stats.reset();
 539   }
 540 }
 541 #endif // TASKQUEUE_STATS
 542 
 543 // Weak Reference Closures
 544 class ShenandoahCMDrainMarkingStackClosure: public VoidClosure {
 545   uint _worker_id;
 546   ParallelTaskTerminator* _terminator;
 547   bool _reset_terminator;
 548 
 549 public:
 550   ShenandoahCMDrainMarkingStackClosure(uint worker_id, ParallelTaskTerminator* t, bool reset_terminator = false):
 551     _worker_id(worker_id),
 552     _terminator(t),
 553     _reset_terminator(reset_terminator) {
 554   }
 555 
 556   void do_void() {
 557     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 558 
 559     ShenandoahHeap* sh = ShenandoahHeap::heap();
 560     ShenandoahConcurrentMark* scm = sh->concurrentMark();
 561     assert(scm->process_references(), "why else would we be here?");
 562     ReferenceProcessor* rp = sh->ref_processor();
 563 
 564     shenandoah_assert_rp_isalive_installed();
 565 
 566     scm->mark_loop(_worker_id, _terminator, rp,
 567                    false, // not cancellable
 568                    scm->unload_classes(),
 569                    sh->has_forwarded_objects(),
 570                    false);  // do not do strdedup
 571 
 572     if (_reset_terminator) {
 573       _terminator->reset_for_reuse();
 574     }
 575   }
 576 };
 577 
 578 
 579 class ShenandoahCMKeepAliveClosure : public OopClosure {
 580 private:
 581   ShenandoahObjToScanQueue* _queue;
 582   ShenandoahHeap* _heap;
 583 
 584   template <class T>
 585   inline void do_oop_nv(T* p) {
 586     ShenandoahConcurrentMark::mark_through_ref<T, NONE, false /* string dedup */>(p, _heap, _queue);
 587   }
 588 
 589 public:
 590   ShenandoahCMKeepAliveClosure(ShenandoahObjToScanQueue* q) :
 591     _queue(q), _heap(ShenandoahHeap::heap()) {}
 592 
 593   void do_oop(narrowOop* p) { do_oop_nv(p); }
 594   void do_oop(oop* p)       { do_oop_nv(p); }
 595 };
 596 
 597 class ShenandoahCMKeepAliveUpdateClosure : public OopClosure {
 598 private:
 599   ShenandoahObjToScanQueue* _queue;
 600   ShenandoahHeap* _heap;
 601 
 602   template <class T>
 603   inline void do_oop_nv(T* p) {
 604     ShenandoahConcurrentMark::mark_through_ref<T, SIMPLE, false /* string dedup */>(p, _heap, _queue);
 605   }
 606 
 607 public:
 608   ShenandoahCMKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) :
 609     _queue(q), _heap(ShenandoahHeap::heap()) {}
 610 
 611   void do_oop(narrowOop* p) { do_oop_nv(p); }
 612   void do_oop(oop* p)       { do_oop_nv(p); }
 613 };
 614 
 615 class ShenandoahRefProcTaskProxy : public AbstractGangTask {
 616 
 617 private:
 618   AbstractRefProcTaskExecutor::ProcessTask& _proc_task;
 619   ParallelTaskTerminator* _terminator;
 620 public:
 621 
 622   ShenandoahRefProcTaskProxy(AbstractRefProcTaskExecutor::ProcessTask& proc_task,
 623                              ParallelTaskTerminator* t) :
 624     AbstractGangTask("Process reference objects in parallel"),
 625     _proc_task(proc_task),
 626     _terminator(t) {
 627   }
 628 
 629   void work(uint worker_id) {
 630     ResourceMark rm;
 631     HandleMark hm;
 632     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 633     ShenandoahHeap* heap = ShenandoahHeap::heap();
 634     ShenandoahCMDrainMarkingStackClosure complete_gc(worker_id, _terminator);
 635     if (heap->has_forwarded_objects()) {
 636       ShenandoahForwardedIsAliveClosure is_alive;
 637       ShenandoahCMKeepAliveUpdateClosure keep_alive(heap->concurrentMark()->get_queue(worker_id));
 638       _proc_task.work(worker_id, is_alive, keep_alive, complete_gc);
 639     } else {
 640       ShenandoahIsAliveClosure is_alive;
 641       ShenandoahCMKeepAliveClosure keep_alive(heap->concurrentMark()->get_queue(worker_id));
 642       _proc_task.work(worker_id, is_alive, keep_alive, complete_gc);
 643     }
 644   }
 645 };
 646 
 647 class ShenandoahRefProcTaskExecutor : public AbstractRefProcTaskExecutor {
 648 
 649 private:
 650   WorkGang* _workers;
 651 
 652 public:
 653 
 654   ShenandoahRefProcTaskExecutor(WorkGang* workers) :
 655     _workers(workers) {
 656   }
 657 
 658   // Executes a task using worker threads.
 659   void execute(ProcessTask& task) {
 660     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 661 
 662     // Shortcut execution if task is empty.
 663     // This should be replaced with the generic ReferenceProcessor shortcut,
 664     // see JDK-8181214, JDK-8043575, JDK-6938732.
 665     if (task.is_empty()) {
 666       return;
 667     }
 668 
 669     ShenandoahHeap* heap = ShenandoahHeap::heap();
 670     ShenandoahConcurrentMark* cm = heap->concurrentMark();
 671     uint nworkers = _workers->active_workers();
 672     cm->task_queues()->reserve(nworkers);
 673     if (UseShenandoahOWST) {
 674       ShenandoahTaskTerminator terminator(nworkers, cm->task_queues());
 675       ShenandoahRefProcTaskProxy proc_task_proxy(task, &terminator);
 676       _workers->run_task(&proc_task_proxy);
 677     } else {
 678       ParallelTaskTerminator terminator(nworkers, cm->task_queues());
 679       ShenandoahRefProcTaskProxy proc_task_proxy(task, &terminator);
 680       _workers->run_task(&proc_task_proxy);
 681     }
 682   }
 683 };
 684 
 685 
 686 void ShenandoahConcurrentMark::weak_refs_work(bool full_gc) {
 687   assert(process_references(), "sanity");
 688 
 689   ShenandoahHeap* sh = ShenandoahHeap::heap();
 690 
 691   ShenandoahPhaseTimings::Phase phase_root =
 692           full_gc ?
 693           ShenandoahPhaseTimings::full_gc_weakrefs :
 694           ShenandoahPhaseTimings::weakrefs;
 695 
 696   ShenandoahGCPhase phase(phase_root);
 697 
 698   ReferenceProcessor* rp = sh->ref_processor();
 699 
 700   // NOTE: We cannot shortcut on has_discovered_references() here, because
 701   // we will miss marking JNI Weak refs then, see implementation in
 702   // ReferenceProcessor::process_discovered_references.
 703   weak_refs_work_doit(full_gc);
 704 
 705   rp->verify_no_references_recorded();
 706   assert(!rp->discovery_enabled(), "Post condition");
 707 
 708 }
 709 
 710 void ShenandoahConcurrentMark::weak_refs_work_doit(bool full_gc) {
 711   ShenandoahHeap* sh = ShenandoahHeap::heap();
 712 
 713   ReferenceProcessor* rp = sh->ref_processor();
 714 
 715   ShenandoahPhaseTimings::Phase phase_process =
 716           full_gc ?
 717           ShenandoahPhaseTimings::full_gc_weakrefs_process :
 718           ShenandoahPhaseTimings::weakrefs_process;
 719 
 720   shenandoah_assert_rp_isalive_not_installed();
 721   ReferenceProcessorIsAliveMutator fix_isalive(rp, sh->is_alive_closure());
 722 
 723   WorkGang* workers = sh->workers();
 724   uint nworkers = workers->active_workers();
 725 
 726   // Setup collector policy for softref cleaning.
 727   bool clear_soft_refs = sh->soft_ref_policy()->use_should_clear_all_soft_refs(true /* bogus arg*/);
 728   log_develop_debug(gc, ref)("clearing soft refs: %s", BOOL_TO_STR(clear_soft_refs));
 729   rp->setup_policy(clear_soft_refs);
 730   rp->set_active_mt_degree(nworkers);
 731 
 732   assert(task_queues()->is_empty(), "Should be empty");
 733 
 734   // complete_gc and keep_alive closures instantiated here are only needed for
 735   // single-threaded path in RP. They share the queue 0 for tracking work, which
 736   // simplifies implementation. Since RP may decide to call complete_gc several
 737   // times, we need to be able to reuse the terminator.
 738   uint serial_worker_id = 0;
 739   ParallelTaskTerminator terminator(1, task_queues());
 740   ShenandoahCMDrainMarkingStackClosure complete_gc(serial_worker_id, &terminator, /* reset_terminator = */ true);
 741 
 742   ShenandoahRefProcTaskExecutor executor(workers);
 743 
 744   ReferenceProcessorPhaseTimes pt(sh->gc_timer(), rp->num_queues());
 745 
 746   {
 747     ShenandoahGCPhase phase(phase_process);
 748 
 749     if (sh->has_forwarded_objects()) {
 750       ShenandoahForwardedIsAliveClosure is_alive;
 751       ShenandoahCMKeepAliveUpdateClosure keep_alive(get_queue(serial_worker_id));
 752       rp->process_discovered_references(&is_alive, &keep_alive,
 753                                         &complete_gc, &executor,
 754                                         &pt);
 755 
 756       WeakProcessor::weak_oops_do(&is_alive, &keep_alive);
 757     } else {
 758       ShenandoahIsAliveClosure is_alive;
 759       ShenandoahCMKeepAliveClosure keep_alive(get_queue(serial_worker_id));
 760       rp->process_discovered_references(&is_alive, &keep_alive,
 761                                         &complete_gc, &executor,
 762                                         &pt);
 763 
 764       WeakProcessor::weak_oops_do(&is_alive, &keep_alive);
 765     }
 766     pt.print_all_references();
 767 
 768     assert(task_queues()->is_empty(), "Should be empty");
 769   }
 770 }
 771 
 772 class ShenandoahCancelledGCYieldClosure : public YieldClosure {
 773 private:
 774   ShenandoahHeap* const _heap;
 775 public:
 776   ShenandoahCancelledGCYieldClosure() : _heap(ShenandoahHeap::heap()) {};
 777   virtual bool should_return() { return _heap->cancelled_gc(); }
 778 };
 779 
 780 class ShenandoahPrecleanCompleteGCClosure : public VoidClosure {
 781 public:
 782   void do_void() {
 783     ShenandoahHeap* sh = ShenandoahHeap::heap();
 784     ShenandoahConcurrentMark* scm = sh->concurrentMark();
 785     assert(scm->process_references(), "why else would we be here?");
 786     ParallelTaskTerminator terminator(1, scm->task_queues());
 787 
 788     ReferenceProcessor* rp = sh->ref_processor();
 789     shenandoah_assert_rp_isalive_installed();
 790 
 791     scm->mark_loop(0, &terminator, rp,
 792                    false, // not cancellable
 793                    scm->unload_classes(),
 794                    sh->has_forwarded_objects(),
 795                    false); // do not do strdedup
 796   }
 797 };
 798 
 799 class ShenandoahPrecleanKeepAliveUpdateClosure : public OopClosure {
 800 private:
 801   ShenandoahObjToScanQueue* _queue;
 802   ShenandoahHeap* _heap;
 803 
 804   template <class T>
 805   inline void do_oop_nv(T* p) {
 806     ShenandoahConcurrentMark::mark_through_ref<T, CONCURRENT, false /* string dedup */>(p, _heap, _queue);
 807   }
 808 
 809 public:
 810   ShenandoahPrecleanKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) :
 811     _queue(q), _heap(ShenandoahHeap::heap()) {}
 812 
 813   void do_oop(narrowOop* p) { do_oop_nv(p); }
 814   void do_oop(oop* p)       { do_oop_nv(p); }
 815 };
 816 
 817 void ShenandoahConcurrentMark::preclean_weak_refs() {
 818   // Pre-cleaning weak references before diving into STW makes sense at the
 819   // end of concurrent mark. This will filter out the references which referents
 820   // are alive. Note that ReferenceProcessor already filters out these on reference
 821   // discovery, and the bulk of work is done here. This phase processes leftovers
 822   // that missed the initial filtering, i.e. when referent was marked alive after
 823   // reference was discovered by RP.
 824 
 825   assert(process_references(), "sanity");
 826 
 827   ShenandoahHeap* sh = ShenandoahHeap::heap();
 828   ReferenceProcessor* rp = sh->ref_processor();
 829 
 830   // Shortcut if no references were discovered to avoid winding up threads.
 831   if (!rp->has_discovered_references()) {
 832     return;
 833   }
 834 
 835   ReferenceProcessorMTDiscoveryMutator fix_mt_discovery(rp, false);
 836 
 837   shenandoah_assert_rp_isalive_not_installed();
 838   ReferenceProcessorIsAliveMutator fix_isalive(rp, sh->is_alive_closure());
 839 
 840   // Interrupt on cancelled GC
 841   ShenandoahCancelledGCYieldClosure yield;
 842 
 843   assert(task_queues()->is_empty(), "Should be empty");
 844 
 845   ShenandoahPrecleanCompleteGCClosure complete_gc;
 846   if (sh->has_forwarded_objects()) {
 847     ShenandoahForwardedIsAliveClosure is_alive;
 848     ShenandoahPrecleanKeepAliveUpdateClosure keep_alive(get_queue(0));
 849     ResourceMark rm;
 850     rp->preclean_discovered_references(&is_alive, &keep_alive,
 851                                        &complete_gc, &yield,
 852                                        NULL);
 853   } else {
 854     ShenandoahIsAliveClosure is_alive;
 855     ShenandoahCMKeepAliveClosure keep_alive(get_queue(0));
 856     ResourceMark rm;
 857     rp->preclean_discovered_references(&is_alive, &keep_alive,
 858                                        &complete_gc, &yield,
 859                                        NULL);
 860   }
 861 
 862   assert(task_queues()->is_empty(), "Should be empty");
 863 }
 864 
 865 void ShenandoahConcurrentMark::cancel() {
 866   // Clean up marking stacks.
 867   ShenandoahObjToScanQueueSet* queues = task_queues();
 868   queues->clear();
 869 
 870   // Cancel SATB buffers.
 871   ShenandoahBarrierSet::satb_mark_queue_set().abandon_partial_marking();
 872 }
 873 
 874 ShenandoahObjToScanQueue* ShenandoahConcurrentMark::get_queue(uint worker_id) {
 875   assert(task_queues()->get_reserved() > worker_id, "No reserved queue for worker id: %d", worker_id);
 876   return _task_queues->queue(worker_id);
 877 }
 878 
 879 void ShenandoahConcurrentMark::clear_queue(ShenandoahObjToScanQueue *q) {
 880   q->set_empty();
 881   q->overflow_stack()->clear();
 882   q->clear_buffer();
 883 }
 884 
 885 template <bool CANCELLABLE>
 886 void ShenandoahConcurrentMark::mark_loop_prework(uint w, ParallelTaskTerminator *t, ReferenceProcessor *rp,
 887                                                  bool class_unload, bool update_refs, bool strdedup) {
 888   ShenandoahObjToScanQueue* q = get_queue(w);
 889 
 890   jushort* ld = get_liveness(w);
 891   Copy::fill_to_bytes(ld, _heap->num_regions() * sizeof(jushort));
 892 
 893   // TODO: We can clean up this if we figure out how to do templated oop closures that
 894   // play nice with specialized_oop_iterators.
 895   if (class_unload) {
 896     if (update_refs) {
 897       if (strdedup) {
 898         ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w);
 899         ShenandoahMarkUpdateRefsMetadataDedupClosure cl(q, dq, rp);
 900         mark_loop_work<ShenandoahMarkUpdateRefsMetadataDedupClosure, CANCELLABLE>(&cl, ld, w, t);
 901       } else {
 902         ShenandoahMarkUpdateRefsMetadataClosure cl(q, rp);
 903         mark_loop_work<ShenandoahMarkUpdateRefsMetadataClosure, CANCELLABLE>(&cl, ld, w, t);
 904       }
 905     } else {
 906       if (strdedup) {
 907         ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w);
 908         ShenandoahMarkRefsMetadataDedupClosure cl(q, dq, rp);
 909         mark_loop_work<ShenandoahMarkRefsMetadataDedupClosure, CANCELLABLE>(&cl, ld, w, t);
 910       } else {
 911         ShenandoahMarkRefsMetadataClosure cl(q, rp);
 912         mark_loop_work<ShenandoahMarkRefsMetadataClosure, CANCELLABLE>(&cl, ld, w, t);
 913       }
 914     }
 915   } else {
 916     if (update_refs) {
 917       if (strdedup) {
 918         ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w);
 919         ShenandoahMarkUpdateRefsDedupClosure cl(q, dq, rp);
 920         mark_loop_work<ShenandoahMarkUpdateRefsDedupClosure, CANCELLABLE>(&cl, ld, w, t);
 921       } else {
 922         ShenandoahMarkUpdateRefsClosure cl(q, rp);
 923         mark_loop_work<ShenandoahMarkUpdateRefsClosure, CANCELLABLE>(&cl, ld, w, t);
 924       }
 925     } else {
 926       if (strdedup) {
 927         ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w);
 928         ShenandoahMarkRefsDedupClosure cl(q, dq, rp);
 929         mark_loop_work<ShenandoahMarkRefsDedupClosure, CANCELLABLE>(&cl, ld, w, t);
 930       } else {
 931         ShenandoahMarkRefsClosure cl(q, rp);
 932         mark_loop_work<ShenandoahMarkRefsClosure, CANCELLABLE>(&cl, ld, w, t);
 933       }
 934     }
 935   }
 936 
 937 
 938   for (uint i = 0; i < _heap->num_regions(); i++) {
 939     ShenandoahHeapRegion* r = _heap->get_region(i);
 940     jushort live = ld[i];
 941     if (live > 0) {
 942       r->increase_live_data_gc_words(live);
 943     }
 944   }
 945 }
 946 
 947 template <class T, bool CANCELLABLE>
 948 void ShenandoahConcurrentMark::mark_loop_work(T* cl, jushort* live_data, uint worker_id, ParallelTaskTerminator *terminator) {
 949   int seed = 17;
 950   uintx stride = CANCELLABLE ? ShenandoahMarkLoopStride : 1;
 951 
 952   ShenandoahHeap* heap = ShenandoahHeap::heap();
 953   ShenandoahObjToScanQueueSet* queues = task_queues();
 954   ShenandoahObjToScanQueue* q;
 955   ShenandoahMarkTask t;
 956 
 957   /*
 958    * Process outstanding queues, if any.
 959    *
 960    * There can be more queues than workers. To deal with the imbalance, we claim
 961    * extra queues first. Since marking can push new tasks into the queue associated
 962    * with this worker id, we come back to process this queue in the normal loop.
 963    */
 964   assert(queues->get_reserved() == heap->workers()->active_workers(),
 965     "Need to reserve proper number of queues");
 966 
 967   q = queues->claim_next();
 968   while (q != NULL) {
 969     if (CANCELLABLE && heap->check_cancelled_gc_and_yield()) {
 970       ShenandoahCancelledTerminatorTerminator tt;
 971       while (!terminator->offer_termination(&tt));
 972       return;
 973     }
 974 
 975     for (uint i = 0; i < stride; i++) {
 976       if (try_queue(q, t)) {
 977         do_task<T>(q, cl, live_data, &t);
 978       } else {
 979         assert(q->is_empty(), "Must be empty");
 980         q = queues->claim_next();
 981         break;
 982       }
 983     }
 984   }
 985   q = get_queue(worker_id);
 986 
 987   ShenandoahSATBBufferClosure drain_satb(q);
 988   SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 989 
 990   /*
 991    * Normal marking loop:
 992    */
 993   while (true) {
 994     if (CANCELLABLE && heap->check_cancelled_gc_and_yield()) {
 995       ShenandoahCancelledTerminatorTerminator tt;
 996       while (!terminator->offer_termination(&tt));
 997       return;
 998     }
 999 
1000     while (satb_mq_set.completed_buffers_num() > 0) {
1001       satb_mq_set.apply_closure_to_completed_buffer(&drain_satb);
1002     }
1003 
1004     uint work = 0;
1005     for (uint i = 0; i < stride; i++) {
1006       if (try_queue(q, t) ||
1007           queues->steal(worker_id, &seed, t)) {
1008         do_task<T>(q, cl, live_data, &t);
1009         work++;
1010       } else {
1011         break;
1012       }
1013     }
1014 
1015     if (work == 0) {
1016       // No work encountered in current stride, try to terminate.
1017       // Need to leave the STS here otherwise it might block safepoints.
1018       SuspendibleThreadSetLeaver stsl(CANCELLABLE && ShenandoahSuspendibleWorkers);
1019       if (terminator->offer_termination()) return;
1020     }
1021   }
1022 }
1023 
1024 bool ShenandoahConcurrentMark::process_references() const {
1025   return _heap->process_references();
1026 }
1027 
1028 bool ShenandoahConcurrentMark::unload_classes() const {
1029   return _heap->unload_classes();
1030 }
1031 
1032 bool ShenandoahConcurrentMark::claim_codecache() {
1033   assert(ShenandoahConcurrentScanCodeRoots, "must not be called otherwise");
1034   return _claimed_codecache.try_set();
1035 }
1036 
1037 void ShenandoahConcurrentMark::clear_claim_codecache() {
1038   assert(ShenandoahConcurrentScanCodeRoots, "must not be called otherwise");
1039   _claimed_codecache.unset();
1040 }
1041 
1042 jushort* ShenandoahConcurrentMark::get_liveness(uint worker_id) {
1043   return _liveness_local[worker_id];
1044 }
1045 
1046 // Generate Shenandoah specialized oop_oop_iterate functions.
1047 SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_SHENANDOAH(ALL_KLASS_OOP_OOP_ITERATE_DEFN)