< 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 Mutex*   NMethodEntryBarrier_lock     = nullptr;
 40 Monitor* SystemDictionary_lock        = nullptr;
 41 Mutex*   InvokeMethodTypeTable_lock   = nullptr;
 42 Monitor* InvokeMethodIntrinsicTable_lock = nullptr;
 43 Mutex*   SharedDictionary_lock        = nullptr;
 44 Monitor* ClassInitError_lock          = nullptr;
 45 Mutex*   Module_lock                  = nullptr;
 46 Mutex*   CompiledIC_lock              = nullptr;
 47 Mutex*   VMStatistic_lock             = nullptr;
 48 Mutex*   JmethodIdCreation_lock       = nullptr;
 49 Mutex*   JfieldIdCreation_lock        = nullptr;
 50 Monitor* JNICritical_lock             = nullptr;
 51 Mutex*   JvmtiThreadState_lock        = nullptr;
 52 Monitor* EscapeBarrier_lock           = nullptr;
 53 Monitor* JvmtiVTMSTransition_lock     = nullptr;

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




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

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

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

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











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

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

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


































































































































































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

  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/atomic.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 Mutex*   NMethodEntryBarrier_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*   VMStatistic_lock             = nullptr;
 52 Mutex*   JmethodIdCreation_lock       = nullptr;
 53 Mutex*   JfieldIdCreation_lock        = nullptr;
 54 Monitor* JNICritical_lock             = nullptr;
 55 Mutex*   JvmtiThreadState_lock        = nullptr;
 56 Monitor* EscapeBarrier_lock           = nullptr;
 57 Monitor* JvmtiVTMSTransition_lock     = nullptr;

 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* ThreadsLockThrottle_lock     = nullptr;
 74 Monitor* Threads_lock                 = nullptr;
 75 Mutex*   NonJavaThreadsList_lock      = nullptr;
 76 Mutex*   NonJavaThreadsListSync_lock  = nullptr;
 77 Monitor* STS_lock                     = nullptr;
 78 Mutex*   MonitoringSupport_lock       = nullptr;
 79 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
 80 Mutex*   Compile_lock                 = nullptr;
 81 Monitor* CompileTaskWait_lock         = nullptr;
 82 Monitor* MethodCompileQueue_lock      = nullptr;
 83 Monitor* MethodCompileQueueC1_lock    = nullptr;
 84 Monitor* MethodCompileQueueC2_lock    = nullptr;
 85 Monitor* MethodCompileQueueSC1_lock   = nullptr;
 86 Monitor* MethodCompileQueueSC2_lock   = nullptr;
 87 Monitor* CompileThread_lock           = nullptr;
 88 Monitor* Compilation_lock             = nullptr;
 89 Mutex*   CompileStatistics_lock       = nullptr;
 90 Mutex*   DirectivesStack_lock         = nullptr;
 91 Monitor* Terminator_lock              = nullptr;
 92 Monitor* InitCompleted_lock           = nullptr;
 93 Monitor* BeforeExit_lock              = nullptr;
 94 Monitor* Notify_lock                  = nullptr;
 95 Mutex*   ExceptionCache_lock          = nullptr;
 96 Mutex*   TrainingData_lock            = nullptr;
 97 Monitor* TrainingReplayQueue_lock     = nullptr;
 98 #ifndef PRODUCT
 99 Mutex*   FullGCALot_lock              = nullptr;
100 #endif
101 
102 Mutex*   tty_lock                     = nullptr;
103 
104 Mutex*   RawMonitor_lock              = nullptr;
105 Mutex*   PerfDataMemAlloc_lock        = nullptr;
106 Mutex*   PerfDataManager_lock         = nullptr;

145 Mutex*   ClassLoaderDataGraph_lock    = nullptr;
146 Monitor* ThreadsSMRDelete_lock        = nullptr;
147 Mutex*   ThreadIdTableCreate_lock     = nullptr;
148 Mutex*   SharedDecoder_lock           = nullptr;
149 Mutex*   DCmdFactory_lock             = nullptr;
150 Mutex*   NMTQuery_lock                = nullptr;
151 Mutex*   NMTCompilationCostHistory_lock = nullptr;
152 Mutex*   NmtVirtualMemory_lock          = nullptr;
153 
154 #if INCLUDE_CDS
155 #if INCLUDE_JVMTI
156 Mutex*   CDSClassFileStream_lock      = nullptr;
157 #endif
158 Mutex*   DumpTimeTable_lock           = nullptr;
159 Mutex*   CDSLambda_lock               = nullptr;
160 Mutex*   DumpRegion_lock              = nullptr;
161 Mutex*   ClassListFile_lock           = nullptr;
162 Mutex*   UnregisteredClassesTable_lock= nullptr;
163 Mutex*   LambdaFormInvokers_lock      = nullptr;
164 Mutex*   ScratchObjects_lock          = nullptr;
165 Mutex*   ArchivedObjectTables_lock    = nullptr;
166 Mutex*   FinalImageRecipes_lock       = nullptr;
167 #endif // INCLUDE_CDS
168 Mutex*   Bootclasspath_lock           = nullptr;
169 
170 #if INCLUDE_JVMCI
171 Monitor* JVMCI_lock                   = nullptr;
172 Monitor* JVMCIRuntime_lock            = nullptr;
173 #endif
174 
175 // Only one RecursiveMutex
176 RecursiveMutex* MultiArray_lock       = nullptr;
177 
178 #ifdef ASSERT
179 void assert_locked_or_safepoint(const Mutex* lock) {
180   if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
181   // check if this thread owns the lock (common case)
182   assert(lock != nullptr, "Need non-null lock");
183   if (lock->owned_by_self()) return;
184   if (SafepointSynchronize::is_at_safepoint()) return;
185   if (!Universe::is_fully_initialized()) return;

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


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

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