< 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* AOTHeapLoading_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;

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;

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











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

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

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


































































































































































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

  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* AOTHeapLoading_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;

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;

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
271   MUTEX_DEFN(MethodCompileQueue_lock         , PaddedMonitor, safepoint);
272   if (UseGlobalCompileQueueLock) {
273     MethodCompileQueueC1_lock  = MethodCompileQueue_lock;
274     MethodCompileQueueC2_lock  = MethodCompileQueue_lock;
275     MethodCompileQueueSC1_lock = MethodCompileQueue_lock;
276     MethodCompileQueueSC2_lock = MethodCompileQueue_lock;
277   } else {
278     MUTEX_DEFN(MethodCompileQueueC1_lock     , PaddedMonitor, safepoint);
279     MUTEX_DEFN(MethodCompileQueueC2_lock     , PaddedMonitor, safepoint);
280     MUTEX_DEFN(MethodCompileQueueSC1_lock    , PaddedMonitor, safepoint);
281     MUTEX_DEFN(MethodCompileQueueSC2_lock    , PaddedMonitor, safepoint);
282   }
283   MUTEX_DEFN(TrainingData_lock               , PaddedMutex  , nosafepoint);
284   MUTEX_DEFN(TrainingReplayQueue_lock        , PaddedMonitor, safepoint);
285   MUTEX_DEFN(CompileStatistics_lock          , PaddedMutex  , safepoint);
286   MUTEX_DEFN(DirectivesStack_lock            , PaddedMutex  , nosafepoint);
287 
288   MUTEX_DEFN(JvmtiVTMSTransition_lock        , PaddedMonitor, safepoint);   // used for Virtual Thread Mount State transition management
289   MUTEX_DEFN(JvmtiVThreadSuspend_lock        , PaddedMutex,   nosafepoint-1);
290   MUTEX_DEFN(EscapeBarrier_lock              , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI
291   MUTEX_DEFN(Management_lock                 , PaddedMutex  , safepoint);   // used for JVM management
292 
293   MUTEX_DEFN(ConcurrentGCBreakpoints_lock    , PaddedMonitor, safepoint, true);
294   MUTEX_DEFN(TouchedMethodLog_lock           , PaddedMutex  , safepoint);
295 
296   MUTEX_DEFN(CompileThread_lock              , PaddedMonitor, safepoint);
297   MUTEX_DEFN(PeriodicTask_lock               , PaddedMonitor, safepoint, true);
298   MUTEX_DEFN(RedefineClasses_lock            , PaddedMonitor, safepoint);
299   MUTEX_DEFN(Verify_lock                     , PaddedMutex  , safepoint);
300   MUTEX_DEFN(ClassLoaderDataGraph_lock       , PaddedMutex  , safepoint);
301 
302   MUTEX_DEFN(Compilation_lock                , PaddedMonitor, nosafepoint);


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

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