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