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