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 ZSubPhasePauseRootsVMWeakHandles("Pause Roots VMWeakHandles"); 56 static const ZStatSubPhase ZSubPhasePauseRootsJNIHandles("Pause Roots JNIHandles"); 57 static const ZStatSubPhase ZSubPhasePauseRootsJNIWeakHandles("Pause Roots JNIWeakHandles"); 58 static const ZStatSubPhase ZSubPhasePauseRootsObjectSynchronizer("Pause Roots ObjectSynchronizer"); 59 static const ZStatSubPhase ZSubPhasePauseRootsManagement("Pause Roots Management"); 60 static const ZStatSubPhase ZSubPhasePauseRootsJVMTIExport("Pause Roots JVMTIExport"); 61 static const ZStatSubPhase ZSubPhasePauseRootsJVMTIWeakExport("Pause Roots JVMTIWeakExport"); 62 static const ZStatSubPhase ZSubPhasePauseRootsJFRWeak("Pause Roots JRFWeak"); 63 static const ZStatSubPhase ZSubPhasePauseRootsSystemDictionary("Pause Roots SystemDictionary"); 64 static const ZStatSubPhase ZSubPhasePauseRootsClassLoaderDataGraph("Pause Roots ClassLoaderDataGraph"); 65 static const ZStatSubPhase ZSubPhasePauseRootsThreads("Pause Roots Threads"); 66 static const ZStatSubPhase ZSubPhasePauseRootsCodeCache("Pause Roots CodeCache"); 67 static const ZStatSubPhase ZSubPhasePauseRootsStringTable("Pause Roots StringTable"); 68 69 static const ZStatSubPhase ZSubPhasePauseWeakRootsSetup("Pause Weak Roots Setup"); 70 static const ZStatSubPhase ZSubPhasePauseWeakRoots("Pause Weak Roots"); 71 static const ZStatSubPhase ZSubPhasePauseWeakRootsTeardown("Pause Weak Roots Teardown"); 72 static const ZStatSubPhase ZSubPhasePauseWeakRootsVMWeakHandles("Pause Weak Roots VMWeakHandles"); 73 static const ZStatSubPhase ZSubPhasePauseWeakRootsJNIWeakHandles("Pause Weak Roots JNIWeakHandles"); 74 static const ZStatSubPhase ZSubPhasePauseWeakRootsJVMTIWeakExport("Pause Weak Roots JVMTIWeakExport"); 75 static const ZStatSubPhase ZSubPhasePauseWeakRootsJFRWeak("Pause Weak Roots JFRWeak"); 76 static const ZStatSubPhase ZSubPhasePauseWeakRootsStringTable("Pause Weak Roots StringTable"); 77 78 static const ZStatSubPhase ZSubPhaseConcurrentWeakRoots("Concurrent Weak Roots"); 79 static const ZStatSubPhase ZSubPhaseConcurrentWeakRootsVMWeakHandles("Concurrent Weak Roots VMWeakHandles"); 80 static const ZStatSubPhase ZSubPhaseConcurrentWeakRootsJNIWeakHandles("Concurrent Weak Roots JNIWeakHandles"); 81 static const ZStatSubPhase ZSubPhaseConcurrentWeakRootsStringTable("Concurrent Weak Roots StringTable"); 82 83 template <typename T, void (T::*F)(OopClosure*)> 84 ZSerialOopsDo<T, F>::ZSerialOopsDo(T* iter) : 85 _iter(iter), 86 _claimed(false) {} 87 88 template <typename T, void (T::*F)(OopClosure*)> 89 void ZSerialOopsDo<T, F>::oops_do(OopClosure* cl) { 90 if (!_claimed && Atomic::cmpxchg(true, &_claimed, false) == false) { 91 (_iter->*F)(cl); 92 } 93 } 94 95 template <typename T, void (T::*F)(OopClosure*)> 96 ZParallelOopsDo<T, F>::ZParallelOopsDo(T* iter) : 97 _iter(iter), 98 _completed(false) {} 99 100 template <typename T, void (T::*F)(OopClosure*)> 101 void ZParallelOopsDo<T, F>::oops_do(OopClosure* cl) { 102 if (!_completed) { 103 (_iter->*F)(cl); 104 if (!_completed) { 105 _completed = true; 106 } 107 } 108 } 109 110 template <typename T, void (T::*F)(BoolObjectClosure*, OopClosure*)> 111 ZSerialWeakOopsDo<T, F>::ZSerialWeakOopsDo(T* iter) : 112 _iter(iter), 113 _claimed(false) {} 114 115 template <typename T, void (T::*F)(BoolObjectClosure*, OopClosure*)> 116 void ZSerialWeakOopsDo<T, F>::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* cl) { 117 if (!_claimed && Atomic::cmpxchg(true, &_claimed, false) == false) { 118 (_iter->*F)(is_alive, cl); 119 } 120 } 121 122 template <typename T, void (T::*F)(BoolObjectClosure*, OopClosure*)> 123 ZParallelWeakOopsDo<T, F>::ZParallelWeakOopsDo(T* iter) : 124 _iter(iter), 125 _completed(false) {} 126 127 template <typename T, void (T::*F)(BoolObjectClosure*, OopClosure*)> 128 void ZParallelWeakOopsDo<T, F>::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* cl) { 129 if (!_completed) { 130 (_iter->*F)(is_alive, cl); 131 if (!_completed) { 132 _completed = true; 133 } 134 } 135 } 136 137 ZRootsIterator::ZRootsIterator() : 138 _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()), 139 _jni_handles_iter(JNIHandles::global_handles()), 140 _jni_weak_handles_iter(JNIHandles::weak_global_handles()), 141 _string_table_iter(StringTable::weak_storage()), 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 ClassLoaderDataGraph::clear_claimed_marks(); 160 COMPILER2_PRESENT(DerivedPointerTable::clear()); 161 CodeCache::gc_prologue(); 162 ZNMethodTable::gc_prologue(); 163 } 164 165 ZRootsIterator::~ZRootsIterator() { 166 ZStatTimer timer(ZSubPhasePauseRootsTeardown); 167 ResourceMark rm; 168 ZNMethodTable::gc_epilogue(); 169 CodeCache::gc_epilogue(); 170 JvmtiExport::gc_epilogue(); 171 COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); 172 Threads::assert_all_threads_claimed(); 173 } 174 175 void ZRootsIterator::do_universe(OopClosure* cl) { 176 ZStatTimer timer(ZSubPhasePauseRootsUniverse); 177 Universe::oops_do(cl); 178 } 179 180 void ZRootsIterator::do_vm_weak_handles(OopClosure* cl) { 181 ZStatTimer timer(ZSubPhasePauseRootsVMWeakHandles); 182 _vm_weak_handles_iter.oops_do(cl); 183 } 184 185 void ZRootsIterator::do_jni_handles(OopClosure* cl) { 186 ZStatTimer timer(ZSubPhasePauseRootsJNIHandles); 187 _jni_handles_iter.oops_do(cl); 188 } 189 190 void ZRootsIterator::do_jni_weak_handles(OopClosure* cl) { 191 ZStatTimer timer(ZSubPhasePauseRootsJNIWeakHandles); 192 _jni_weak_handles_iter.oops_do(cl); 193 } 194 195 void ZRootsIterator::do_object_synchronizer(OopClosure* cl) { 196 ZStatTimer timer(ZSubPhasePauseRootsObjectSynchronizer); 197 ObjectSynchronizer::oops_do(cl); 198 } 199 200 void ZRootsIterator::do_management(OopClosure* cl) { 201 ZStatTimer timer(ZSubPhasePauseRootsManagement); 202 Management::oops_do(cl); 203 } 204 205 void ZRootsIterator::do_jvmti_export(OopClosure* cl) { 206 ZStatTimer timer(ZSubPhasePauseRootsJVMTIExport); 207 JvmtiExport::oops_do(cl); 208 } 209 210 void ZRootsIterator::do_jvmti_weak_export(OopClosure* cl) { 211 ZStatTimer timer(ZSubPhasePauseRootsJVMTIWeakExport); 212 AlwaysTrueClosure always_alive; 213 JvmtiExport::weak_oops_do(&always_alive, cl); 214 } 215 216 void ZRootsIterator::do_jfr_weak(OopClosure* cl) { 217 #if INCLUDE_JFR 218 ZStatTimer timer(ZSubPhasePauseRootsJFRWeak); 219 AlwaysTrueClosure always_alive; 220 Jfr::weak_oops_do(&always_alive, cl); 221 #endif 222 } 223 224 void ZRootsIterator::do_system_dictionary(OopClosure* cl) { 225 ZStatTimer timer(ZSubPhasePauseRootsSystemDictionary); 226 SystemDictionary::oops_do(cl); 227 } 228 229 void ZRootsIterator::do_class_loader_data_graph(OopClosure* cl) { 230 ZStatTimer timer(ZSubPhasePauseRootsClassLoaderDataGraph); 231 CLDToOopClosure cld_cl(cl); 232 ClassLoaderDataGraph::cld_do(&cld_cl); 233 } 234 235 class ZRootsIteratorThreadClosure : public ThreadClosure { 236 private: 237 OopClosure* const _cl; 238 239 public: 240 ZRootsIteratorThreadClosure(OopClosure* cl) : 241 _cl(cl) {} 242 243 virtual void do_thread(Thread* thread) { 244 if (thread->is_Java_thread()) { 245 // Update thread local address bad mask 246 ZThreadLocalData::set_address_bad_mask(thread, ZAddressBadMask); 247 } 248 249 // Process thread oops 250 thread->oops_do(_cl, NULL); 251 } 252 }; 253 254 void ZRootsIterator::do_threads(OopClosure* cl) { 255 ZStatTimer timer(ZSubPhasePauseRootsThreads); 256 ResourceMark rm; 257 ZRootsIteratorThreadClosure thread_cl(cl); 258 Threads::possibly_parallel_threads_do(true, &thread_cl); 259 } 260 261 void ZRootsIterator::do_code_cache(OopClosure* cl) { 262 ZStatTimer timer(ZSubPhasePauseRootsCodeCache); 263 ZNMethodTable::oops_do(cl); 264 } 265 266 void ZRootsIterator::do_string_table(OopClosure* cl) { 267 ZStatTimer timer(ZSubPhasePauseRootsStringTable); 268 _string_table_iter.oops_do(cl); 269 } 270 271 void ZRootsIterator::oops_do(OopClosure* cl, bool visit_jvmti_weak_export) { 272 ZStatTimer timer(ZSubPhasePauseRoots); 273 _universe.oops_do(cl); 274 _object_synchronizer.oops_do(cl); 275 _management.oops_do(cl); 276 _jvmti_export.oops_do(cl); 277 _system_dictionary.oops_do(cl); 278 _jni_handles.oops_do(cl); 279 _class_loader_data_graph.oops_do(cl); 280 _threads.oops_do(cl); 281 _code_cache.oops_do(cl); 282 if (!ZWeakRoots) { 283 _jvmti_weak_export.oops_do(cl); 284 _jfr_weak.oops_do(cl); 285 _vm_weak_handles.oops_do(cl); 286 _jni_weak_handles.oops_do(cl); 287 _string_table.oops_do(cl); 288 } else { 289 if (visit_jvmti_weak_export) { 290 _jvmti_weak_export.oops_do(cl); 291 } 292 } 293 } 294 295 ZWeakRootsIterator::ZWeakRootsIterator() : 296 _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()), 297 _jni_weak_handles_iter(JNIHandles::weak_global_handles()), 298 _string_table_iter(StringTable::weak_storage()), 299 _jvmti_weak_export(this), 300 _jfr_weak(this), 301 _vm_weak_handles(this), 302 _jni_weak_handles(this), 303 _string_table(this) { 304 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 305 ZStatTimer timer(ZSubPhasePauseWeakRootsSetup); 306 StringTable::reset_dead_counter(); 307 } 308 309 ZWeakRootsIterator::~ZWeakRootsIterator() { 310 ZStatTimer timer(ZSubPhasePauseWeakRootsTeardown); 311 StringTable::finish_dead_counter(); 312 } 313 314 void ZWeakRootsIterator::do_vm_weak_handles(BoolObjectClosure* is_alive, OopClosure* cl) { 315 ZStatTimer timer(ZSubPhasePauseWeakRootsVMWeakHandles); 316 _vm_weak_handles_iter.weak_oops_do(is_alive, cl); 317 } 318 319 void ZWeakRootsIterator::do_jni_weak_handles(BoolObjectClosure* is_alive, OopClosure* cl) { 320 ZStatTimer timer(ZSubPhasePauseWeakRootsJNIWeakHandles); 321 _jni_weak_handles_iter.weak_oops_do(is_alive, cl); 322 } 323 324 void ZWeakRootsIterator::do_jvmti_weak_export(BoolObjectClosure* is_alive, OopClosure* cl) { 325 ZStatTimer timer(ZSubPhasePauseWeakRootsJVMTIWeakExport); 326 JvmtiExport::weak_oops_do(is_alive, cl); 327 } 328 329 void ZWeakRootsIterator::do_jfr_weak(BoolObjectClosure* is_alive, OopClosure* cl) { 330 #if INCLUDE_JFR 331 ZStatTimer timer(ZSubPhasePauseWeakRootsJFRWeak); 332 Jfr::weak_oops_do(is_alive, cl); 333 #endif 334 } 335 336 class ZStringTableDeadCounterBoolObjectClosure : public BoolObjectClosure { 337 private: 338 BoolObjectClosure* const _cl; 339 size_t _ndead; 340 341 public: 342 ZStringTableDeadCounterBoolObjectClosure(BoolObjectClosure* cl) : 343 _cl(cl), 344 _ndead(0) {} 345 346 ~ZStringTableDeadCounterBoolObjectClosure() { 347 StringTable::inc_dead_counter(_ndead); 348 } 349 350 virtual bool do_object_b(oop obj) { 351 if (_cl->do_object_b(obj)) { 352 return true; 353 } 354 355 _ndead++; 356 return false; 357 } 358 }; 359 360 void ZWeakRootsIterator::do_string_table(BoolObjectClosure* is_alive, OopClosure* cl) { 361 ZStatTimer timer(ZSubPhasePauseWeakRootsStringTable); 362 ZStringTableDeadCounterBoolObjectClosure counter_is_alive(is_alive); 363 _string_table_iter.weak_oops_do(&counter_is_alive, cl); 364 } 365 366 void ZWeakRootsIterator::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* cl) { 367 ZStatTimer timer(ZSubPhasePauseWeakRoots); 368 if (ZWeakRoots) { 369 _jvmti_weak_export.weak_oops_do(is_alive, cl); 370 _jfr_weak.weak_oops_do(is_alive, cl); 371 if (!ZConcurrentVMWeakHandles) { 372 _vm_weak_handles.weak_oops_do(is_alive, cl); 373 } 374 if (!ZConcurrentJNIWeakGlobalHandles) { 375 _jni_weak_handles.weak_oops_do(is_alive, cl); 376 } 377 if (!ZConcurrentStringTable) { 378 _string_table.weak_oops_do(is_alive, cl); 379 } 380 } 381 } 382 383 void ZWeakRootsIterator::oops_do(OopClosure* cl) { 384 AlwaysTrueClosure always_alive; 385 weak_oops_do(&always_alive, cl); 386 } 387 388 ZConcurrentWeakRootsIterator::ZConcurrentWeakRootsIterator() : 389 _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()), 390 _jni_weak_handles_iter(JNIHandles::weak_global_handles()), 391 _string_table_iter(StringTable::weak_storage()), 392 _vm_weak_handles(this), 393 _jni_weak_handles(this), 394 _string_table(this) { 395 StringTable::reset_dead_counter(); 396 } 397 398 ZConcurrentWeakRootsIterator::~ZConcurrentWeakRootsIterator() { 399 StringTable::finish_dead_counter(); 400 } 401 402 void ZConcurrentWeakRootsIterator::do_vm_weak_handles(OopClosure* cl) { 403 ZStatTimer timer(ZSubPhaseConcurrentWeakRootsVMWeakHandles); 404 _vm_weak_handles_iter.oops_do(cl); 405 } 406 407 void ZConcurrentWeakRootsIterator::do_jni_weak_handles(OopClosure* cl) { 408 ZStatTimer timer(ZSubPhaseConcurrentWeakRootsJNIWeakHandles); 409 _jni_weak_handles_iter.oops_do(cl); 410 } 411 412 class ZStringTableDeadCounterOopClosure : public OopClosure { 413 private: 414 OopClosure* const _cl; 415 size_t _ndead; 416 417 public: 418 ZStringTableDeadCounterOopClosure(OopClosure* cl) : 419 _cl(cl), 420 _ndead(0) {} 421 422 ~ZStringTableDeadCounterOopClosure() { 423 StringTable::inc_dead_counter(_ndead); 424 } 425 426 virtual void do_oop(oop* p) { 427 _cl->do_oop(p); 428 if (*p == NULL) { 429 _ndead++; 430 } 431 } 432 433 virtual void do_oop(narrowOop* p) { 434 ShouldNotReachHere(); 435 } 436 }; 437 438 void ZConcurrentWeakRootsIterator::do_string_table(OopClosure* cl) { 439 ZStatTimer timer(ZSubPhaseConcurrentWeakRootsStringTable); 440 ZStringTableDeadCounterOopClosure counter_cl(cl); 441 _string_table_iter.oops_do(&counter_cl); 442 } 443 444 void ZConcurrentWeakRootsIterator::oops_do(OopClosure* cl) { 445 ZStatTimer timer(ZSubPhaseConcurrentWeakRoots); 446 if (ZWeakRoots) { 447 if (ZConcurrentVMWeakHandles) { 448 _vm_weak_handles.oops_do(cl); 449 } 450 if (ZConcurrentJNIWeakGlobalHandles) { 451 _jni_weak_handles.oops_do(cl); 452 } 453 if (ZConcurrentStringTable) { 454 _string_table.oops_do(cl); 455 } 456 } 457 } 458 459 ZThreadRootsIterator::ZThreadRootsIterator() : 460 _threads(this) { 461 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 462 ZStatTimer timer(ZSubPhasePauseRootsSetup); 463 Threads::change_thread_claim_parity(); 464 } 465 466 ZThreadRootsIterator::~ZThreadRootsIterator() { 467 ZStatTimer timer(ZSubPhasePauseRootsTeardown); 468 Threads::assert_all_threads_claimed(); 469 } 470 471 void ZThreadRootsIterator::do_threads(OopClosure* cl) { 472 ZStatTimer timer(ZSubPhasePauseRootsThreads); 473 ResourceMark rm; 474 Threads::possibly_parallel_oops_do(true, cl, NULL); 475 } 476 477 void ZThreadRootsIterator::oops_do(OopClosure* cl) { 478 ZStatTimer timer(ZSubPhasePauseRoots); 479 _threads.oops_do(cl); 480 }