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 } 256 257 ZWeakRootsIterator::~ZWeakRootsIterator() { 258 ZStatTimer timer(ZSubPhasePauseWeakRootsTeardown); 259 } 260 261 void ZWeakRootsIterator::do_jvmti_weak_export(BoolObjectClosure* is_alive, OopClosure* cl) { 262 ZStatTimer timer(ZSubPhasePauseWeakRootsJVMTIWeakExport); 263 JvmtiExport::weak_oops_do(is_alive, cl); 264 } 265 266 void ZWeakRootsIterator::do_jfr_weak(BoolObjectClosure* is_alive, OopClosure* cl) { 267 #if INCLUDE_JFR 268 ZStatTimer timer(ZSubPhasePauseWeakRootsJFRWeak); 269 Jfr::weak_oops_do(is_alive, cl); 270 #endif 271 } 272 273 void ZWeakRootsIterator::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* cl) { 274 ZStatTimer timer(ZSubPhasePauseWeakRoots); 275 _jvmti_weak_export.weak_oops_do(is_alive, cl); 276 _jfr_weak.weak_oops_do(is_alive, cl); 277 } 278 279 void ZWeakRootsIterator::oops_do(OopClosure* cl) { 280 AlwaysTrueClosure always_alive; 281 weak_oops_do(&always_alive, cl); 282 } 283 284 ZConcurrentWeakRootsIterator::ZConcurrentWeakRootsIterator() : 285 _vm_weak_handles_iter(SystemDictionary::vm_weak_oop_storage()), 286 _jni_weak_handles_iter(JNIHandles::weak_global_handles()), 287 _string_table_iter(StringTable::weak_storage()), 288 _vm_weak_handles(this), 289 _jni_weak_handles(this), 290 _string_table(this) { 291 StringTable::reset_dead_counter(); 292 } 293 294 ZConcurrentWeakRootsIterator::~ZConcurrentWeakRootsIterator() { 295 StringTable::finish_dead_counter(); 296 } 297 298 void ZConcurrentWeakRootsIterator::do_vm_weak_handles(OopClosure* cl) { 299 ZStatTimer timer(ZSubPhaseConcurrentWeakRootsVMWeakHandles); 300 _vm_weak_handles_iter.oops_do(cl); 301 } 302 303 void ZConcurrentWeakRootsIterator::do_jni_weak_handles(OopClosure* cl) { 304 ZStatTimer timer(ZSubPhaseConcurrentWeakRootsJNIWeakHandles); 305 _jni_weak_handles_iter.oops_do(cl); 306 } 307 308 class ZStringTableDeadCounterOopClosure : public OopClosure { 309 private: 310 OopClosure* const _cl; 311 size_t _ndead; 312 313 public: 314 ZStringTableDeadCounterOopClosure(OopClosure* cl) : 315 _cl(cl), 316 _ndead(0) {} 317 318 ~ZStringTableDeadCounterOopClosure() { 319 StringTable::inc_dead_counter(_ndead); 320 } 321 322 virtual void do_oop(oop* p) { 323 _cl->do_oop(p); 324 if (*p == NULL) { 325 _ndead++; 326 } 327 } 328 329 virtual void do_oop(narrowOop* p) { 330 ShouldNotReachHere(); 331 } 332 }; 333 334 void ZConcurrentWeakRootsIterator::do_string_table(OopClosure* cl) { 335 ZStatTimer timer(ZSubPhaseConcurrentWeakRootsStringTable); 336 ZStringTableDeadCounterOopClosure counter_cl(cl); 337 _string_table_iter.oops_do(&counter_cl); 338 } 339 340 void ZConcurrentWeakRootsIterator::oops_do(OopClosure* cl) { 341 ZStatTimer timer(ZSubPhaseConcurrentWeakRoots); 342 _vm_weak_handles.oops_do(cl); 343 _jni_weak_handles.oops_do(cl); 344 _string_table.oops_do(cl); 345 } 346 347 ZThreadRootsIterator::ZThreadRootsIterator() : 348 _threads(this) { 349 assert(SafepointSynchronize::is_at_safepoint(), "Should be at safepoint"); 350 ZStatTimer timer(ZSubPhasePauseRootsSetup); 351 Threads::change_thread_claim_parity(); 352 } 353 354 ZThreadRootsIterator::~ZThreadRootsIterator() { 355 ZStatTimer timer(ZSubPhasePauseRootsTeardown); 356 Threads::assert_all_threads_claimed(); 357 } 358 359 void ZThreadRootsIterator::do_threads(OopClosure* cl) { 360 ZStatTimer timer(ZSubPhasePauseRootsThreads); 361 ResourceMark rm; 362 Threads::possibly_parallel_oops_do(true, cl, NULL); 363 } 364 365 void ZThreadRootsIterator::oops_do(OopClosure* cl) { 366 ZStatTimer timer(ZSubPhasePauseRoots); 367 _threads.oops_do(cl); 368 }