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