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