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 address 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   StringTable::reset_dead_counter();
 311 }
 312 
 313 ZWeakRootsIterator::~ZWeakRootsIterator() {
 314   ZStatTimer timer(ZSubPhasePauseWeakRootsTeardown);
 315   StringTable::finish_dead_counter();
 316 }
 317 
 318 void ZWeakRootsIterator::do_vm_weak_handles(BoolObjectClosure* is_alive, OopClosure* cl) {
 319   ZStatTimer timer(ZSubPhasePauseWeakRootsVMWeakHandles);
 320   _vm_weak_handles_iter.weak_oops_do(is_alive, cl);
 321 }
 322 
 323 void ZWeakRootsIterator::do_jni_weak_handles(BoolObjectClosure* is_alive, OopClosure* cl) {
 324   ZStatTimer timer(ZSubPhasePauseWeakRootsJNIWeakHandles);
 325   _jni_weak_handles_iter.weak_oops_do(is_alive, cl);
 326 }
 327 
 328 void ZWeakRootsIterator::do_jvmti_weak_export(BoolObjectClosure* is_alive, OopClosure* cl) {
 329   ZStatTimer timer(ZSubPhasePauseWeakRootsJVMTIWeakExport);
 330   JvmtiExport::weak_oops_do(is_alive, cl);
 331 }
 332 
 333 void ZWeakRootsIterator::do_jfr_weak(BoolObjectClosure* is_alive, OopClosure* cl) {
 334 #if INCLUDE_JFR
 335   ZStatTimer timer(ZSubPhasePauseWeakRootsJFRWeak);
 336   Jfr::weak_oops_do(is_alive, cl);
 337 #endif
 338 }
 339 
 340 void ZWeakRootsIterator::do_symbol_table(BoolObjectClosure* is_alive, OopClosure* cl) {
 341   ZStatTimer timer(ZSubPhasePauseWeakRootsSymbolTable);
 342   int dummy;
 343   SymbolTable::possibly_parallel_unlink(&dummy, &dummy);
 344 }
 345 
 346 class ZStringTableDeadCounterBoolObjectClosure : public BoolObjectClosure  {
 347 private:
 348   BoolObjectClosure* const _cl;
 349   size_t                   _ndead;
 350 
 351 public:
 352   ZStringTableDeadCounterBoolObjectClosure(BoolObjectClosure* cl) :
 353       _cl(cl),
 354       _ndead(0) {}
 355 
 356   ~ZStringTableDeadCounterBoolObjectClosure() {
 357     StringTable::inc_dead_counter(_ndead);
 358   }
 359 
 360   virtual bool do_object_b(oop obj) {
 361     if (_cl->do_object_b(obj)) {
 362       return true;
 363     }
 364 
 365     _ndead++;
 366     return false;
 367   }
 368 };
 369 
 370 void ZWeakRootsIterator::do_string_table(BoolObjectClosure* is_alive, OopClosure* cl) {
 371   ZStatTimer timer(ZSubPhasePauseWeakRootsStringTable);
 372   ZStringTableDeadCounterBoolObjectClosure counter_is_alive(is_alive);
 373   _string_table_iter.weak_oops_do(&counter_is_alive, cl);
 374 }
 375 
 376 void ZWeakRootsIterator::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* cl) {
 377   ZStatTimer timer(ZSubPhasePauseWeakRoots);
 378   if (ZSymbolTableUnloading) {
 379     _symbol_table.weak_oops_do(is_alive, cl);
 380   }
 381   if (ZWeakRoots) {
 382     _jvmti_weak_export.weak_oops_do(is_alive, cl);
 383     _jfr_weak.weak_oops_do(is_alive, cl);
 384     if (!ZConcurrentVMWeakHandles) {
 385       _vm_weak_handles.weak_oops_do(is_alive, cl);
 386     }
 387     if (!ZConcurrentJNIWeakGlobalHandles) {
 388       _jni_weak_handles.weak_oops_do(is_alive, cl);
 389     }
 390     if (!ZConcurrentStringTable) {
 391       _string_table.weak_oops_do(is_alive, cl);
 392     }
 393   }
 394 }
 395 
 396 void ZWeakRootsIterator::oops_do(OopClosure* cl) {
 397   AlwaysTrueClosure always_alive;
 398   weak_oops_do(&always_alive, cl);
 399 }
 400 
 401 ZConcurrentWeakRootsIterator::ZConcurrentWeakRootsIterator() :
 402     _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()),
 403     _jni_weak_handles_iter(JNIHandles::weak_global_handles()),
 404     _string_table_iter(StringTable::weak_storage()),
 405     _vm_weak_handles(this),
 406     _jni_weak_handles(this),
 407     _string_table(this) {
 408   StringTable::reset_dead_counter();
 409 }
 410 
 411 ZConcurrentWeakRootsIterator::~ZConcurrentWeakRootsIterator() {
 412   StringTable::finish_dead_counter();
 413 }
 414 
 415 void ZConcurrentWeakRootsIterator::do_vm_weak_handles(OopClosure* cl) {
 416   ZStatTimer timer(ZSubPhaseConcurrentWeakRootsVMWeakHandles);
 417   _vm_weak_handles_iter.oops_do(cl);
 418 }
 419 
 420 void ZConcurrentWeakRootsIterator::do_jni_weak_handles(OopClosure* cl) {
 421   ZStatTimer timer(ZSubPhaseConcurrentWeakRootsJNIWeakHandles);
 422   _jni_weak_handles_iter.oops_do(cl);
 423 }
 424 
 425 class ZStringTableDeadCounterOopClosure : public OopClosure  {
 426 private:
 427   OopClosure* const _cl;
 428   size_t            _ndead;
 429 
 430 public:
 431   ZStringTableDeadCounterOopClosure(OopClosure* cl) :
 432       _cl(cl),
 433       _ndead(0) {}
 434 
 435   ~ZStringTableDeadCounterOopClosure() {
 436     StringTable::inc_dead_counter(_ndead);
 437   }
 438 
 439   virtual void do_oop(oop* p) {
 440     _cl->do_oop(p);
 441     if (*p == NULL) {
 442       _ndead++;
 443     }
 444   }
 445 
 446   virtual void do_oop(narrowOop* p) {
 447     ShouldNotReachHere();
 448   }
 449 };
 450 
 451 void ZConcurrentWeakRootsIterator::do_string_table(OopClosure* cl) {
 452   ZStatTimer timer(ZSubPhaseConcurrentWeakRootsStringTable);
 453   ZStringTableDeadCounterOopClosure counter_cl(cl);
 454   _string_table_iter.oops_do(&counter_cl);
 455 }
 456 
 457 void ZConcurrentWeakRootsIterator::oops_do(OopClosure* cl) {
 458   ZStatTimer timer(ZSubPhaseConcurrentWeakRoots);
 459   if (ZWeakRoots) {
 460     if (ZConcurrentVMWeakHandles) {
 461       _vm_weak_handles.oops_do(cl);
 462     }
 463     if (ZConcurrentJNIWeakGlobalHandles) {
 464       _jni_weak_handles.oops_do(cl);
 465     }
 466     if (ZConcurrentStringTable) {
 467       _string_table.oops_do(cl);
 468     }
 469   }
 470 }
 471 
 472 ZThreadRootsIterator::ZThreadRootsIterator() :
 473     _threads(this) {
 474   assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint");
 475   ZStatTimer timer(ZSubPhasePauseRootsSetup);
 476   Threads::change_thread_claim_parity();
 477 }
 478 
 479 ZThreadRootsIterator::~ZThreadRootsIterator() {
 480   ZStatTimer timer(ZSubPhasePauseRootsTeardown);
 481   Threads::assert_all_threads_claimed();
 482 }
 483 
 484 void ZThreadRootsIterator::do_threads(OopClosure* cl) {
 485   ZStatTimer timer(ZSubPhasePauseRootsThreads);
 486   ResourceMark rm;
 487   Threads::possibly_parallel_oops_do(true, cl, NULL);
 488 }
 489 
 490 void ZThreadRootsIterator::oops_do(OopClosure* cl) {
 491   ZStatTimer timer(ZSubPhasePauseRoots);
 492   _threads.oops_do(cl);
 493 }