1 /*
   2  * Copyright (c) 2015, 2019, 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 "classfile/classLoaderDataGraph.hpp"
  26 #include "classfile/stringTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "compiler/oopMap.hpp"
  30 #include "gc/shared/barrierSet.hpp"
  31 #include "gc/shared/barrierSetNMethod.hpp"
  32 #include "gc/shared/oopStorageParState.inline.hpp"
  33 #include "gc/shared/suspendibleThreadSet.hpp"
  34 #include "gc/z/zBarrierSetNMethod.hpp"
  35 #include "gc/z/zGlobals.hpp"
  36 #include "gc/z/zNMethod.hpp"
  37 #include "gc/z/zOopClosures.inline.hpp"
  38 #include "gc/z/zRootsIterator.hpp"
  39 #include "gc/z/zStat.hpp"
  40 #include "gc/z/zThreadLocalData.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "memory/universe.hpp"
  43 #include "prims/jvmtiExport.hpp"
  44 #include "runtime/atomic.hpp"
  45 #include "runtime/jniHandles.hpp"
  46 #include "runtime/thread.hpp"
  47 #include "runtime/safepoint.hpp"
  48 #include "runtime/synchronizer.hpp"
  49 #include "services/management.hpp"
  50 #include "utilities/debug.hpp"
  51 #if INCLUDE_JFR
  52 #include "jfr/jfr.hpp"
  53 #endif
  54 
  55 static const ZStatSubPhase ZSubPhasePauseRootsSetup("Pause Roots Setup");
  56 static const ZStatSubPhase ZSubPhasePauseRoots("Pause Roots");
  57 static const ZStatSubPhase ZSubPhasePauseRootsTeardown("Pause Roots Teardown");
  58 static const ZStatSubPhase ZSubPhasePauseRootsUniverse("Pause Roots Universe");
  59 static const ZStatSubPhase ZSubPhasePauseRootsObjectSynchronizer("Pause Roots ObjectSynchronizer");
  60 static const ZStatSubPhase ZSubPhasePauseRootsManagement("Pause Roots Management");
  61 static const ZStatSubPhase ZSubPhasePauseRootsJVMTIExport("Pause Roots JVMTIExport");
  62 static const ZStatSubPhase ZSubPhasePauseRootsJVMTIWeakExport("Pause Roots JVMTIWeakExport");
  63 static const ZStatSubPhase ZSubPhasePauseRootsSystemDictionary("Pause Roots SystemDictionary");
  64 static const ZStatSubPhase ZSubPhasePauseRootsThreads("Pause Roots Threads");
  65 static const ZStatSubPhase ZSubPhasePauseRootsCodeCache("Pause Roots CodeCache");
  66 
  67 static const ZStatSubPhase ZSubPhaseConcurrentRootsSetup("Concurrent Roots Setup");
  68 static const ZStatSubPhase ZSubPhaseConcurrentRoots("Concurrent Roots");
  69 static const ZStatSubPhase ZSubPhaseConcurrentRootsTeardown("Concurrent Roots Teardown");
  70 static const ZStatSubPhase ZSubPhaseConcurrentRootsJNIHandles("Concurrent Roots JNIHandles");
  71 static const ZStatSubPhase ZSubPhaseConcurrentRootsClassLoaderDataGraph("Concurrent Roots ClassLoaderDataGraph");
  72 
  73 static const ZStatSubPhase ZSubPhasePauseWeakRootsSetup("Pause Weak Roots Setup");
  74 static const ZStatSubPhase ZSubPhasePauseWeakRoots("Pause Weak Roots");
  75 static const ZStatSubPhase ZSubPhasePauseWeakRootsTeardown("Pause Weak Roots Teardown");
  76 static const ZStatSubPhase ZSubPhasePauseWeakRootsJVMTIWeakExport("Pause Weak Roots JVMTIWeakExport");
  77 static const ZStatSubPhase ZSubPhasePauseWeakRootsJFRWeak("Pause Weak Roots JFRWeak");
  78 
  79 static const ZStatSubPhase ZSubPhaseConcurrentWeakRoots("Concurrent Weak Roots");
  80 static const ZStatSubPhase ZSubPhaseConcurrentWeakRootsVMWeakHandles("Concurrent Weak Roots VMWeakHandles");
  81 static const ZStatSubPhase ZSubPhaseConcurrentWeakRootsJNIWeakHandles("Concurrent Weak Roots JNIWeakHandles");
  82 static const ZStatSubPhase ZSubPhaseConcurrentWeakRootsStringTable("Concurrent Weak Roots StringTable");
  83 
  84 template <typename T, void (T::*F)(ZRootsIteratorClosure*)>
  85 ZSerialOopsDo<T, F>::ZSerialOopsDo(T* iter) :
  86     _iter(iter),
  87     _claimed(false) {}
  88 
  89 template <typename T, void (T::*F)(ZRootsIteratorClosure*)>
  90 void ZSerialOopsDo<T, F>::oops_do(ZRootsIteratorClosure* cl) {
  91   if (!_claimed && Atomic::cmpxchg(true, &_claimed, false) == false) {
  92     (_iter->*F)(cl);
  93   }
  94 }
  95 
  96 template <typename T, void (T::*F)(ZRootsIteratorClosure*)>
  97 ZParallelOopsDo<T, F>::ZParallelOopsDo(T* iter) :
  98     _iter(iter),
  99     _completed(false) {}
 100 
 101 template <typename T, void (T::*F)(ZRootsIteratorClosure*)>
 102 void ZParallelOopsDo<T, F>::oops_do(ZRootsIteratorClosure* cl) {
 103   if (!_completed) {
 104     (_iter->*F)(cl);
 105     if (!_completed) {
 106       _completed = true;
 107     }
 108   }
 109 }
 110 
 111 template <typename T, void (T::*F)(BoolObjectClosure*, ZRootsIteratorClosure*)>
 112 ZSerialWeakOopsDo<T, F>::ZSerialWeakOopsDo(T* iter) :
 113     _iter(iter),
 114     _claimed(false) {}
 115 
 116 template <typename T, void (T::*F)(BoolObjectClosure*, ZRootsIteratorClosure*)>
 117 void ZSerialWeakOopsDo<T, F>::weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl) {
 118   if (!_claimed && Atomic::cmpxchg(true, &_claimed, false) == false) {
 119     (_iter->*F)(is_alive, cl);
 120   }
 121 }
 122 
 123 template <typename T, void (T::*F)(BoolObjectClosure*, ZRootsIteratorClosure*)>
 124 ZParallelWeakOopsDo<T, F>::ZParallelWeakOopsDo(T* iter) :
 125     _iter(iter),
 126     _completed(false) {}
 127 
 128 template <typename T, void (T::*F)(BoolObjectClosure*, ZRootsIteratorClosure*)>
 129 void ZParallelWeakOopsDo<T, F>::weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl) {
 130   if (!_completed) {
 131     (_iter->*F)(is_alive, cl);
 132     if (!_completed) {
 133       _completed = true;
 134     }
 135   }
 136 }
 137 
 138 class ZRootsIteratorCodeBlobClosure : public CodeBlobToOopClosure {
 139 private:
 140   BarrierSetNMethod* _bs;
 141 
 142 public:
 143   ZRootsIteratorCodeBlobClosure(OopClosure* cl) :
 144     CodeBlobToOopClosure(cl, true /* fix_relocations */),
 145     _bs(BarrierSet::barrier_set()->barrier_set_nmethod()) {}
 146 
 147   virtual void do_code_blob(CodeBlob* cb) {
 148     nmethod* const nm = cb->as_nmethod_or_null();
 149     if (nm != NULL && !nm->test_set_oops_do_mark()) {
 150       CodeBlobToOopClosure::do_code_blob(cb);
 151       _bs->disarm(nm);
 152     }
 153   }
 154 };
 155 
 156 class ZRootsIteratorThreadClosure : public ThreadClosure {
 157 private:
 158   ZRootsIteratorClosure* _cl;
 159 
 160 public:
 161   ZRootsIteratorThreadClosure(ZRootsIteratorClosure* cl) :
 162       _cl(cl) {}
 163 
 164   virtual void do_thread(Thread* thread) {
 165     ZRootsIteratorCodeBlobClosure code_cl(_cl);
 166     thread->oops_do(_cl, ClassUnloading ? &code_cl : NULL);
 167     _cl->do_thread(thread);
 168   }
 169 };
 170 
 171 ZRootsIterator::ZRootsIterator() :
 172     _universe(this),
 173     _object_synchronizer(this),
 174     _management(this),
 175     _jvmti_export(this),
 176     _jvmti_weak_export(this),
 177     _system_dictionary(this),
 178     _threads(this),
 179     _code_cache(this) {
 180   assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint");
 181   ZStatTimer timer(ZSubPhasePauseRootsSetup);
 182   Threads::change_thread_claim_token();
 183   COMPILER2_PRESENT(DerivedPointerTable::clear());
 184   if (ClassUnloading) {
 185     nmethod::oops_do_marking_prologue();
 186   } else {
 187     ZNMethod::oops_do_begin();
 188   }
 189 }
 190 
 191 ZRootsIterator::~ZRootsIterator() {
 192   ZStatTimer timer(ZSubPhasePauseRootsTeardown);
 193   ResourceMark rm;
 194   if (ClassUnloading) {
 195     nmethod::oops_do_marking_epilogue();
 196   } else {
 197     ZNMethod::oops_do_end();
 198   }
 199   JvmtiExport::gc_epilogue();
 200 
 201   COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
 202   Threads::assert_all_threads_claimed();
 203 }
 204 
 205 void ZRootsIterator::do_universe(ZRootsIteratorClosure* cl) {
 206   ZStatTimer timer(ZSubPhasePauseRootsUniverse);
 207   Universe::oops_do(cl);
 208 }
 209 
 210 void ZRootsIterator::do_object_synchronizer(ZRootsIteratorClosure* cl) {
 211   ZStatTimer timer(ZSubPhasePauseRootsObjectSynchronizer);
 212   ObjectSynchronizer::oops_do(cl);
 213 }
 214 
 215 void ZRootsIterator::do_management(ZRootsIteratorClosure* cl) {
 216   ZStatTimer timer(ZSubPhasePauseRootsManagement);
 217   Management::oops_do(cl);
 218 }
 219 
 220 void ZRootsIterator::do_jvmti_export(ZRootsIteratorClosure* cl) {
 221   ZStatTimer timer(ZSubPhasePauseRootsJVMTIExport);
 222   JvmtiExport::oops_do(cl);
 223 }
 224 
 225 void ZRootsIterator::do_jvmti_weak_export(ZRootsIteratorClosure* cl) {
 226   ZStatTimer timer(ZSubPhasePauseRootsJVMTIWeakExport);
 227   AlwaysTrueClosure always_alive;
 228   JvmtiExport::weak_oops_do(&always_alive, cl);
 229 }
 230 
 231 void ZRootsIterator::do_system_dictionary(ZRootsIteratorClosure* cl) {
 232   ZStatTimer timer(ZSubPhasePauseRootsSystemDictionary);
 233   SystemDictionary::oops_do(cl);
 234 }
 235 
 236 void ZRootsIterator::do_threads(ZRootsIteratorClosure* cl) {
 237   ZStatTimer timer(ZSubPhasePauseRootsThreads);
 238   ResourceMark rm;
 239   ZRootsIteratorThreadClosure thread_cl(cl);
 240   Threads::possibly_parallel_threads_do(true, &thread_cl);
 241 }
 242 
 243 void ZRootsIterator::do_code_cache(ZRootsIteratorClosure* cl) {
 244   ZStatTimer timer(ZSubPhasePauseRootsCodeCache);
 245   ZNMethod::oops_do(cl);
 246 }
 247 
 248 void ZRootsIterator::oops_do(ZRootsIteratorClosure* cl, bool visit_jvmti_weak_export) {
 249   ZStatTimer timer(ZSubPhasePauseRoots);
 250   _universe.oops_do(cl);
 251   _object_synchronizer.oops_do(cl);
 252   _management.oops_do(cl);
 253   _jvmti_export.oops_do(cl);
 254   _system_dictionary.oops_do(cl);
 255   _threads.oops_do(cl);
 256   if (!ClassUnloading) {
 257     _code_cache.oops_do(cl);
 258   }
 259   if (visit_jvmti_weak_export) {
 260     _jvmti_weak_export.oops_do(cl);
 261   }
 262 }
 263 
 264 ZConcurrentRootsIterator::ZConcurrentRootsIterator(bool marking) :
 265     _marking(marking),
 266     _sts_joiner(marking /* active */),
 267     _jni_handles_iter(JNIHandles::global_handles()),
 268     _jni_handles(this),
 269     _class_loader_data_graph(this) {
 270   ZStatTimer timer(ZSubPhaseConcurrentRootsSetup);
 271   if (_marking) {
 272     ClassLoaderDataGraph_lock->lock();
 273     ClassLoaderDataGraph::clear_claimed_marks();
 274   }
 275 }
 276 
 277 ZConcurrentRootsIterator::~ZConcurrentRootsIterator() {
 278   ZStatTimer timer(ZSubPhaseConcurrentRootsTeardown);
 279   if (_marking) {
 280     ClassLoaderDataGraph_lock->unlock();
 281   }
 282 }
 283 
 284 void ZConcurrentRootsIterator::do_jni_handles(ZRootsIteratorClosure* cl) {
 285   ZStatTimer timer(ZSubPhaseConcurrentRootsJNIHandles);
 286   _jni_handles_iter.oops_do(cl);
 287 }
 288 
 289 void ZConcurrentRootsIterator::do_class_loader_data_graph(ZRootsIteratorClosure* cl) {
 290   ZStatTimer timer(ZSubPhaseConcurrentRootsClassLoaderDataGraph);
 291   if (_marking) {
 292     CLDToOopClosure cld_cl(cl, ClassLoaderData::_claim_strong);
 293     ClassLoaderDataGraph::always_strong_cld_do(&cld_cl);
 294   } else {
 295     CLDToOopClosure cld_cl(cl, ClassLoaderData::_claim_none);
 296     ClassLoaderDataGraph::cld_do(&cld_cl);
 297   }
 298 }
 299 
 300 void ZConcurrentRootsIterator::oops_do(ZRootsIteratorClosure* cl) {
 301   ZStatTimer timer(ZSubPhaseConcurrentRoots);
 302   _jni_handles.oops_do(cl);
 303   _class_loader_data_graph.oops_do(cl);
 304 }
 305 
 306 ZWeakRootsIterator::ZWeakRootsIterator() :
 307     _jvmti_weak_export(this),
 308     _jfr_weak(this) {
 309   assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint");
 310   ZStatTimer timer(ZSubPhasePauseWeakRootsSetup);
 311 }
 312 
 313 ZWeakRootsIterator::~ZWeakRootsIterator() {
 314   ZStatTimer timer(ZSubPhasePauseWeakRootsTeardown);
 315 }
 316 
 317 void ZWeakRootsIterator::do_jvmti_weak_export(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl) {
 318   ZStatTimer timer(ZSubPhasePauseWeakRootsJVMTIWeakExport);
 319   JvmtiExport::weak_oops_do(is_alive, cl);
 320 }
 321 
 322 void ZWeakRootsIterator::do_jfr_weak(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl) {
 323 #if INCLUDE_JFR
 324   ZStatTimer timer(ZSubPhasePauseWeakRootsJFRWeak);
 325   Jfr::weak_oops_do(is_alive, cl);
 326 #endif
 327 }
 328 
 329 void ZWeakRootsIterator::weak_oops_do(BoolObjectClosure* is_alive, ZRootsIteratorClosure* cl) {
 330   ZStatTimer timer(ZSubPhasePauseWeakRoots);
 331   _jvmti_weak_export.weak_oops_do(is_alive, cl);
 332   _jfr_weak.weak_oops_do(is_alive, cl);
 333 }
 334 
 335 void ZWeakRootsIterator::oops_do(ZRootsIteratorClosure* cl) {
 336   AlwaysTrueClosure always_alive;
 337   weak_oops_do(&always_alive, cl);
 338 }
 339 
 340 ZConcurrentWeakRootsIterator::ZConcurrentWeakRootsIterator() :
 341     _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()),
 342     _jni_weak_handles_iter(JNIHandles::weak_global_handles()),
 343     _string_table_iter(StringTable::weak_storage()),
 344     _vm_weak_handles(this),
 345     _jni_weak_handles(this),
 346     _string_table(this) {
 347   StringTable::reset_dead_counter();
 348 }
 349 
 350 ZConcurrentWeakRootsIterator::~ZConcurrentWeakRootsIterator() {
 351   StringTable::finish_dead_counter();
 352 }
 353 
 354 void ZConcurrentWeakRootsIterator::do_vm_weak_handles(ZRootsIteratorClosure* cl) {
 355   ZStatTimer timer(ZSubPhaseConcurrentWeakRootsVMWeakHandles);
 356   _vm_weak_handles_iter.oops_do(cl);
 357 }
 358 
 359 void ZConcurrentWeakRootsIterator::do_jni_weak_handles(ZRootsIteratorClosure* cl) {
 360   ZStatTimer timer(ZSubPhaseConcurrentWeakRootsJNIWeakHandles);
 361   _jni_weak_handles_iter.oops_do(cl);
 362 }
 363 
 364 class ZStringTableDeadCounterClosure : public ZRootsIteratorClosure  {
 365 private:
 366   ZRootsIteratorClosure* const _cl;
 367   size_t                       _ndead;
 368 
 369 public:
 370   ZStringTableDeadCounterClosure(ZRootsIteratorClosure* cl) :
 371       _cl(cl),
 372       _ndead(0) {}
 373 
 374   ~ZStringTableDeadCounterClosure() {
 375     StringTable::inc_dead_counter(_ndead);
 376   }
 377 
 378   virtual void do_oop(oop* p) {
 379     _cl->do_oop(p);
 380     if (*p == NULL) {
 381       _ndead++;
 382     }
 383   }
 384 
 385   virtual void do_oop(narrowOop* p) {
 386     ShouldNotReachHere();
 387   }
 388 };
 389 
 390 void ZConcurrentWeakRootsIterator::do_string_table(ZRootsIteratorClosure* cl) {
 391   ZStatTimer timer(ZSubPhaseConcurrentWeakRootsStringTable);
 392   ZStringTableDeadCounterClosure counter_cl(cl);
 393   _string_table_iter.oops_do(&counter_cl);
 394 }
 395 
 396 void ZConcurrentWeakRootsIterator::oops_do(ZRootsIteratorClosure* cl) {
 397   ZStatTimer timer(ZSubPhaseConcurrentWeakRoots);
 398   _vm_weak_handles.oops_do(cl);
 399   _jni_weak_handles.oops_do(cl);
 400   _string_table.oops_do(cl);
 401 }
 402 
 403 ZThreadRootsIterator::ZThreadRootsIterator() :
 404     _threads(this) {
 405   assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint");
 406   ZStatTimer timer(ZSubPhasePauseRootsSetup);
 407   Threads::change_thread_claim_token();
 408 }
 409 
 410 ZThreadRootsIterator::~ZThreadRootsIterator() {
 411   ZStatTimer timer(ZSubPhasePauseRootsTeardown);
 412   Threads::assert_all_threads_claimed();
 413 }
 414 
 415 void ZThreadRootsIterator::do_threads(ZRootsIteratorClosure* cl) {
 416   ZStatTimer timer(ZSubPhasePauseRootsThreads);
 417   ResourceMark rm;
 418   Threads::possibly_parallel_oops_do(true, cl, NULL);
 419 }
 420 
 421 void ZThreadRootsIterator::oops_do(ZRootsIteratorClosure* cl) {
 422   ZStatTimer timer(ZSubPhasePauseRoots);
 423   _threads.oops_do(cl);
 424 }