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 84 template <typename T, void (T::*F)(OopClosure*)> 85 ZSerialOopsDo<T, F>::ZSerialOopsDo(T* iter) : 86 _iter(iter), 87 _claimed(false) {} 88 89 template <typename T, void (T::*F)(OopClosure*)> 90 void ZSerialOopsDo<T, F>::oops_do(OopClosure* cl) { 91 if (!_claimed && Atomic::cmpxchg(true, &_claimed, false) == false) { 92 (_iter->*F)(cl); 93 } 94 } 95 96 template <typename T, void (T::*F)(OopClosure*)> 97 ZParallelOopsDo<T, F>::ZParallelOopsDo(T* iter) : 98 _iter(iter), 99 _completed(false) {} 100 101 template <typename T, void (T::*F)(OopClosure*)> 102 void ZParallelOopsDo<T, F>::oops_do(OopClosure* 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*, OopClosure*)> 112 ZSerialWeakOopsDo<T, F>::ZSerialWeakOopsDo(T* iter) : 113 _iter(iter), 114 _claimed(false) {} 115 116 template <typename T, void (T::*F)(BoolObjectClosure*, OopClosure*)> 117 void ZSerialWeakOopsDo<T, F>::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* 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*, OopClosure*)> 124 ZParallelWeakOopsDo<T, F>::ZParallelWeakOopsDo(T* iter) : 125 _iter(iter), 126 _completed(false) {} 127 128 template <typename T, void (T::*F)(BoolObjectClosure*, OopClosure*)> 129 void ZParallelWeakOopsDo<T, F>::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* cl) { 130 if (!_completed) { 131 (_iter->*F)(is_alive, cl); 132 if (!_completed) { 133 _completed = true; 134 } 135 } 136 } 137 138 ZRootsIterator::ZRootsIterator() : 139 _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()), 140 _jni_handles_iter(JNIHandles::global_handles()), 141 _jni_weak_handles_iter(JNIHandles::weak_global_handles()), 142 _universe(this), 143 _object_synchronizer(this), 144 _management(this), 145 _jvmti_export(this), 146 _jvmti_weak_export(this), 147 _jfr_weak(this), 148 _system_dictionary(this), 149 _vm_weak_handles(this), 150 _jni_handles(this), 151 _jni_weak_handles(this), 152 _class_loader_data_graph(this), 153 _threads(this), 154 _code_cache(this), 155 _string_table(this) { 156 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 157 ZStatTimer timer(ZSubPhasePauseRootsSetup); 158 Threads::change_thread_claim_parity(); 159 StringTable::clear_parallel_claimed_index(); 160 ClassLoaderDataGraph::clear_claimed_marks(); 161 COMPILER2_PRESENT(DerivedPointerTable::clear()); 162 CodeCache::gc_prologue(); 163 ZNMethodTable::gc_prologue(); 164 } 165 166 ZRootsIterator::~ZRootsIterator() { 167 ZStatTimer timer(ZSubPhasePauseRootsTeardown); 168 ResourceMark rm; 169 ZNMethodTable::gc_epilogue(); 170 CodeCache::gc_epilogue(); 171 JvmtiExport::gc_epilogue(); 172 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 173 Threads::assert_all_threads_claimed(); 174 } 175 176 void ZRootsIterator::do_universe(OopClosure* cl) { 177 ZStatTimer timer(ZSubPhasePauseRootsUniverse); 178 Universe::oops_do(cl); 179 } 180 181 void ZRootsIterator::do_vm_weak_handles(OopClosure* cl) { 182 ZStatTimer timer(ZSubPhasePauseRootsVMWeakHandles); 183 _vm_weak_handles_iter.oops_do(cl); 184 } 185 186 void ZRootsIterator::do_jni_handles(OopClosure* cl) { 187 ZStatTimer timer(ZSubPhasePauseRootsJNIHandles); 188 _jni_handles_iter.oops_do(cl); 189 } 190 191 void ZRootsIterator::do_jni_weak_handles(OopClosure* cl) { 192 ZStatTimer timer(ZSubPhasePauseRootsJNIWeakHandles); 193 _jni_weak_handles_iter.oops_do(cl); 194 } 195 196 void ZRootsIterator::do_object_synchronizer(OopClosure* cl) { 197 ZStatTimer timer(ZSubPhasePauseRootsObjectSynchronizer); 198 ObjectSynchronizer::oops_do(cl); 199 } 200 201 void ZRootsIterator::do_management(OopClosure* cl) { 202 ZStatTimer timer(ZSubPhasePauseRootsManagement); 203 Management::oops_do(cl); 204 } 205 206 void ZRootsIterator::do_jvmti_export(OopClosure* cl) { 207 ZStatTimer timer(ZSubPhasePauseRootsJVMTIExport); 208 JvmtiExport::oops_do(cl); 209 } 210 211 void ZRootsIterator::do_jvmti_weak_export(OopClosure* cl) { 212 ZStatTimer timer(ZSubPhasePauseRootsJVMTIWeakExport); 213 AlwaysTrueClosure always_alive; 214 JvmtiExport::weak_oops_do(&always_alive, cl); 215 } 216 217 void ZRootsIterator::do_jfr_weak(OopClosure* cl) { 218 #if INCLUDE_JFR 219 ZStatTimer timer(ZSubPhasePauseRootsJFRWeak); 220 AlwaysTrueClosure always_alive; 221 Jfr::weak_oops_do(&always_alive, cl); 222 #endif 223 } 224 225 void ZRootsIterator::do_system_dictionary(OopClosure* cl) { 226 ZStatTimer timer(ZSubPhasePauseRootsSystemDictionary); 227 SystemDictionary::oops_do(cl); 228 } 229 230 void ZRootsIterator::do_class_loader_data_graph(OopClosure* cl) { 231 ZStatTimer timer(ZSubPhasePauseRootsClassLoaderDataGraph); 232 CLDToOopClosure cld_cl(cl); 233 ClassLoaderDataGraph::cld_do(&cld_cl); 234 } 235 236 class ZRootsIteratorThreadClosure : public ThreadClosure { 237 private: 238 OopClosure* const _cl; 239 240 public: 241 ZRootsIteratorThreadClosure(OopClosure* cl) : 242 _cl(cl) {} 243 244 virtual void do_thread(Thread* thread) { 245 if (thread->is_Java_thread()) { 246 // Update thread local adddress bad mask 247 ZThreadLocalData::set_address_bad_mask(thread, ZAddressBadMask); 248 } 249 250 // Process thread oops 251 thread->oops_do(_cl, NULL); 252 } 253 }; 254 255 void ZRootsIterator::do_threads(OopClosure* cl) { 256 ZStatTimer timer(ZSubPhasePauseRootsThreads); 257 ResourceMark rm; 258 ZRootsIteratorThreadClosure thread_cl(cl); 259 Threads::possibly_parallel_threads_do(true, &thread_cl); 260 } 261 262 void ZRootsIterator::do_code_cache(OopClosure* cl) { 263 ZStatTimer timer(ZSubPhasePauseRootsCodeCache); 264 ZNMethodTable::oops_do(cl); 265 } 266 267 void ZRootsIterator::do_string_table(OopClosure* cl) { 268 ZStatTimer timer(ZSubPhasePauseRootsStringTable); 269 StringTable::possibly_parallel_oops_do(cl); 270 } 271 272 void ZRootsIterator::oops_do(OopClosure* cl, bool visit_jvmti_weak_export) { 273 ZStatTimer timer(ZSubPhasePauseRoots); 274 _universe.oops_do(cl); 275 _object_synchronizer.oops_do(cl); 276 _management.oops_do(cl); 277 _jvmti_export.oops_do(cl); 278 _system_dictionary.oops_do(cl); 279 _jni_handles.oops_do(cl); 280 _class_loader_data_graph.oops_do(cl); 281 _threads.oops_do(cl); 282 _code_cache.oops_do(cl); 283 if (!ZWeakRoots) { 284 _jvmti_weak_export.oops_do(cl); 285 _jfr_weak.oops_do(cl); 286 _vm_weak_handles.oops_do(cl); 287 _jni_weak_handles.oops_do(cl); 288 _string_table.oops_do(cl); 289 } else { 290 if (visit_jvmti_weak_export) { 291 _jvmti_weak_export.oops_do(cl); 292 } 293 } 294 } 295 296 ZWeakRootsIterator::ZWeakRootsIterator() : 297 _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()), 298 _jni_weak_handles_iter(JNIHandles::weak_global_handles()), 299 _jvmti_weak_export(this), 300 _jfr_weak(this), 301 _vm_weak_handles(this), 302 _jni_weak_handles(this), 303 _symbol_table(this), 304 _string_table(this) { 305 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 306 ZStatTimer timer(ZSubPhasePauseWeakRootsSetup); 307 SymbolTable::clear_parallel_claimed_index(); 308 StringTable::clear_parallel_claimed_index(); 309 } 310 311 ZWeakRootsIterator::~ZWeakRootsIterator() { 312 ZStatTimer timer(ZSubPhasePauseWeakRootsTeardown); 313 } 314 315 void ZWeakRootsIterator::do_vm_weak_handles(BoolObjectClosure* is_alive, OopClosure* cl) { 316 ZStatTimer timer(ZSubPhasePauseWeakRootsVMWeakHandles); 317 _vm_weak_handles_iter.weak_oops_do(is_alive, cl); 318 } 319 320 void ZWeakRootsIterator::do_jni_weak_handles(BoolObjectClosure* is_alive, OopClosure* cl) { 321 ZStatTimer timer(ZSubPhasePauseWeakRootsJNIWeakHandles); 322 _jni_weak_handles_iter.weak_oops_do(is_alive, cl); 323 } 324 325 void ZWeakRootsIterator::do_jvmti_weak_export(BoolObjectClosure* is_alive, OopClosure* cl) { 326 ZStatTimer timer(ZSubPhasePauseWeakRootsJVMTIWeakExport); 327 JvmtiExport::weak_oops_do(is_alive, cl); 328 } 329 330 void ZWeakRootsIterator::do_jfr_weak(BoolObjectClosure* is_alive, OopClosure* cl) { 331 #if INCLUDE_JFR 332 ZStatTimer timer(ZSubPhasePauseWeakRootsJFRWeak); 333 Jfr::weak_oops_do(is_alive, cl); 334 #endif 335 } 336 337 void ZWeakRootsIterator::do_symbol_table(BoolObjectClosure* is_alive, OopClosure* cl) { 338 ZStatTimer timer(ZSubPhasePauseWeakRootsSymbolTable); 339 int dummy; 340 SymbolTable::possibly_parallel_unlink(&dummy, &dummy); 341 } 342 343 void ZWeakRootsIterator::do_string_table(BoolObjectClosure* is_alive, OopClosure* cl) { 344 ZStatTimer timer(ZSubPhasePauseWeakRootsStringTable); 345 int dummy; 346 StringTable::possibly_parallel_unlink_or_oops_do(is_alive, cl, &dummy, &dummy); 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 _string_table.weak_oops_do(is_alive, cl); 364 } 365 } 366 367 void ZWeakRootsIterator::oops_do(OopClosure* cl) { 368 AlwaysTrueClosure always_alive; 369 weak_oops_do(&always_alive, cl); 370 } 371 372 ZConcurrentWeakRootsIterator::ZConcurrentWeakRootsIterator() : 373 _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()), 374 _jni_weak_handles_iter(JNIHandles::weak_global_handles()), 375 _vm_weak_handles(this), 376 _jni_weak_handles(this) {} 377 378 void ZConcurrentWeakRootsIterator::do_vm_weak_handles(OopClosure* cl) { 379 ZStatTimer timer(ZSubPhaseConcurrentWeakRootsVMWeakHandles); 380 _vm_weak_handles_iter.oops_do(cl); 381 } 382 383 void ZConcurrentWeakRootsIterator::do_jni_weak_handles(OopClosure* cl) { 384 ZStatTimer timer(ZSubPhaseConcurrentWeakRootsJNIWeakHandles); 385 _jni_weak_handles_iter.oops_do(cl); 386 } 387 388 void ZConcurrentWeakRootsIterator::oops_do(OopClosure* cl) { 389 ZStatTimer timer(ZSubPhaseConcurrentWeakRoots); 390 if (ZWeakRoots) { 391 if (ZConcurrentVMWeakHandles) { 392 _vm_weak_handles.oops_do(cl); 393 } 394 if (ZConcurrentJNIWeakGlobalHandles) { 395 _jni_weak_handles.oops_do(cl); 396 } 397 } 398 } 399 400 ZThreadRootsIterator::ZThreadRootsIterator() : 401 _threads(this) { 402 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 403 ZStatTimer timer(ZSubPhasePauseRootsSetup); 404 Threads::change_thread_claim_parity(); 405 } 406 407 ZThreadRootsIterator::~ZThreadRootsIterator() { 408 ZStatTimer timer(ZSubPhasePauseRootsTeardown); 409 Threads::assert_all_threads_claimed(); 410 } 411 412 void ZThreadRootsIterator::do_threads(OopClosure* cl) { 413 ZStatTimer timer(ZSubPhasePauseRootsThreads); 414 ResourceMark rm; 415 Threads::possibly_parallel_oops_do(true, cl, NULL); 416 } 417 418 void ZThreadRootsIterator::oops_do(OopClosure* cl) { 419 ZStatTimer timer(ZSubPhasePauseRoots); 420 _threads.oops_do(cl); 421 }