< prev index next >

src/hotspot/share/runtime/mutexLocker.cpp

Print this page

  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 "gc/shared/gc_globals.hpp"
 26 #include "logging/log.hpp"
 27 #include "logging/logStream.hpp"
 28 #include "memory/resourceArea.hpp"
 29 #include "memory/universe.hpp"


 30 #include "runtime/javaThread.hpp"
 31 #include "runtime/mutexLocker.hpp"
 32 #include "runtime/safepoint.hpp"
 33 #include "runtime/vmThread.hpp"

 34 #include "utilities/vmError.hpp"
 35 
 36 // Mutexes used in the VM (see comment in mutexLocker.hpp):
 37 
 38 Mutex*   NMethodState_lock            = nullptr;
 39 Monitor* SystemDictionary_lock        = nullptr;
 40 Mutex*   InvokeMethodTypeTable_lock   = nullptr;
 41 Monitor* InvokeMethodIntrinsicTable_lock = nullptr;
 42 Mutex*   SharedDictionary_lock        = nullptr;
 43 Monitor* ClassInitError_lock          = nullptr;
 44 Mutex*   Module_lock                  = nullptr;
 45 Mutex*   CompiledIC_lock              = nullptr;
 46 Mutex*   VMStatistic_lock             = nullptr;
 47 Mutex*   JmethodIdCreation_lock       = nullptr;
 48 Mutex*   JfieldIdCreation_lock        = nullptr;
 49 Monitor* JNICritical_lock             = nullptr;
 50 Mutex*   JvmtiThreadState_lock        = nullptr;
 51 Monitor* EscapeBarrier_lock           = nullptr;
 52 Monitor* JvmtiVTMSTransition_lock     = nullptr;
 53 Mutex*   JvmtiVThreadSuspend_lock     = nullptr;

 58 Mutex*   AdapterHandlerLibrary_lock   = nullptr;
 59 Mutex*   SignatureHandlerLibrary_lock = nullptr;
 60 Mutex*   VtableStubs_lock             = nullptr;
 61 Mutex*   SymbolArena_lock             = nullptr;
 62 Monitor* StringDedup_lock             = nullptr;
 63 Mutex*   StringDedupIntern_lock       = nullptr;
 64 Monitor* CodeCache_lock               = nullptr;
 65 Mutex*   TouchedMethodLog_lock        = nullptr;
 66 Mutex*   RetData_lock                 = nullptr;
 67 Monitor* VMOperation_lock             = nullptr;
 68 Monitor* ThreadsLockThrottle_lock     = nullptr;
 69 Monitor* Threads_lock                 = nullptr;
 70 Mutex*   NonJavaThreadsList_lock      = nullptr;
 71 Mutex*   NonJavaThreadsListSync_lock  = nullptr;
 72 Monitor* STS_lock                     = nullptr;
 73 Mutex*   MonitoringSupport_lock       = nullptr;
 74 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
 75 Mutex*   Compile_lock                 = nullptr;
 76 Monitor* CompileTaskWait_lock         = nullptr;
 77 Monitor* MethodCompileQueue_lock      = nullptr;




 78 Monitor* CompileThread_lock           = nullptr;
 79 Monitor* Compilation_lock             = nullptr;
 80 Mutex*   CompileStatistics_lock       = nullptr;
 81 Mutex*   DirectivesStack_lock         = nullptr;
 82 Monitor* Terminator_lock              = nullptr;
 83 Monitor* InitCompleted_lock           = nullptr;
 84 Monitor* BeforeExit_lock              = nullptr;
 85 Monitor* Notify_lock                  = nullptr;
 86 Mutex*   ExceptionCache_lock          = nullptr;
 87 Mutex*   TrainingData_lock            = nullptr;
 88 Monitor* TrainingReplayQueue_lock     = nullptr;
 89 #ifndef PRODUCT
 90 Mutex*   FullGCALot_lock              = nullptr;
 91 #endif
 92 
 93 Mutex*   tty_lock                     = nullptr;
 94 
 95 Mutex*   RawMonitor_lock              = nullptr;
 96 Mutex*   PerfDataMemAlloc_lock        = nullptr;
 97 Mutex*   PerfDataManager_lock         = nullptr;

136 Mutex*   ClassLoaderDataGraph_lock    = nullptr;
137 Monitor* ThreadsSMRDelete_lock        = nullptr;
138 Mutex*   ThreadIdTableCreate_lock     = nullptr;
139 Mutex*   SharedDecoder_lock           = nullptr;
140 Mutex*   DCmdFactory_lock             = nullptr;
141 Mutex*   NMTQuery_lock                = nullptr;
142 Mutex*   NMTCompilationCostHistory_lock = nullptr;
143 Mutex*   NmtVirtualMemory_lock          = nullptr;
144 
145 #if INCLUDE_CDS
146 #if INCLUDE_JVMTI
147 Mutex*   CDSClassFileStream_lock      = nullptr;
148 #endif
149 Mutex*   DumpTimeTable_lock           = nullptr;
150 Mutex*   CDSLambda_lock               = nullptr;
151 Mutex*   DumpRegion_lock              = nullptr;
152 Mutex*   ClassListFile_lock           = nullptr;
153 Mutex*   UnregisteredClassesTable_lock= nullptr;
154 Mutex*   LambdaFormInvokers_lock      = nullptr;
155 Mutex*   ScratchObjects_lock          = nullptr;

156 Mutex*   FinalImageRecipes_lock       = nullptr;
157 #endif // INCLUDE_CDS
158 Mutex*   Bootclasspath_lock           = nullptr;
159 
160 #if INCLUDE_JVMCI
161 Monitor* JVMCI_lock                   = nullptr;
162 Monitor* JVMCIRuntime_lock            = nullptr;
163 #endif
164 
165 // Only one RecursiveMutex
166 RecursiveMutex* MultiArray_lock       = nullptr;
167 
168 #ifdef ASSERT
169 void assert_locked_or_safepoint(const Mutex* lock) {
170   if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
171   // check if this thread owns the lock (common case)
172   assert(lock != nullptr, "Need non-null lock");
173   if (lock->owned_by_self()) return;
174   if (SafepointSynchronize::is_at_safepoint()) return;
175   if (!Universe::is_fully_initialized()) return;

242   MUTEX_DEFN(SignatureHandlerLibrary_lock    , PaddedMutex  , safepoint);
243   MUTEX_DEFN(SymbolArena_lock                , PaddedMutex  , nosafepoint);
244   MUTEX_DEFN(ExceptionCache_lock             , PaddedMutex  , safepoint);
245 #ifndef PRODUCT
246   MUTEX_DEFN(FullGCALot_lock                 , PaddedMutex  , safepoint); // a lock to make FullGCALot MT safe
247 #endif
248   MUTEX_DEFN(BeforeExit_lock                 , PaddedMonitor, safepoint);
249 
250   MUTEX_DEFN(NonJavaThreadsList_lock         , PaddedMutex  , nosafepoint-1);
251   MUTEX_DEFN(NonJavaThreadsListSync_lock     , PaddedMutex  , nosafepoint);
252 
253   MUTEX_DEFN(RetData_lock                    , PaddedMutex  , safepoint);
254   MUTEX_DEFN(Terminator_lock                 , PaddedMonitor, safepoint, true);
255   MUTEX_DEFN(InitCompleted_lock              , PaddedMonitor, nosafepoint);
256   MUTEX_DEFN(Notify_lock                     , PaddedMonitor, safepoint, true);
257 
258   MUTEX_DEFN(JfieldIdCreation_lock           , PaddedMutex  , safepoint);
259 
260   MUTEX_DEFN(CompiledIC_lock                 , PaddedMutex  , nosafepoint);  // locks VtableStubs_lock
261   MUTEX_DEFN(MethodCompileQueue_lock         , PaddedMonitor, safepoint);











262   MUTEX_DEFN(TrainingData_lock               , PaddedMutex  , nosafepoint);
263   MUTEX_DEFN(TrainingReplayQueue_lock        , PaddedMonitor, safepoint);
264   MUTEX_DEFN(CompileStatistics_lock          , PaddedMutex  , safepoint);
265   MUTEX_DEFN(DirectivesStack_lock            , PaddedMutex  , nosafepoint);
266 
267   MUTEX_DEFN(JvmtiVTMSTransition_lock        , PaddedMonitor, safepoint);   // used for Virtual Thread Mount State transition management
268   MUTEX_DEFN(JvmtiVThreadSuspend_lock        , PaddedMutex,   nosafepoint-1);
269   MUTEX_DEFN(EscapeBarrier_lock              , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI
270   MUTEX_DEFN(Management_lock                 , PaddedMutex  , safepoint);   // used for JVM management
271 
272   MUTEX_DEFN(ConcurrentGCBreakpoints_lock    , PaddedMonitor, safepoint, true);
273   MUTEX_DEFN(TouchedMethodLog_lock           , PaddedMutex  , safepoint);
274 
275   MUTEX_DEFN(CompileThread_lock              , PaddedMonitor, safepoint);
276   MUTEX_DEFN(PeriodicTask_lock               , PaddedMonitor, safepoint, true);
277   MUTEX_DEFN(RedefineClasses_lock            , PaddedMonitor, safepoint);
278   MUTEX_DEFN(Verify_lock                     , PaddedMutex  , safepoint);
279   MUTEX_DEFN(ClassLoaderDataGraph_lock       , PaddedMutex  , safepoint);
280 
281   if (WhiteBoxAPI) {
282     MUTEX_DEFN(Compilation_lock              , PaddedMonitor, nosafepoint);
283   }
284 
285 #if INCLUDE_JFR
286   MUTEX_DEFN(JfrBuffer_lock                  , PaddedMutex  , event);
287   MUTEX_DEFN(JfrMsg_lock                     , PaddedMonitor, event);
288   MUTEX_DEFN(JfrStacktrace_lock              , PaddedMutex  , event);
289 #endif
290 
291   MUTEX_DEFN(ContinuationRelativize_lock     , PaddedMonitor, nosafepoint-3);
292   MUTEX_DEFN(CodeHeapStateAnalytics_lock     , PaddedMutex  , safepoint);
293   MUTEX_DEFN(ThreadsSMRDelete_lock           , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock
294   MUTEX_DEFN(ThreadIdTableCreate_lock        , PaddedMutex  , safepoint);
295   MUTEX_DEFN(DCmdFactory_lock                , PaddedMutex  , nosafepoint);
296   MUTEX_DEFN(NMTQuery_lock                   , PaddedMutex  , safepoint);
297   MUTEX_DEFN(NMTCompilationCostHistory_lock  , PaddedMutex  , nosafepoint);
298   MUTEX_DEFN(NmtVirtualMemory_lock           , PaddedMutex  , service-4); // Must be lower than G1Mapper_lock used from G1RegionsSmallerThanCommitSizeMapper::commit_regions
299 #if INCLUDE_CDS
300 #if INCLUDE_JVMTI
301   MUTEX_DEFN(CDSClassFileStream_lock         , PaddedMutex  , safepoint);
302 #endif
303   MUTEX_DEFN(DumpTimeTable_lock              , PaddedMutex  , nosafepoint);
304   MUTEX_DEFN(CDSLambda_lock                  , PaddedMutex  , nosafepoint);
305   MUTEX_DEFL(DumpRegion_lock                 , PaddedMutex  , DumpTimeTable_lock);
306   MUTEX_DEFN(ClassListFile_lock              , PaddedMutex  , nosafepoint);
307   MUTEX_DEFN(UnregisteredClassesTable_lock   , PaddedMutex  , nosafepoint-1);
308   MUTEX_DEFN(LambdaFormInvokers_lock         , PaddedMutex  , safepoint);
309   MUTEX_DEFL(ScratchObjects_lock             , PaddedMutex  , DumpTimeTable_lock);

310   MUTEX_DEFN(FinalImageRecipes_lock          , PaddedMutex  , nosafepoint);
311 #endif // INCLUDE_CDS
312   MUTEX_DEFN(Bootclasspath_lock              , PaddedMutex  , nosafepoint);
313 
314 #if INCLUDE_JVMCI
315   // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock
316   MUTEX_DEFN(JVMCIRuntime_lock               , PaddedMonitor, safepoint, true);
317 #endif
318 
319   MUTEX_DEFN(ThreadsLockThrottle_lock        , PaddedMonitor, safepoint);
320 
321   // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
322   MUTEX_DEFL(VtableStubs_lock               , PaddedMutex  , CompiledIC_lock);  // Also holds DumpTimeTable_lock
323   MUTEX_DEFL(CodeCache_lock                 , PaddedMonitor, VtableStubs_lock);
324   MUTEX_DEFL(NMethodState_lock              , PaddedMutex  , CodeCache_lock);
325 
326   // tty_lock is held when printing nmethod and its relocations which use this lock.
327   MUTEX_DEFL(ExternalsRecorder_lock         , PaddedMutex  , tty_lock);
328 
329   MUTEX_DEFL(AOTCodeCStrings_lock           , PaddedMutex  , tty_lock);

355 #endif
356   MUTEX_DEFL(Module_lock                    , PaddedMutex  ,  ClassLoaderDataGraph_lock);
357   MUTEX_DEFL(SystemDictionary_lock          , PaddedMonitor, Module_lock);
358 #if INCLUDE_JVMCI
359   // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
360   MUTEX_DEFL(JVMCI_lock                     , PaddedMonitor, JVMCIRuntime_lock);
361 #endif
362   MUTEX_DEFL(JvmtiThreadState_lock          , PaddedMutex  , JvmtiVTMSTransition_lock);   // Used by JvmtiThreadState/JvmtiEventController
363   MUTEX_DEFL(SharedDecoder_lock             , PaddedMutex  , NmtVirtualMemory_lock); // Must be lower than NmtVirtualMemory_lock due to MemTracker::print_containing_region
364 
365   // Allocate RecursiveMutex
366   MultiArray_lock = new RecursiveMutex();
367 }
368 
369 #undef MUTEX_DEFL
370 #undef MUTEX_DEFN
371 #undef MUTEX_DEF
372 #undef MUTEX_STORAGE
373 #undef MUTEX_STORAGE_NAME
374 


































































































































































375 void MutexLockerImpl::post_initialize() {
376   // Print mutex ranks if requested.
377   LogTarget(Info, vmmutex) lt;
378   if (lt.is_enabled()) {
379     ResourceMark rm;
380     LogStream ls(lt);
381     Mutex::print_lock_ranks(&ls);
382   }
383 }
384 
385 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
386   if (SafepointSynchronize::is_at_safepoint()) {
387     _locked = false;
388   } else {
389     _mutex = mutex;
390     _locked = true;
391     _mutex->lock();
392   }
393 }

  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 "compiler/compiler_globals.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/atomicAccess.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*   NMethodState_lock            = nullptr;
 43 Monitor* SystemDictionary_lock        = nullptr;
 44 Mutex*   InvokeMethodTypeTable_lock   = nullptr;
 45 Monitor* InvokeMethodIntrinsicTable_lock = nullptr;
 46 Mutex*   SharedDictionary_lock        = nullptr;
 47 Monitor* ClassInitError_lock          = nullptr;
 48 Mutex*   Module_lock                  = nullptr;
 49 Mutex*   CompiledIC_lock              = nullptr;
 50 Mutex*   VMStatistic_lock             = nullptr;
 51 Mutex*   JmethodIdCreation_lock       = nullptr;
 52 Mutex*   JfieldIdCreation_lock        = nullptr;
 53 Monitor* JNICritical_lock             = nullptr;
 54 Mutex*   JvmtiThreadState_lock        = nullptr;
 55 Monitor* EscapeBarrier_lock           = nullptr;
 56 Monitor* JvmtiVTMSTransition_lock     = nullptr;
 57 Mutex*   JvmtiVThreadSuspend_lock     = nullptr;

 62 Mutex*   AdapterHandlerLibrary_lock   = nullptr;
 63 Mutex*   SignatureHandlerLibrary_lock = nullptr;
 64 Mutex*   VtableStubs_lock             = nullptr;
 65 Mutex*   SymbolArena_lock             = nullptr;
 66 Monitor* StringDedup_lock             = nullptr;
 67 Mutex*   StringDedupIntern_lock       = nullptr;
 68 Monitor* CodeCache_lock               = nullptr;
 69 Mutex*   TouchedMethodLog_lock        = nullptr;
 70 Mutex*   RetData_lock                 = nullptr;
 71 Monitor* VMOperation_lock             = nullptr;
 72 Monitor* ThreadsLockThrottle_lock     = nullptr;
 73 Monitor* Threads_lock                 = nullptr;
 74 Mutex*   NonJavaThreadsList_lock      = nullptr;
 75 Mutex*   NonJavaThreadsListSync_lock  = nullptr;
 76 Monitor* STS_lock                     = nullptr;
 77 Mutex*   MonitoringSupport_lock       = nullptr;
 78 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
 79 Mutex*   Compile_lock                 = nullptr;
 80 Monitor* CompileTaskWait_lock         = nullptr;
 81 Monitor* MethodCompileQueue_lock      = nullptr;
 82 Monitor* MethodCompileQueueC1_lock    = nullptr;
 83 Monitor* MethodCompileQueueC2_lock    = nullptr;
 84 Monitor* MethodCompileQueueSC1_lock   = nullptr;
 85 Monitor* MethodCompileQueueSC2_lock   = nullptr;
 86 Monitor* CompileThread_lock           = nullptr;
 87 Monitor* Compilation_lock             = nullptr;
 88 Mutex*   CompileStatistics_lock       = nullptr;
 89 Mutex*   DirectivesStack_lock         = nullptr;
 90 Monitor* Terminator_lock              = nullptr;
 91 Monitor* InitCompleted_lock           = nullptr;
 92 Monitor* BeforeExit_lock              = nullptr;
 93 Monitor* Notify_lock                  = nullptr;
 94 Mutex*   ExceptionCache_lock          = nullptr;
 95 Mutex*   TrainingData_lock            = nullptr;
 96 Monitor* TrainingReplayQueue_lock     = nullptr;
 97 #ifndef PRODUCT
 98 Mutex*   FullGCALot_lock              = nullptr;
 99 #endif
100 
101 Mutex*   tty_lock                     = nullptr;
102 
103 Mutex*   RawMonitor_lock              = nullptr;
104 Mutex*   PerfDataMemAlloc_lock        = nullptr;
105 Mutex*   PerfDataManager_lock         = nullptr;

144 Mutex*   ClassLoaderDataGraph_lock    = nullptr;
145 Monitor* ThreadsSMRDelete_lock        = nullptr;
146 Mutex*   ThreadIdTableCreate_lock     = nullptr;
147 Mutex*   SharedDecoder_lock           = nullptr;
148 Mutex*   DCmdFactory_lock             = nullptr;
149 Mutex*   NMTQuery_lock                = nullptr;
150 Mutex*   NMTCompilationCostHistory_lock = nullptr;
151 Mutex*   NmtVirtualMemory_lock          = nullptr;
152 
153 #if INCLUDE_CDS
154 #if INCLUDE_JVMTI
155 Mutex*   CDSClassFileStream_lock      = nullptr;
156 #endif
157 Mutex*   DumpTimeTable_lock           = nullptr;
158 Mutex*   CDSLambda_lock               = nullptr;
159 Mutex*   DumpRegion_lock              = nullptr;
160 Mutex*   ClassListFile_lock           = nullptr;
161 Mutex*   UnregisteredClassesTable_lock= nullptr;
162 Mutex*   LambdaFormInvokers_lock      = nullptr;
163 Mutex*   ScratchObjects_lock          = nullptr;
164 Mutex*   ArchivedObjectTables_lock    = nullptr;
165 Mutex*   FinalImageRecipes_lock       = nullptr;
166 #endif // INCLUDE_CDS
167 Mutex*   Bootclasspath_lock           = nullptr;
168 
169 #if INCLUDE_JVMCI
170 Monitor* JVMCI_lock                   = nullptr;
171 Monitor* JVMCIRuntime_lock            = nullptr;
172 #endif
173 
174 // Only one RecursiveMutex
175 RecursiveMutex* MultiArray_lock       = nullptr;
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;

251   MUTEX_DEFN(SignatureHandlerLibrary_lock    , PaddedMutex  , safepoint);
252   MUTEX_DEFN(SymbolArena_lock                , PaddedMutex  , nosafepoint);
253   MUTEX_DEFN(ExceptionCache_lock             , PaddedMutex  , safepoint);
254 #ifndef PRODUCT
255   MUTEX_DEFN(FullGCALot_lock                 , PaddedMutex  , safepoint); // a lock to make FullGCALot MT safe
256 #endif
257   MUTEX_DEFN(BeforeExit_lock                 , PaddedMonitor, safepoint);
258 
259   MUTEX_DEFN(NonJavaThreadsList_lock         , PaddedMutex  , nosafepoint-1);
260   MUTEX_DEFN(NonJavaThreadsListSync_lock     , PaddedMutex  , nosafepoint);
261 
262   MUTEX_DEFN(RetData_lock                    , PaddedMutex  , safepoint);
263   MUTEX_DEFN(Terminator_lock                 , PaddedMonitor, safepoint, true);
264   MUTEX_DEFN(InitCompleted_lock              , PaddedMonitor, nosafepoint);
265   MUTEX_DEFN(Notify_lock                     , PaddedMonitor, safepoint, true);
266 
267   MUTEX_DEFN(JfieldIdCreation_lock           , PaddedMutex  , safepoint);
268 
269   MUTEX_DEFN(CompiledIC_lock                 , PaddedMutex  , nosafepoint);  // locks VtableStubs_lock
270   MUTEX_DEFN(MethodCompileQueue_lock         , PaddedMonitor, safepoint);
271   if (UseGlobalCompileQueueLock) {
272     MethodCompileQueueC1_lock  = MethodCompileQueue_lock;
273     MethodCompileQueueC2_lock  = MethodCompileQueue_lock;
274     MethodCompileQueueSC1_lock = MethodCompileQueue_lock;
275     MethodCompileQueueSC2_lock = MethodCompileQueue_lock;
276   } else {
277     MUTEX_DEFN(MethodCompileQueueC1_lock     , PaddedMonitor, safepoint);
278     MUTEX_DEFN(MethodCompileQueueC2_lock     , PaddedMonitor, safepoint);
279     MUTEX_DEFN(MethodCompileQueueSC1_lock    , PaddedMonitor, safepoint);
280     MUTEX_DEFN(MethodCompileQueueSC2_lock    , PaddedMonitor, safepoint);
281   }
282   MUTEX_DEFN(TrainingData_lock               , PaddedMutex  , nosafepoint);
283   MUTEX_DEFN(TrainingReplayQueue_lock        , PaddedMonitor, safepoint);
284   MUTEX_DEFN(CompileStatistics_lock          , PaddedMutex  , safepoint);
285   MUTEX_DEFN(DirectivesStack_lock            , PaddedMutex  , nosafepoint);
286 
287   MUTEX_DEFN(JvmtiVTMSTransition_lock        , PaddedMonitor, safepoint);   // used for Virtual Thread Mount State transition management
288   MUTEX_DEFN(JvmtiVThreadSuspend_lock        , PaddedMutex,   nosafepoint-1);
289   MUTEX_DEFN(EscapeBarrier_lock              , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI
290   MUTEX_DEFN(Management_lock                 , PaddedMutex  , safepoint);   // used for JVM management
291 
292   MUTEX_DEFN(ConcurrentGCBreakpoints_lock    , PaddedMonitor, safepoint, true);
293   MUTEX_DEFN(TouchedMethodLog_lock           , PaddedMutex  , safepoint);
294 
295   MUTEX_DEFN(CompileThread_lock              , PaddedMonitor, safepoint);
296   MUTEX_DEFN(PeriodicTask_lock               , PaddedMonitor, safepoint, true);
297   MUTEX_DEFN(RedefineClasses_lock            , PaddedMonitor, safepoint);
298   MUTEX_DEFN(Verify_lock                     , PaddedMutex  , safepoint);
299   MUTEX_DEFN(ClassLoaderDataGraph_lock       , PaddedMutex  , safepoint);
300 
301   MUTEX_DEFN(Compilation_lock                , PaddedMonitor, nosafepoint);


302 
303 #if INCLUDE_JFR
304   MUTEX_DEFN(JfrBuffer_lock                  , PaddedMutex  , event);
305   MUTEX_DEFN(JfrMsg_lock                     , PaddedMonitor, event);
306   MUTEX_DEFN(JfrStacktrace_lock              , PaddedMutex  , event);
307 #endif
308 
309   MUTEX_DEFN(ContinuationRelativize_lock     , PaddedMonitor, nosafepoint-3);
310   MUTEX_DEFN(CodeHeapStateAnalytics_lock     , PaddedMutex  , safepoint);
311   MUTEX_DEFN(ThreadsSMRDelete_lock           , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock
312   MUTEX_DEFN(ThreadIdTableCreate_lock        , PaddedMutex  , safepoint);
313   MUTEX_DEFN(DCmdFactory_lock                , PaddedMutex  , nosafepoint);
314   MUTEX_DEFN(NMTQuery_lock                   , PaddedMutex  , safepoint);
315   MUTEX_DEFN(NMTCompilationCostHistory_lock  , PaddedMutex  , nosafepoint);
316   MUTEX_DEFN(NmtVirtualMemory_lock           , PaddedMutex  , service-4); // Must be lower than G1Mapper_lock used from G1RegionsSmallerThanCommitSizeMapper::commit_regions
317 #if INCLUDE_CDS
318 #if INCLUDE_JVMTI
319   MUTEX_DEFN(CDSClassFileStream_lock         , PaddedMutex  , safepoint);
320 #endif
321   MUTEX_DEFN(DumpTimeTable_lock              , PaddedMutex  , nosafepoint);
322   MUTEX_DEFN(CDSLambda_lock                  , PaddedMutex  , nosafepoint);
323   MUTEX_DEFL(DumpRegion_lock                 , PaddedMutex  , DumpTimeTable_lock);
324   MUTEX_DEFN(ClassListFile_lock              , PaddedMutex  , nosafepoint);
325   MUTEX_DEFN(UnregisteredClassesTable_lock   , PaddedMutex  , nosafepoint-1);
326   MUTEX_DEFN(LambdaFormInvokers_lock         , PaddedMutex  , safepoint);
327   MUTEX_DEFL(ScratchObjects_lock             , PaddedMutex  , DumpTimeTable_lock);
328   MUTEX_DEFN(ArchivedObjectTables_lock       , PaddedMutex  , nosafepoint);
329   MUTEX_DEFN(FinalImageRecipes_lock          , PaddedMutex  , nosafepoint);
330 #endif // INCLUDE_CDS
331   MUTEX_DEFN(Bootclasspath_lock              , PaddedMutex  , nosafepoint);
332 
333 #if INCLUDE_JVMCI
334   // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock
335   MUTEX_DEFN(JVMCIRuntime_lock               , PaddedMonitor, safepoint, true);
336 #endif
337 
338   MUTEX_DEFN(ThreadsLockThrottle_lock        , PaddedMonitor, safepoint);
339 
340   // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
341   MUTEX_DEFL(VtableStubs_lock               , PaddedMutex  , CompiledIC_lock);  // Also holds DumpTimeTable_lock
342   MUTEX_DEFL(CodeCache_lock                 , PaddedMonitor, VtableStubs_lock);
343   MUTEX_DEFL(NMethodState_lock              , PaddedMutex  , CodeCache_lock);
344 
345   // tty_lock is held when printing nmethod and its relocations which use this lock.
346   MUTEX_DEFL(ExternalsRecorder_lock         , PaddedMutex  , tty_lock);
347 
348   MUTEX_DEFL(AOTCodeCStrings_lock           , PaddedMutex  , tty_lock);

374 #endif
375   MUTEX_DEFL(Module_lock                    , PaddedMutex  ,  ClassLoaderDataGraph_lock);
376   MUTEX_DEFL(SystemDictionary_lock          , PaddedMonitor, Module_lock);
377 #if INCLUDE_JVMCI
378   // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
379   MUTEX_DEFL(JVMCI_lock                     , PaddedMonitor, JVMCIRuntime_lock);
380 #endif
381   MUTEX_DEFL(JvmtiThreadState_lock          , PaddedMutex  , JvmtiVTMSTransition_lock);   // Used by JvmtiThreadState/JvmtiEventController
382   MUTEX_DEFL(SharedDecoder_lock             , PaddedMutex  , NmtVirtualMemory_lock); // Must be lower than NmtVirtualMemory_lock due to MemTracker::print_containing_region
383 
384   // Allocate RecursiveMutex
385   MultiArray_lock = new RecursiveMutex();
386 }
387 
388 #undef MUTEX_DEFL
389 #undef MUTEX_DEFN
390 #undef MUTEX_DEF
391 #undef MUTEX_STORAGE
392 #undef MUTEX_STORAGE_NAME
393 
394 static const int MAX_NAMES = 200;
395 static const char* _names[MAX_NAMES] = { nullptr };
396 static bool _is_unique[MAX_NAMES] = { false };
397 static volatile int _num_names = 0;
398 
399 static bool _mutex_init_done = false;
400 
401 PerfCounter** MutexLockerImpl::_perf_lock_count     = nullptr;
402 PerfCounter** MutexLockerImpl::_perf_lock_wait_time = nullptr;
403 PerfCounter** MutexLockerImpl::_perf_lock_hold_time = nullptr;
404 
405 MutexLockerImpl::MutexLockerImpl(Mutex* mutex, Mutex::SafepointCheckFlag flag) :
406   _mutex(mutex),
407   _prof(ProfileVMLocks && Thread::current_or_null() != nullptr && Thread::current()->profile_vm_locks()) {
408 
409   bool no_safepoint_check = flag == Mutex::_no_safepoint_check_flag;
410   if (_mutex != nullptr) {
411     if (_prof) { _before.start(); } // before
412 
413     if (no_safepoint_check) {
414       _mutex->lock_without_safepoint_check();
415     } else {
416       _mutex->lock();
417     }
418 
419     if (_prof) { _before.stop(); _after.start(); } // after
420   }
421 }
422 
423 MutexLockerImpl::MutexLockerImpl(Thread* thread, Mutex* mutex, Mutex::SafepointCheckFlag flag) :
424   _mutex(mutex), _prof(thread->profile_vm_locks()) {
425 
426   if (_prof) { _before.start(); } // before
427 
428   bool no_safepoint_check = flag == Mutex::_no_safepoint_check_flag;
429   if (_mutex != nullptr) {
430     if (no_safepoint_check) {
431       _mutex->lock_without_safepoint_check(thread);
432     } else {
433       _mutex->lock(thread);
434     }
435   }
436 
437   if (_prof) { _before.stop(); _after.start(); } // after
438 }
439 
440 void MutexLockerImpl::init_counters() {
441   if (ProfileVMLocks && UsePerfData) {
442     ResourceMark rm;
443     EXCEPTION_MARK;
444     _perf_lock_count     = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
445     _perf_lock_wait_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
446     _perf_lock_hold_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
447 
448     NEWPERFEVENTCOUNTER(_perf_lock_count[0],     SUN_RT, PerfDataManager::counter_name("Other", "Count"));
449     NEWPERFEVENTCOUNTER(_perf_lock_wait_time[0], SUN_RT, PerfDataManager::counter_name("Other", "BeforeTime"));
450     NEWPERFEVENTCOUNTER(_perf_lock_hold_time[0], SUN_RT, PerfDataManager::counter_name("Other", "AfterTime"));
451     for (int i = 0; i < MAX_NAMES; i++) {
452       ResourceMark rm;
453       const char* counter_name = _names[i];
454       if (counter_name == nullptr) {
455         stringStream ss;
456         ss.print("UnnamedMutex#%d", i);
457         counter_name = ss.as_string();
458         _names[i] = os::strdup(counter_name, mtInternal); // replace default nullptr
459       }
460       NEWPERFEVENTCOUNTER(_perf_lock_count[i + 1],     SUN_RT, PerfDataManager::counter_name(counter_name, "Count"));
461       NEWPERFEVENTCOUNTER(_perf_lock_wait_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "BeforeTime"));
462       NEWPERFEVENTCOUNTER(_perf_lock_hold_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "AfterTime"));
463     }
464     if (HAS_PENDING_EXCEPTION) {
465       vm_exit_during_initialization("MutexLockerImpl::init_counters() failed unexpectedly");
466     }
467   }
468   _mutex_init_done = true;
469 }
470 
471 int MutexLockerImpl::name2id(const char* name) {
472   if (ProfileVMLocks && UsePerfData) {
473     // There is not concurency or duplication in mutex_init().
474     if (!_mutex_init_done) {
475       int new_id = AtomicAccess::load(&_num_names);
476       precond(new_id < MAX_NAMES);
477       AtomicAccess::inc(&_num_names);
478       _names[new_id] = os::strdup(name, mtInternal);
479       _is_unique[new_id] = true;
480       return new_id;
481     }
482     int limit = AtomicAccess::load(&_num_names); // Cache static value which can be updated concurently
483     for (int i = Mutex::num_mutex(); i < limit; i++) {
484       if (strcmp(_names[i], name) == 0) {
485         _is_unique[i] = false;
486         return i;
487       }
488     }
489     if (limit < MAX_NAMES) {
490       int old_limit = limit;
491       const char* name_dup = os::strdup(name, mtInternal);
492       int new_id; // Get new id for this name
493       do {
494         new_id = limit++;
495         if (new_id == MAX_NAMES) break;
496       } while (AtomicAccess::cmpxchg(&_num_names, new_id, limit) != new_id);
497       for (int i = old_limit; i < new_id; i++) {
498         if (strcmp(_names[i], name) == 0) { // Other thread put it there
499           _is_unique[i] = false;
500           return i; // Wasted new_id slot to simplify code: _num_names is only incremented
501         }
502       }
503       if (new_id < MAX_NAMES) {
504         _names[new_id] = name_dup;
505         _is_unique[new_id] = true;
506         return new_id;
507       }
508     }
509     log_debug(init)("Unnamed: %s", name); // no slots left
510   }
511   return -1;
512 }
513 
514 void MutexLockerImpl::print_counter_on(outputStream* st, const char* name, bool is_unique, int idx) {
515   jlong count = _perf_lock_count[idx]->get_value();
516   if (count > 0) {
517     st->print_cr("  %3d: %s%40s = " JLONG_FORMAT_W(5) "us (" JLONG_FORMAT_W(5) "us) / " JLONG_FORMAT_W(9) " events",
518                  idx, (is_unique ? " " : "M"), name,
519                  Management::ticks_to_us(_perf_lock_hold_time[idx]->get_value()),
520                  Management::ticks_to_us(_perf_lock_wait_time[idx]->get_value()),
521                  count);
522   }
523 }
524 
525 static jlong accumulate_lock_counters(PerfCounter** lock_counters) {
526   jlong acc = 0;
527   for (int i = 0; i < _num_names + 1; i++) { // 0 slot is reserved for unnamed locks
528     if (lock_counters[i] == nullptr) {
529       break;
530     }
531     acc += lock_counters[i]->get_value();
532   }
533   return acc;
534 }
535 
536 void MutexLockerImpl::print_counters_on(outputStream* st) {
537   if (ProfileVMLocks && UsePerfData) {
538     jlong total_count     = accumulate_lock_counters(_perf_lock_count);
539     jlong total_wait_time = accumulate_lock_counters(_perf_lock_wait_time);
540     jlong total_hold_time = accumulate_lock_counters(_perf_lock_hold_time);
541 
542     st->print_cr("MutexLocker: Total: %d named locks (%d unique names); hold = " JLONG_FORMAT "us (wait = " JLONG_FORMAT "us) / " JLONG_FORMAT " events for thread \"main\"",
543                  Mutex::num_mutex(), _num_names,
544                  Management::ticks_to_us(total_hold_time),
545                  Management::ticks_to_us(total_wait_time),
546                  total_count);
547     for (int i = 0; i < _num_names; i++) {
548       print_counter_on(st, _names[i], _is_unique[i], i+1);
549     }
550     print_counter_on(st, "Unnamed / Other", false /*is_unique*/, 0);
551   } else {
552     st->print_cr("MutexLocker: no info (%s is disabled)", (UsePerfData ? "ProfileVMLocks" : "UsePerfData"));
553   }
554 }
555 
556 void MutexLockerImpl::post_initialize() {
557   // Print mutex ranks if requested.
558   LogTarget(Info, vmmutex) lt;
559   if (lt.is_enabled()) {
560     ResourceMark rm;
561     LogStream ls(lt);
562     Mutex::print_lock_ranks(&ls);
563   }
564 }
565 
566 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
567   if (SafepointSynchronize::is_at_safepoint()) {
568     _locked = false;
569   } else {
570     _mutex = mutex;
571     _locked = true;
572     _mutex->lock();
573   }
574 }
< prev index next >