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 "compiler/compiler_globals.hpp" 27 #include "gc/shared/gc_globals.hpp" 28 #include "logging/log.hpp" 29 #include "logging/logStream.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "memory/universe.hpp" 32 #include "runtime/java.hpp" 33 #include "runtime/javaThread.hpp" 34 #include "runtime/mutexLocker.hpp" 35 #include "runtime/safepoint.hpp" 36 #include "runtime/vmThread.hpp" 37 #include "services/management.hpp" 38 #include "utilities/vmError.hpp" 39 40 // Mutexes used in the VM (see comment in mutexLocker.hpp): 41 42 Mutex* Patching_lock = nullptr; 43 Mutex* NMethodState_lock = nullptr; 44 Monitor* SystemDictionary_lock = nullptr; 45 Mutex* InvokeMethodTypeTable_lock = nullptr; 46 Monitor* InvokeMethodIntrinsicTable_lock = nullptr; 47 Mutex* SharedDictionary_lock = nullptr; 48 Monitor* ClassInitError_lock = nullptr; 49 Mutex* Module_lock = nullptr; 50 Mutex* CompiledIC_lock = nullptr; 51 Mutex* InlineCacheBuffer_lock = nullptr; 52 Mutex* VMStatistic_lock = nullptr; 53 Mutex* JmethodIdCreation_lock = nullptr; 54 Mutex* JfieldIdCreation_lock = nullptr; 55 Monitor* JNICritical_lock = nullptr; 56 Mutex* JvmtiThreadState_lock = nullptr; 57 Monitor* EscapeBarrier_lock = nullptr; 58 Monitor* JvmtiVTMSTransition_lock = nullptr; 59 Monitor* Heap_lock = nullptr; 60 #ifdef INCLUDE_PARALLELGC 61 Mutex* PSOldGenExpand_lock = nullptr; 62 #endif 63 Mutex* AdapterHandlerLibrary_lock = nullptr; 64 Mutex* SignatureHandlerLibrary_lock = nullptr; 65 Mutex* VtableStubs_lock = nullptr; 66 Mutex* SymbolArena_lock = nullptr; 67 Monitor* StringDedup_lock = nullptr; 68 Mutex* StringDedupIntern_lock = nullptr; 69 Monitor* CodeCache_lock = nullptr; 70 Mutex* TouchedMethodLog_lock = nullptr; 71 Mutex* RetData_lock = nullptr; 72 Monitor* VMOperation_lock = nullptr; 73 Monitor* Threads_lock = nullptr; 74 Mutex* NonJavaThreadsList_lock = nullptr; 75 Mutex* NonJavaThreadsListSync_lock = nullptr; 76 Monitor* CGC_lock = nullptr; 77 Monitor* STS_lock = nullptr; 78 Monitor* G1OldGCCount_lock = nullptr; 79 Mutex* G1RareEvent_lock = nullptr; 80 Mutex* G1DetachedRefinementStats_lock = nullptr; 81 Mutex* MarkStackFreeList_lock = nullptr; 82 Mutex* MarkStackChunkList_lock = nullptr; 83 Mutex* MonitoringSupport_lock = nullptr; 84 Monitor* ConcurrentGCBreakpoints_lock = nullptr; 85 Mutex* Compile_lock = nullptr; 86 Monitor* MethodCompileQueue_lock = nullptr; 87 Monitor* MethodCompileQueueC1_lock = nullptr; 88 Monitor* MethodCompileQueueC2_lock = nullptr; 89 Monitor* MethodCompileQueueC3_lock = nullptr; 90 Monitor* MethodCompileQueueSC1_lock = nullptr; 91 Monitor* MethodCompileQueueSC2_lock = nullptr; 92 Monitor* CompileThread_lock = nullptr; 93 Monitor* Compilation_lock = nullptr; 94 Mutex* CompileTaskAlloc_lock = nullptr; 95 Mutex* CompileStatistics_lock = nullptr; 96 Mutex* DirectivesStack_lock = nullptr; 97 Monitor* Terminator_lock = nullptr; 98 Monitor* InitCompleted_lock = nullptr; 99 Monitor* BeforeExit_lock = nullptr; 100 Monitor* Notify_lock = nullptr; 101 Mutex* ExceptionCache_lock = nullptr; 102 Mutex* TrainingData_lock = nullptr; 103 Monitor* TrainingReplayQueue_lock = nullptr; 104 #ifndef PRODUCT 105 Mutex* FullGCALot_lock = nullptr; 106 #endif 107 108 Mutex* tty_lock = nullptr; 109 110 Mutex* RawMonitor_lock = nullptr; 111 Mutex* PerfDataMemAlloc_lock = nullptr; 112 Mutex* PerfDataManager_lock = nullptr; 113 114 Mutex* FreeList_lock = nullptr; 115 Mutex* OldSets_lock = nullptr; 116 Mutex* Uncommit_lock = nullptr; 117 Monitor* RootRegionScan_lock = nullptr; 118 119 Mutex* Management_lock = nullptr; 120 Monitor* MonitorDeflation_lock = nullptr; 121 Monitor* Service_lock = nullptr; 122 Monitor* Notification_lock = nullptr; 123 Monitor* PeriodicTask_lock = nullptr; 124 Monitor* RedefineClasses_lock = nullptr; 125 Mutex* Verify_lock = nullptr; 126 127 #if INCLUDE_JFR 128 Mutex* JfrStacktrace_lock = nullptr; 129 Monitor* JfrMsg_lock = nullptr; 130 Mutex* JfrBuffer_lock = nullptr; 131 Monitor* JfrThreadSampler_lock = nullptr; 132 #endif 133 134 Mutex* CodeHeapStateAnalytics_lock = nullptr; 135 136 Mutex* ExternalsRecorder_lock = nullptr; 137 138 Monitor* ContinuationRelativize_lock = nullptr; 139 140 Mutex* Metaspace_lock = nullptr; 141 Monitor* MetaspaceCritical_lock = nullptr; 142 Mutex* ClassLoaderDataGraph_lock = nullptr; 143 Monitor* ThreadsSMRDelete_lock = nullptr; 144 Mutex* ThreadIdTableCreate_lock = nullptr; 145 Mutex* SharedDecoder_lock = nullptr; 146 Mutex* DCmdFactory_lock = nullptr; 147 Mutex* NMTQuery_lock = nullptr; 148 Mutex* NMTCompilationCostHistory_lock = nullptr; 149 150 #if INCLUDE_CDS 151 #if INCLUDE_JVMTI 152 Mutex* CDSClassFileStream_lock = nullptr; 153 #endif 154 Mutex* DumpTimeTable_lock = nullptr; 155 Mutex* CDSLambda_lock = nullptr; 156 Mutex* DumpRegion_lock = nullptr; 157 Mutex* ClassListFile_lock = nullptr; 158 Mutex* UnregisteredClassesTable_lock= nullptr; 159 Mutex* LambdaFormInvokers_lock = nullptr; 160 Mutex* ScratchObjects_lock = nullptr; 161 Mutex* ArchivedObjectTables_lock = nullptr; 162 #endif // INCLUDE_CDS 163 Mutex* Bootclasspath_lock = nullptr; 164 165 #if INCLUDE_JVMCI 166 Monitor* JVMCI_lock = nullptr; 167 Monitor* JVMCIRuntime_lock = nullptr; 168 #endif 169 170 // Only one RecursiveMutex 171 RecursiveMutex* MultiArray_lock = nullptr; 172 173 #define MAX_NUM_MUTEX 128 174 static Mutex* _mutex_array[MAX_NUM_MUTEX]; 175 static int _num_mutex; 176 177 #ifdef ASSERT 178 void assert_locked_or_safepoint(const Mutex* lock) { 179 if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return; 180 // check if this thread owns the lock (common case) 181 assert(lock != nullptr, "Need non-null lock"); 182 if (lock->owned_by_self()) return; 183 if (SafepointSynchronize::is_at_safepoint()) return; 184 if (!Universe::is_fully_initialized()) return; 185 fatal("must own lock %s", lock->name()); 186 } 187 188 // a stronger assertion than the above 189 void assert_lock_strong(const Mutex* lock) { 190 if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return; 191 assert(lock != nullptr, "Need non-null lock"); 192 if (lock->owned_by_self()) return; 193 fatal("must own lock %s", lock->name()); 194 } 195 #endif 196 197 static void add_mutex(Mutex* var) { 198 assert(_num_mutex < MAX_NUM_MUTEX, "increase MAX_NUM_MUTEX"); 199 int id = _num_mutex++; 200 _mutex_array[id] = var; 201 // var->set_id(id); 202 } 203 204 #define MUTEX_STORAGE_NAME(name) name##_storage 205 #define MUTEX_STORAGE(name, type) alignas(type) static uint8_t MUTEX_STORAGE_NAME(name)[sizeof(type)] 206 #define MUTEX_DEF(name, type, pri, ...) { \ 207 assert(name == nullptr, "Mutex/Monitor initialized twice"); \ 208 MUTEX_STORAGE(name, type); \ 209 name = ::new(static_cast<void*>(MUTEX_STORAGE_NAME(name))) type((pri), #name, ##__VA_ARGS__); \ 210 add_mutex(name); \ 211 } 212 #define MUTEX_DEFN(name, type, pri, ...) MUTEX_DEF(name, type, Mutex::pri, ##__VA_ARGS__) 213 214 // Specify relative ranked lock 215 #ifdef ASSERT 216 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEF(name, type, (held_lock)->rank() - 1, ##__VA_ARGS__) 217 #else 218 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEFN(name, type, safepoint, ##__VA_ARGS__) 219 #endif 220 221 // Using Padded subclasses to prevent false sharing of these global monitors and mutexes. 222 void mutex_init() { 223 MUTEX_DEFN(tty_lock , PaddedMutex , tty); // allow to lock in VM 224 225 MUTEX_DEFN(STS_lock , PaddedMonitor, nosafepoint); 226 227 if (UseG1GC) { 228 MUTEX_DEFN(CGC_lock , PaddedMonitor, nosafepoint); 229 230 MUTEX_DEFN(G1DetachedRefinementStats_lock, PaddedMutex , nosafepoint-2); 231 232 MUTEX_DEFN(FreeList_lock , PaddedMutex , service-1); 233 MUTEX_DEFN(OldSets_lock , PaddedMutex , nosafepoint); 234 MUTEX_DEFN(Uncommit_lock , PaddedMutex , service-2); 235 MUTEX_DEFN(RootRegionScan_lock , PaddedMonitor, nosafepoint-1); 236 237 MUTEX_DEFN(MarkStackFreeList_lock , PaddedMutex , nosafepoint); 238 MUTEX_DEFN(MarkStackChunkList_lock , PaddedMutex , nosafepoint); 239 } 240 MUTEX_DEFN(MonitoringSupport_lock , PaddedMutex , service-1); // used for serviceability monitoring support 241 242 MUTEX_DEFN(StringDedup_lock , PaddedMonitor, nosafepoint); 243 MUTEX_DEFN(StringDedupIntern_lock , PaddedMutex , nosafepoint); 244 MUTEX_DEFN(RawMonitor_lock , PaddedMutex , nosafepoint-1); 245 246 MUTEX_DEFN(Metaspace_lock , PaddedMutex , nosafepoint-3); 247 MUTEX_DEFN(MetaspaceCritical_lock , PaddedMonitor, nosafepoint-1); 248 249 MUTEX_DEFN(Patching_lock , PaddedMutex , nosafepoint); // used for safepointing and code patching. 250 MUTEX_DEFN(MonitorDeflation_lock , PaddedMonitor, nosafepoint); // used for monitor deflation thread operations 251 MUTEX_DEFN(Service_lock , PaddedMonitor, service); // used for service thread operations 252 253 if (UseNotificationThread) { 254 MUTEX_DEFN(Notification_lock , PaddedMonitor, service); // used for notification thread operations 255 } else { 256 Notification_lock = Service_lock; 257 } 258 259 MUTEX_DEFN(JmethodIdCreation_lock , PaddedMutex , nosafepoint-2); // used for creating jmethodIDs. 260 MUTEX_DEFN(InvokeMethodTypeTable_lock , PaddedMutex , safepoint); 261 MUTEX_DEFN(InvokeMethodIntrinsicTable_lock , PaddedMonitor, safepoint); 262 MUTEX_DEFN(AdapterHandlerLibrary_lock , PaddedMutex , safepoint); 263 MUTEX_DEFN(SharedDictionary_lock , PaddedMutex , safepoint); 264 MUTEX_DEFN(VMStatistic_lock , PaddedMutex , safepoint); 265 MUTEX_DEFN(SignatureHandlerLibrary_lock , PaddedMutex , safepoint); 266 MUTEX_DEFN(SymbolArena_lock , PaddedMutex , nosafepoint); 267 MUTEX_DEFN(ExceptionCache_lock , PaddedMutex , safepoint); 268 #ifndef PRODUCT 269 MUTEX_DEFN(FullGCALot_lock , PaddedMutex , safepoint); // a lock to make FullGCALot MT safe 270 #endif 271 MUTEX_DEFN(BeforeExit_lock , PaddedMonitor, safepoint); 272 273 MUTEX_DEFN(NonJavaThreadsList_lock , PaddedMutex , nosafepoint-1); 274 MUTEX_DEFN(NonJavaThreadsListSync_lock , PaddedMutex , nosafepoint); 275 276 MUTEX_DEFN(RetData_lock , PaddedMutex , safepoint); 277 MUTEX_DEFN(Terminator_lock , PaddedMonitor, safepoint, true); 278 MUTEX_DEFN(InitCompleted_lock , PaddedMonitor, nosafepoint); 279 MUTEX_DEFN(Notify_lock , PaddedMonitor, safepoint, true); 280 281 MUTEX_DEFN(JfieldIdCreation_lock , PaddedMutex , safepoint); 282 283 MUTEX_DEFN(CompiledIC_lock , PaddedMutex , nosafepoint); // locks VtableStubs_lock, InlineCacheBuffer_lock 284 MUTEX_DEFN(MethodCompileQueue_lock , PaddedMonitor, safepoint); 285 if (UseGlobalCompileQueueLock) { 286 MethodCompileQueueC1_lock = MethodCompileQueue_lock; 287 MethodCompileQueueC2_lock = MethodCompileQueue_lock; 288 MethodCompileQueueC3_lock = MethodCompileQueue_lock; 289 MethodCompileQueueSC1_lock = MethodCompileQueue_lock; 290 MethodCompileQueueSC2_lock = MethodCompileQueue_lock; 291 } else { 292 MUTEX_DEFN(MethodCompileQueueC1_lock , PaddedMonitor, safepoint); 293 MUTEX_DEFN(MethodCompileQueueC2_lock , PaddedMonitor, safepoint); 294 MUTEX_DEFN(MethodCompileQueueC3_lock , PaddedMonitor, safepoint); 295 MUTEX_DEFN(MethodCompileQueueSC1_lock , PaddedMonitor, safepoint); 296 MUTEX_DEFN(MethodCompileQueueSC2_lock , PaddedMonitor, safepoint); 297 } 298 MUTEX_DEFL(TrainingData_lock , PaddedMutex , MethodCompileQueue_lock); 299 MUTEX_DEFN(TrainingReplayQueue_lock , PaddedMonitor, safepoint); 300 MUTEX_DEFN(CompileStatistics_lock , PaddedMutex , safepoint); 301 MUTEX_DEFN(DirectivesStack_lock , PaddedMutex , nosafepoint); 302 303 MUTEX_DEFN(JvmtiThreadState_lock , PaddedMutex , safepoint); // Used by JvmtiThreadState/JvmtiEventController 304 MUTEX_DEFN(EscapeBarrier_lock , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI 305 MUTEX_DEFN(JvmtiVTMSTransition_lock , PaddedMonitor, safepoint); // used for Virtual Thread Mount State transition management 306 MUTEX_DEFN(Management_lock , PaddedMutex , safepoint); // used for JVM management 307 308 MUTEX_DEFN(ConcurrentGCBreakpoints_lock , PaddedMonitor, safepoint, true); 309 MUTEX_DEFN(TouchedMethodLog_lock , PaddedMutex , safepoint); 310 311 MUTEX_DEFN(CompileThread_lock , PaddedMonitor, safepoint); 312 MUTEX_DEFN(PeriodicTask_lock , PaddedMonitor, safepoint, true); 313 MUTEX_DEFN(RedefineClasses_lock , PaddedMonitor, safepoint); 314 MUTEX_DEFN(Verify_lock , PaddedMutex , safepoint); 315 MUTEX_DEFN(ClassLoaderDataGraph_lock , PaddedMutex , safepoint); 316 317 MUTEX_DEFN(Compilation_lock , PaddedMonitor, nosafepoint); 318 319 #if INCLUDE_JFR 320 MUTEX_DEFN(JfrBuffer_lock , PaddedMutex , event); 321 MUTEX_DEFN(JfrMsg_lock , PaddedMonitor, event); 322 MUTEX_DEFN(JfrStacktrace_lock , PaddedMutex , event); 323 MUTEX_DEFN(JfrThreadSampler_lock , PaddedMonitor, nosafepoint); 324 #endif 325 326 MUTEX_DEFN(ContinuationRelativize_lock , PaddedMonitor, nosafepoint-3); 327 MUTEX_DEFN(CodeHeapStateAnalytics_lock , PaddedMutex , safepoint); 328 MUTEX_DEFN(ThreadsSMRDelete_lock , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock 329 MUTEX_DEFN(ThreadIdTableCreate_lock , PaddedMutex , safepoint); 330 MUTEX_DEFN(SharedDecoder_lock , PaddedMutex , tty-1); 331 MUTEX_DEFN(DCmdFactory_lock , PaddedMutex , nosafepoint); 332 MUTEX_DEFN(NMTQuery_lock , PaddedMutex , safepoint); 333 MUTEX_DEFN(NMTCompilationCostHistory_lock , PaddedMutex , nosafepoint); 334 #if INCLUDE_CDS 335 #if INCLUDE_JVMTI 336 MUTEX_DEFN(CDSClassFileStream_lock , PaddedMutex , safepoint); 337 #endif 338 MUTEX_DEFN(DumpTimeTable_lock , PaddedMutex , nosafepoint); 339 MUTEX_DEFN(CDSLambda_lock , PaddedMutex , nosafepoint); 340 MUTEX_DEFN(DumpRegion_lock , PaddedMutex , nosafepoint); 341 MUTEX_DEFN(ClassListFile_lock , PaddedMutex , nosafepoint); 342 MUTEX_DEFN(UnregisteredClassesTable_lock , PaddedMutex , nosafepoint-1); 343 MUTEX_DEFN(LambdaFormInvokers_lock , PaddedMutex , safepoint); 344 MUTEX_DEFN(ScratchObjects_lock , PaddedMutex , nosafepoint-1); // Holds DumpTimeTable_lock 345 MUTEX_DEFN(ArchivedObjectTables_lock , PaddedMutex , nosafepoint); 346 #endif // INCLUDE_CDS 347 MUTEX_DEFN(Bootclasspath_lock , PaddedMutex , nosafepoint); 348 349 #if INCLUDE_JVMCI 350 // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock 351 MUTEX_DEFN(JVMCIRuntime_lock , PaddedMonitor, safepoint, true); 352 #endif 353 354 // These locks have relative rankings, and inherit safepoint checking attributes from that rank. 355 MUTEX_DEFL(InlineCacheBuffer_lock , PaddedMutex , CompiledIC_lock); 356 MUTEX_DEFL(VtableStubs_lock , PaddedMutex , CompiledIC_lock); // Also holds DumpTimeTable_lock 357 MUTEX_DEFL(CodeCache_lock , PaddedMonitor, VtableStubs_lock); 358 MUTEX_DEFL(NMethodState_lock , PaddedMutex , CodeCache_lock); 359 360 // tty_lock is held when printing nmethod and its relocations which use this lock. 361 MUTEX_DEFL(ExternalsRecorder_lock , PaddedMutex , tty_lock); 362 363 MUTEX_DEFL(Threads_lock , PaddedMonitor, CompileThread_lock, true); 364 MUTEX_DEFL(Compile_lock , PaddedMutex , MethodCompileQueue_lock); 365 MUTEX_DEFL(Heap_lock , PaddedMonitor, TrainingData_lock /*AdapterHandlerLibrary_lock*/); 366 367 MUTEX_DEFL(PerfDataMemAlloc_lock , PaddedMutex , Heap_lock); 368 MUTEX_DEFL(PerfDataManager_lock , PaddedMutex , Heap_lock); 369 MUTEX_DEFL(VMOperation_lock , PaddedMonitor, Heap_lock, true); 370 MUTEX_DEFL(ClassInitError_lock , PaddedMonitor, Threads_lock); 371 372 if (UseG1GC) { 373 MUTEX_DEFL(G1OldGCCount_lock , PaddedMonitor, Threads_lock, true); 374 MUTEX_DEFL(G1RareEvent_lock , PaddedMutex , Threads_lock, true); 375 } 376 377 MUTEX_DEFL(CompileTaskAlloc_lock , PaddedMutex , MethodCompileQueue_lock); 378 #ifdef INCLUDE_PARALLELGC 379 if (UseParallelGC) { 380 MUTEX_DEFL(PSOldGenExpand_lock , PaddedMutex , Heap_lock, true); 381 } 382 #endif 383 MUTEX_DEFL(Module_lock , PaddedMutex , ClassLoaderDataGraph_lock); 384 MUTEX_DEFL(SystemDictionary_lock , PaddedMonitor, Module_lock); 385 MUTEX_DEFL(JNICritical_lock , PaddedMonitor, AdapterHandlerLibrary_lock); // used for JNI critical regions 386 #if INCLUDE_JVMCI 387 // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock 388 MUTEX_DEFL(JVMCI_lock , PaddedMonitor, JVMCIRuntime_lock); 389 #endif 390 391 // Allocate RecursiveMutex 392 MultiArray_lock = new RecursiveMutex(); 393 } 394 395 #undef MUTEX_DEFL 396 #undef MUTEX_DEFN 397 #undef MUTEX_DEF 398 #undef MUTEX_STORAGE 399 #undef MUTEX_STORAGE_NAME 400 401 static const int MAX_NAMES = 200; 402 static const char* _names[MAX_NAMES] = { nullptr }; 403 static bool _is_unique[MAX_NAMES] = { false }; 404 static int _num_names = 0; 405 406 PerfCounter** MutexLockerImpl::_perf_lock_count = nullptr; 407 PerfCounter** MutexLockerImpl::_perf_lock_wait_time = nullptr; 408 PerfCounter** MutexLockerImpl::_perf_lock_hold_time = nullptr; 409 410 void MutexLockerImpl::init_counters() { 411 if (ProfileVMLocks && UsePerfData) { 412 ResourceMark rm; 413 EXCEPTION_MARK; 414 _perf_lock_count = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal); 415 _perf_lock_wait_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal); 416 _perf_lock_hold_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal); 417 418 NEWPERFEVENTCOUNTER(_perf_lock_count[0], SUN_RT, PerfDataManager::counter_name("Other", "Count")); 419 NEWPERFEVENTCOUNTER(_perf_lock_wait_time[0], SUN_RT, PerfDataManager::counter_name("Other", "BeforeTime")); 420 NEWPERFEVENTCOUNTER(_perf_lock_hold_time[0], SUN_RT, PerfDataManager::counter_name("Other", "AfterTime")); 421 for (int i = 0; i < MAX_NAMES; i++) { 422 ResourceMark rm; 423 const char* counter_name = _names[i]; 424 if (counter_name == nullptr) { 425 stringStream ss; 426 ss.print("UnnamedMutex#%d", i); 427 counter_name = ss.as_string(); 428 } 429 NEWPERFEVENTCOUNTER(_perf_lock_count[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "Count")); 430 NEWPERFEVENTCOUNTER(_perf_lock_wait_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "BeforeTime")); 431 NEWPERFEVENTCOUNTER(_perf_lock_hold_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "AfterTime")); 432 } 433 if (HAS_PENDING_EXCEPTION) { 434 vm_exit_during_initialization("MutexLockerImpl::init_counters() failed unexpectedly"); 435 } 436 } 437 } 438 439 int MutexLockerImpl::name2id(const char* name) { 440 if (ProfileVMLocks && UsePerfData) { 441 for (int i = 0; i < _num_names; i++) { 442 if (strcmp(_names[i], name) == 0) { 443 _is_unique[i] = false; 444 return i; 445 } 446 } 447 if (_num_names < MAX_NAMES) { 448 int new_id = _num_names++; 449 _names[new_id] = os::strdup(name, mtInternal); 450 _is_unique[new_id] = true; 451 return new_id; 452 } 453 log_debug(init)("Unnamed: %s", name); // no slots left 454 } 455 return -1; 456 } 457 458 void MutexLockerImpl::print_counter_on(outputStream* st, const char* name, bool is_unique, int idx) { 459 jlong count = _perf_lock_count[idx]->get_value(); 460 if (count > 0) { 461 st->print_cr(" %3d: %s%40s = %5ldms (%5ldms) / %9ld events", idx, (is_unique ? " " : "M"), name, 462 Management::ticks_to_ms(_perf_lock_hold_time[idx]->get_value()), 463 Management::ticks_to_ms(_perf_lock_wait_time[idx]->get_value()), 464 count); 465 } 466 } 467 468 static jlong accumulate_lock_counters(PerfCounter** lock_counters) { 469 jlong acc = 0; 470 for (int i = 0; i < _num_names + 1; i++) { // 0 slot is reserved for unnamed locks 471 if (lock_counters[i] == nullptr) { 472 break; 473 } 474 acc += lock_counters[i]->get_value(); 475 } 476 return acc; 477 } 478 479 void MutexLockerImpl::print_counters_on(outputStream* st) { 480 if (ProfileVMLocks && UsePerfData) { 481 jlong total_count = accumulate_lock_counters(_perf_lock_count); 482 jlong total_wait_time = accumulate_lock_counters(_perf_lock_wait_time); 483 jlong total_hold_time = accumulate_lock_counters(_perf_lock_hold_time); 484 485 st->print_cr("MutexLocker: Total: %d named locks (%d unique names); hold = %ldms (wait = %ldms) / %ld events for thread \"main\"", 486 _num_mutex, _num_names, 487 Management::ticks_to_ms(total_hold_time), 488 Management::ticks_to_ms(total_wait_time), 489 total_count); 490 for (int i = 0; i < _num_names; i++) { 491 print_counter_on(st, _names[i], _is_unique[i], i+1); 492 } 493 print_counter_on(st, "Unnamed / Other", false /*is_unique*/, 0); 494 } else { 495 st->print_cr("MutexLocker: no info (%s is disabled)", (UsePerfData ? "ProfileVMLocks" : "UsePerfData")); 496 } 497 } 498 499 void MutexLockerImpl::post_initialize() { 500 // Print mutex ranks if requested. 501 LogTarget(Info, vmmutex) lt; 502 if (lt.is_enabled()) { 503 ResourceMark rm; 504 LogStream ls(lt); 505 print_lock_ranks(&ls); 506 } 507 } 508 509 GCMutexLocker::GCMutexLocker(Mutex* mutex) { 510 if (SafepointSynchronize::is_at_safepoint()) { 511 _locked = false; 512 } else { 513 _mutex = mutex; 514 _locked = true; 515 _mutex->lock(); 516 } 517 } 518 519 // Print all mutexes/monitors that are currently owned by a thread; called 520 // by fatal error handler. 521 void print_owned_locks_on_error(outputStream* st) { 522 st->print("VM Mutex/Monitor currently owned by a thread: "); 523 bool none = true; 524 for (int i = 0; i < _num_mutex; i++) { 525 // see if it has an owner 526 if (_mutex_array[i]->owner() != nullptr) { 527 if (none) { 528 // print format used by Mutex::print_on_error() 529 st->print_cr(" ([mutex/lock_event])"); 530 none = false; 531 } 532 _mutex_array[i]->print_on_error(st); 533 st->cr(); 534 } 535 } 536 if (none) st->print_cr("None"); 537 } 538 539 void print_lock_ranks(outputStream* st) { 540 st->print_cr("VM Mutex/Monitor ranks: "); 541 542 #ifdef ASSERT 543 // Be extra defensive and figure out the bounds on 544 // ranks right here. This also saves a bit of time 545 // in the #ranks*#mutexes loop below. 546 int min_rank = INT_MAX; 547 int max_rank = INT_MIN; 548 for (int i = 0; i < _num_mutex; i++) { 549 Mutex* m = _mutex_array[i]; 550 int r = (int) m->rank(); 551 if (min_rank > r) min_rank = r; 552 if (max_rank < r) max_rank = r; 553 } 554 555 // Print the listings rank by rank 556 for (int r = min_rank; r <= max_rank; r++) { 557 bool first = true; 558 for (int i = 0; i < _num_mutex; i++) { 559 Mutex* m = _mutex_array[i]; 560 if (r != (int) m->rank()) continue; 561 562 if (first) { 563 st->cr(); 564 st->print_cr("Rank \"%s\":", m->rank_name()); 565 first = false; 566 } 567 st->print_cr(" %s", m->name()); 568 } 569 } 570 #else 571 st->print_cr(" Only known in debug builds."); 572 #endif // ASSERT 573 }