1 /* 2 * Copyright (c) 1997, 2024, 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 25 #include "precompiled.hpp" 26 #include "gc/shared/gc_globals.hpp" 27 #include "logging/log.hpp" 28 #include "logging/logStream.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "memory/universe.hpp" 31 #include "runtime/javaThread.hpp" 32 #include "runtime/mutexLocker.hpp" 33 #include "runtime/safepoint.hpp" 34 #include "runtime/vmThread.hpp" 35 #include "utilities/vmError.hpp" 36 37 // Mutexes used in the VM (see comment in mutexLocker.hpp): 38 39 Mutex* Patching_lock = nullptr; 40 Mutex* NMethodState_lock = nullptr; 41 Monitor* SystemDictionary_lock = nullptr; 42 Mutex* InvokeMethodTypeTable_lock = nullptr; 43 Monitor* InvokeMethodIntrinsicTable_lock = nullptr; 44 Mutex* SharedDictionary_lock = nullptr; 45 Monitor* ClassInitError_lock = nullptr; 46 Mutex* Module_lock = nullptr; 47 Mutex* CompiledIC_lock = nullptr; 48 Mutex* InlineCacheBuffer_lock = nullptr; 49 Mutex* VMStatistic_lock = nullptr; 50 Mutex* JmethodIdCreation_lock = nullptr; 51 Mutex* JfieldIdCreation_lock = nullptr; 52 Monitor* JNICritical_lock = nullptr; 53 Mutex* JvmtiThreadState_lock = nullptr; 54 Monitor* EscapeBarrier_lock = nullptr; 55 Monitor* JvmtiVTMSTransition_lock = nullptr; 56 Monitor* Heap_lock = nullptr; 57 #ifdef INCLUDE_PARALLELGC 58 Mutex* PSOldGenExpand_lock = nullptr; 59 #endif 60 Mutex* AdapterHandlerLibrary_lock = nullptr; 61 Mutex* SignatureHandlerLibrary_lock = nullptr; 62 Mutex* VtableStubs_lock = nullptr; 63 Mutex* SymbolArena_lock = nullptr; 64 Monitor* StringDedup_lock = nullptr; 65 Mutex* StringDedupIntern_lock = nullptr; 66 Monitor* CodeCache_lock = nullptr; 67 Mutex* TouchedMethodLog_lock = nullptr; 68 Mutex* RetData_lock = nullptr; 69 Monitor* VMOperation_lock = nullptr; 70 Monitor* Threads_lock = nullptr; 71 Mutex* NonJavaThreadsList_lock = nullptr; 72 Mutex* NonJavaThreadsListSync_lock = nullptr; 73 Monitor* CGC_lock = nullptr; 74 Monitor* STS_lock = nullptr; 75 Monitor* G1OldGCCount_lock = nullptr; 76 Mutex* G1RareEvent_lock = nullptr; 77 Mutex* G1DetachedRefinementStats_lock = nullptr; 78 Mutex* MarkStackFreeList_lock = nullptr; 79 Mutex* MarkStackChunkList_lock = nullptr; 80 Mutex* MonitoringSupport_lock = nullptr; 81 Monitor* ConcurrentGCBreakpoints_lock = nullptr; 82 Mutex* Compile_lock = nullptr; 83 Monitor* MethodCompileQueue_lock = nullptr; 84 Monitor* CompileThread_lock = nullptr; 85 Monitor* Compilation_lock = nullptr; 86 Mutex* CompileTaskAlloc_lock = nullptr; 87 Mutex* CompileStatistics_lock = nullptr; 88 Mutex* DirectivesStack_lock = nullptr; 89 Monitor* Terminator_lock = nullptr; 90 Monitor* InitCompleted_lock = nullptr; 91 Monitor* BeforeExit_lock = nullptr; 92 Monitor* Notify_lock = nullptr; 93 Mutex* ExceptionCache_lock = nullptr; 94 #ifndef PRODUCT 95 Mutex* FullGCALot_lock = nullptr; 96 #endif 97 98 Mutex* tty_lock = nullptr; 99 100 Mutex* RawMonitor_lock = nullptr; 101 Mutex* PerfDataMemAlloc_lock = nullptr; 102 Mutex* PerfDataManager_lock = nullptr; 103 104 Mutex* FreeList_lock = nullptr; 105 Mutex* OldSets_lock = nullptr; 106 Mutex* Uncommit_lock = nullptr; 107 Monitor* RootRegionScan_lock = nullptr; 108 109 Mutex* Management_lock = nullptr; 110 Monitor* MonitorDeflation_lock = nullptr; 111 Monitor* Service_lock = nullptr; 112 Monitor* Notification_lock = nullptr; 113 Monitor* PeriodicTask_lock = nullptr; 114 Monitor* RedefineClasses_lock = nullptr; 115 Mutex* Verify_lock = nullptr; 116 117 #if INCLUDE_JFR 118 Mutex* JfrStacktrace_lock = nullptr; 119 Monitor* JfrMsg_lock = nullptr; 120 Mutex* JfrBuffer_lock = nullptr; 121 Monitor* JfrThreadSampler_lock = nullptr; 122 #endif 123 124 Mutex* CodeHeapStateAnalytics_lock = nullptr; 125 126 Mutex* ExternalsRecorder_lock = nullptr; 127 128 Monitor* ContinuationRelativize_lock = nullptr; 129 130 Mutex* Metaspace_lock = nullptr; 131 Monitor* MetaspaceCritical_lock = nullptr; 132 Mutex* ClassLoaderDataGraph_lock = nullptr; 133 Monitor* ThreadsSMRDelete_lock = nullptr; 134 Mutex* ThreadIdTableCreate_lock = nullptr; 135 Mutex* SharedDecoder_lock = nullptr; 136 Mutex* DCmdFactory_lock = nullptr; 137 Mutex* NMTQuery_lock = nullptr; 138 Mutex* NMTCompilationCostHistory_lock = nullptr; 139 140 #if INCLUDE_CDS 141 #if INCLUDE_JVMTI 142 Mutex* CDSClassFileStream_lock = nullptr; 143 #endif 144 Mutex* DumpTimeTable_lock = nullptr; 145 Mutex* CDSLambda_lock = nullptr; 146 Mutex* DumpRegion_lock = nullptr; 147 Mutex* ClassListFile_lock = nullptr; 148 Mutex* UnregisteredClassesTable_lock= nullptr; 149 Mutex* LambdaFormInvokers_lock = nullptr; 150 Mutex* ScratchObjects_lock = nullptr; 151 #endif // INCLUDE_CDS 152 Mutex* Bootclasspath_lock = nullptr; 153 154 #if INCLUDE_JVMCI 155 Monitor* JVMCI_lock = nullptr; 156 Monitor* JVMCIRuntime_lock = nullptr; 157 #endif 158 159 // Only one RecursiveMutex 160 RecursiveMutex* MultiArray_lock = nullptr; 161 162 #define MAX_NUM_MUTEX 128 163 static Mutex* _mutex_array[MAX_NUM_MUTEX]; 164 static int _num_mutex; 165 166 #ifdef ASSERT 167 void assert_locked_or_safepoint(const Mutex* lock) { 168 if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return; 169 // check if this thread owns the lock (common case) 170 assert(lock != nullptr, "Need non-null lock"); 171 if (lock->owned_by_self()) return; 172 if (SafepointSynchronize::is_at_safepoint()) return; 173 if (!Universe::is_fully_initialized()) return; 174 fatal("must own lock %s", lock->name()); 175 } 176 177 // a stronger assertion than the above 178 void assert_lock_strong(const Mutex* lock) { 179 if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return; 180 assert(lock != nullptr, "Need non-null lock"); 181 if (lock->owned_by_self()) return; 182 fatal("must own lock %s", lock->name()); 183 } 184 #endif 185 186 static void add_mutex(Mutex* var) { 187 assert(_num_mutex < MAX_NUM_MUTEX, "increase MAX_NUM_MUTEX"); 188 _mutex_array[_num_mutex++] = var; 189 } 190 191 #define MUTEX_STORAGE_NAME(name) name##_storage 192 #define MUTEX_STORAGE(name, type) alignas(type) static uint8_t MUTEX_STORAGE_NAME(name)[sizeof(type)] 193 #define MUTEX_DEF(name, type, pri, ...) { \ 194 assert(name == nullptr, "Mutex/Monitor initialized twice"); \ 195 MUTEX_STORAGE(name, type); \ 196 name = ::new(static_cast<void*>(MUTEX_STORAGE_NAME(name))) type((pri), #name, ##__VA_ARGS__); \ 197 add_mutex(name); \ 198 } 199 #define MUTEX_DEFN(name, type, pri, ...) MUTEX_DEF(name, type, Mutex::pri, ##__VA_ARGS__) 200 201 // Specify relative ranked lock 202 #ifdef ASSERT 203 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEF(name, type, (held_lock)->rank() - 1, ##__VA_ARGS__) 204 #else 205 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEFN(name, type, safepoint, ##__VA_ARGS__) 206 #endif 207 208 // Using Padded subclasses to prevent false sharing of these global monitors and mutexes. 209 void mutex_init() { 210 MUTEX_DEFN(tty_lock , PaddedMutex , tty); // allow to lock in VM 211 212 MUTEX_DEFN(STS_lock , PaddedMonitor, nosafepoint); 213 214 if (UseG1GC) { 215 MUTEX_DEFN(CGC_lock , PaddedMonitor, nosafepoint); 216 217 MUTEX_DEFN(G1DetachedRefinementStats_lock, PaddedMutex , nosafepoint-2); 218 219 MUTEX_DEFN(FreeList_lock , PaddedMutex , service-1); 220 MUTEX_DEFN(OldSets_lock , PaddedMutex , nosafepoint); 221 MUTEX_DEFN(Uncommit_lock , PaddedMutex , service-2); 222 MUTEX_DEFN(RootRegionScan_lock , PaddedMonitor, nosafepoint-1); 223 224 MUTEX_DEFN(MarkStackFreeList_lock , PaddedMutex , nosafepoint); 225 MUTEX_DEFN(MarkStackChunkList_lock , PaddedMutex , nosafepoint); 226 } 227 MUTEX_DEFN(MonitoringSupport_lock , PaddedMutex , service-1); // used for serviceability monitoring support 228 229 MUTEX_DEFN(StringDedup_lock , PaddedMonitor, nosafepoint); 230 MUTEX_DEFN(StringDedupIntern_lock , PaddedMutex , nosafepoint); 231 MUTEX_DEFN(RawMonitor_lock , PaddedMutex , nosafepoint-1); 232 233 MUTEX_DEFN(Metaspace_lock , PaddedMutex , nosafepoint-3); 234 MUTEX_DEFN(MetaspaceCritical_lock , PaddedMonitor, nosafepoint-1); 235 236 MUTEX_DEFN(Patching_lock , PaddedMutex , nosafepoint); // used for safepointing and code patching. 237 MUTEX_DEFN(MonitorDeflation_lock , PaddedMonitor, nosafepoint); // used for monitor deflation thread operations 238 MUTEX_DEFN(Service_lock , PaddedMonitor, service); // used for service thread operations 239 240 if (UseNotificationThread) { 241 MUTEX_DEFN(Notification_lock , PaddedMonitor, service); // used for notification thread operations 242 } else { 243 Notification_lock = Service_lock; 244 } 245 246 MUTEX_DEFN(JmethodIdCreation_lock , PaddedMutex , nosafepoint-2); // used for creating jmethodIDs. 247 MUTEX_DEFN(InvokeMethodTypeTable_lock , PaddedMutex , safepoint); 248 MUTEX_DEFN(InvokeMethodIntrinsicTable_lock , PaddedMonitor, safepoint); 249 MUTEX_DEFN(AdapterHandlerLibrary_lock , PaddedMutex , safepoint); 250 MUTEX_DEFN(SharedDictionary_lock , PaddedMutex , safepoint); 251 MUTEX_DEFN(VMStatistic_lock , PaddedMutex , safepoint); 252 MUTEX_DEFN(SignatureHandlerLibrary_lock , PaddedMutex , safepoint); 253 MUTEX_DEFN(SymbolArena_lock , PaddedMutex , nosafepoint); 254 MUTEX_DEFN(ExceptionCache_lock , PaddedMutex , safepoint); 255 #ifndef PRODUCT 256 MUTEX_DEFN(FullGCALot_lock , PaddedMutex , safepoint); // a lock to make FullGCALot MT safe 257 #endif 258 MUTEX_DEFN(BeforeExit_lock , PaddedMonitor, safepoint); 259 260 MUTEX_DEFN(NonJavaThreadsList_lock , PaddedMutex , nosafepoint-1); 261 MUTEX_DEFN(NonJavaThreadsListSync_lock , PaddedMutex , nosafepoint); 262 263 MUTEX_DEFN(RetData_lock , PaddedMutex , safepoint); 264 MUTEX_DEFN(Terminator_lock , PaddedMonitor, safepoint, true); 265 MUTEX_DEFN(InitCompleted_lock , PaddedMonitor, nosafepoint); 266 MUTEX_DEFN(Notify_lock , PaddedMonitor, safepoint, true); 267 268 MUTEX_DEFN(JfieldIdCreation_lock , PaddedMutex , safepoint); 269 270 MUTEX_DEFN(CompiledIC_lock , PaddedMutex , nosafepoint); // locks VtableStubs_lock, InlineCacheBuffer_lock 271 MUTEX_DEFN(MethodCompileQueue_lock , PaddedMonitor, safepoint); 272 MUTEX_DEFN(CompileStatistics_lock , PaddedMutex , safepoint); 273 MUTEX_DEFN(DirectivesStack_lock , PaddedMutex , nosafepoint); 274 275 MUTEX_DEFN(JvmtiThreadState_lock , PaddedMutex , safepoint); // Used by JvmtiThreadState/JvmtiEventController 276 MUTEX_DEFN(EscapeBarrier_lock , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI 277 MUTEX_DEFN(JvmtiVTMSTransition_lock , PaddedMonitor, safepoint); // used for Virtual Thread Mount State transition management 278 MUTEX_DEFN(Management_lock , PaddedMutex , safepoint); // used for JVM management 279 280 MUTEX_DEFN(ConcurrentGCBreakpoints_lock , PaddedMonitor, safepoint, true); 281 MUTEX_DEFN(TouchedMethodLog_lock , PaddedMutex , safepoint); 282 283 MUTEX_DEFN(CompileThread_lock , PaddedMonitor, safepoint); 284 MUTEX_DEFN(PeriodicTask_lock , PaddedMonitor, safepoint, true); 285 MUTEX_DEFN(RedefineClasses_lock , PaddedMonitor, safepoint); 286 MUTEX_DEFN(Verify_lock , PaddedMutex , safepoint); 287 MUTEX_DEFN(ClassLoaderDataGraph_lock , PaddedMutex , safepoint); 288 289 if (WhiteBoxAPI) { 290 MUTEX_DEFN(Compilation_lock , PaddedMonitor, nosafepoint); 291 } 292 293 #if INCLUDE_JFR 294 MUTEX_DEFN(JfrBuffer_lock , PaddedMutex , event); 295 MUTEX_DEFN(JfrMsg_lock , PaddedMonitor, event); 296 MUTEX_DEFN(JfrStacktrace_lock , PaddedMutex , event); 297 MUTEX_DEFN(JfrThreadSampler_lock , PaddedMonitor, nosafepoint); 298 #endif 299 300 MUTEX_DEFN(ContinuationRelativize_lock , PaddedMonitor, nosafepoint-3); 301 MUTEX_DEFN(CodeHeapStateAnalytics_lock , PaddedMutex , safepoint); 302 MUTEX_DEFN(ThreadsSMRDelete_lock , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock 303 MUTEX_DEFN(ThreadIdTableCreate_lock , PaddedMutex , safepoint); 304 MUTEX_DEFN(SharedDecoder_lock , PaddedMutex , tty-1); 305 MUTEX_DEFN(DCmdFactory_lock , PaddedMutex , nosafepoint); 306 MUTEX_DEFN(NMTQuery_lock , PaddedMutex , safepoint); 307 MUTEX_DEFN(NMTCompilationCostHistory_lock , PaddedMutex , nosafepoint); 308 #if INCLUDE_CDS 309 #if INCLUDE_JVMTI 310 MUTEX_DEFN(CDSClassFileStream_lock , PaddedMutex , safepoint); 311 #endif 312 MUTEX_DEFN(DumpTimeTable_lock , PaddedMutex , nosafepoint); 313 MUTEX_DEFN(CDSLambda_lock , PaddedMutex , nosafepoint); 314 MUTEX_DEFN(DumpRegion_lock , PaddedMutex , nosafepoint); 315 MUTEX_DEFN(ClassListFile_lock , PaddedMutex , nosafepoint); 316 MUTEX_DEFN(UnregisteredClassesTable_lock , PaddedMutex , nosafepoint-1); 317 MUTEX_DEFN(LambdaFormInvokers_lock , PaddedMutex , safepoint); 318 MUTEX_DEFN(ScratchObjects_lock , PaddedMutex , nosafepoint-1); // Holds DumpTimeTable_lock 319 #endif // INCLUDE_CDS 320 MUTEX_DEFN(Bootclasspath_lock , PaddedMutex , nosafepoint); 321 322 #if INCLUDE_JVMCI 323 // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock 324 MUTEX_DEFN(JVMCIRuntime_lock , PaddedMonitor, safepoint, true); 325 #endif 326 327 // These locks have relative rankings, and inherit safepoint checking attributes from that rank. 328 MUTEX_DEFL(InlineCacheBuffer_lock , PaddedMutex , CompiledIC_lock); 329 MUTEX_DEFL(VtableStubs_lock , PaddedMutex , CompiledIC_lock); // Also holds DumpTimeTable_lock 330 MUTEX_DEFL(CodeCache_lock , PaddedMonitor, VtableStubs_lock); 331 MUTEX_DEFL(NMethodState_lock , PaddedMutex , CodeCache_lock); 332 333 // tty_lock is held when printing nmethod and its relocations which use this lock. 334 MUTEX_DEFL(ExternalsRecorder_lock , PaddedMutex , tty_lock); 335 336 MUTEX_DEFL(Threads_lock , PaddedMonitor, CompileThread_lock, true); 337 MUTEX_DEFL(Compile_lock , PaddedMutex , MethodCompileQueue_lock); 338 MUTEX_DEFL(Heap_lock , PaddedMonitor, AdapterHandlerLibrary_lock); 339 340 MUTEX_DEFL(PerfDataMemAlloc_lock , PaddedMutex , Heap_lock); 341 MUTEX_DEFL(PerfDataManager_lock , PaddedMutex , Heap_lock); 342 MUTEX_DEFL(VMOperation_lock , PaddedMonitor, Heap_lock, true); 343 MUTEX_DEFL(ClassInitError_lock , PaddedMonitor, Threads_lock); 344 345 if (UseG1GC) { 346 MUTEX_DEFL(G1OldGCCount_lock , PaddedMonitor, Threads_lock, true); 347 MUTEX_DEFL(G1RareEvent_lock , PaddedMutex , Threads_lock, true); 348 } 349 350 MUTEX_DEFL(CompileTaskAlloc_lock , PaddedMutex , MethodCompileQueue_lock); 351 #ifdef INCLUDE_PARALLELGC 352 if (UseParallelGC) { 353 MUTEX_DEFL(PSOldGenExpand_lock , PaddedMutex , Heap_lock, true); 354 } 355 #endif 356 MUTEX_DEFL(Module_lock , PaddedMutex , ClassLoaderDataGraph_lock); 357 MUTEX_DEFL(SystemDictionary_lock , PaddedMonitor, Module_lock); 358 MUTEX_DEFL(JNICritical_lock , PaddedMonitor, AdapterHandlerLibrary_lock); // used for JNI critical regions 359 #if INCLUDE_JVMCI 360 // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock 361 MUTEX_DEFL(JVMCI_lock , PaddedMonitor, JVMCIRuntime_lock); 362 #endif 363 364 // Allocate RecursiveMutex 365 MultiArray_lock = new RecursiveMutex(); 366 } 367 368 #undef MUTEX_DEFL 369 #undef MUTEX_DEFN 370 #undef MUTEX_DEF 371 #undef MUTEX_STORAGE 372 #undef MUTEX_STORAGE_NAME 373 374 void MutexLockerImpl::post_initialize() { 375 // Print mutex ranks if requested. 376 LogTarget(Info, vmmutex) lt; 377 if (lt.is_enabled()) { 378 ResourceMark rm; 379 LogStream ls(lt); 380 print_lock_ranks(&ls); 381 } 382 } 383 384 GCMutexLocker::GCMutexLocker(Mutex* mutex) { 385 if (SafepointSynchronize::is_at_safepoint()) { 386 _locked = false; 387 } else { 388 _mutex = mutex; 389 _locked = true; 390 _mutex->lock(); 391 } 392 } 393 394 // Print all mutexes/monitors that are currently owned by a thread; called 395 // by fatal error handler. 396 void print_owned_locks_on_error(outputStream* st) { 397 st->print("VM Mutex/Monitor currently owned by a thread: "); 398 bool none = true; 399 for (int i = 0; i < _num_mutex; i++) { 400 // see if it has an owner 401 if (_mutex_array[i]->owner() != nullptr) { 402 if (none) { 403 // print format used by Mutex::print_on_error() 404 st->print_cr(" ([mutex/lock_event])"); 405 none = false; 406 } 407 _mutex_array[i]->print_on_error(st); 408 st->cr(); 409 } 410 } 411 if (none) st->print_cr("None"); 412 } 413 414 void print_lock_ranks(outputStream* st) { 415 st->print_cr("VM Mutex/Monitor ranks: "); 416 417 #ifdef ASSERT 418 // Be extra defensive and figure out the bounds on 419 // ranks right here. This also saves a bit of time 420 // in the #ranks*#mutexes loop below. 421 int min_rank = INT_MAX; 422 int max_rank = INT_MIN; 423 for (int i = 0; i < _num_mutex; i++) { 424 Mutex* m = _mutex_array[i]; 425 int r = (int) m->rank(); 426 if (min_rank > r) min_rank = r; 427 if (max_rank < r) max_rank = r; 428 } 429 430 // Print the listings rank by rank 431 for (int r = min_rank; r <= max_rank; r++) { 432 bool first = true; 433 for (int i = 0; i < _num_mutex; i++) { 434 Mutex* m = _mutex_array[i]; 435 if (r != (int) m->rank()) continue; 436 437 if (first) { 438 st->cr(); 439 st->print_cr("Rank \"%s\":", m->rank_name()); 440 first = false; 441 } 442 st->print_cr(" %s", m->name()); 443 } 444 } 445 #else 446 st->print_cr(" Only known in debug builds."); 447 #endif // ASSERT 448 }