< prev index next >

src/hotspot/share/runtime/mutexLocker.cpp

Print this page

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"

 26 #include "gc/shared/gc_globals.hpp"
 27 #include "logging/log.hpp"
 28 #include "logging/logStream.hpp"
 29 #include "memory/resourceArea.hpp"
 30 #include "memory/universe.hpp"

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

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

 64 Monitor* StringDedup_lock             = nullptr;
 65 Mutex*   StringDedupIntern_lock       = nullptr;
 66 Monitor* CodeCache_lock               = nullptr;
 67 Mutex*   TouchedMethodLog_lock        = nullptr;
 68 Mutex*   RetData_lock                 = nullptr;
 69 Monitor* VMOperation_lock             = nullptr;
 70 Monitor* Threads_lock                 = nullptr;
 71 Mutex*   NonJavaThreadsList_lock      = nullptr;
 72 Mutex*   NonJavaThreadsListSync_lock  = nullptr;
 73 Monitor* CGC_lock                     = nullptr;
 74 Monitor* STS_lock                     = nullptr;
 75 Monitor* G1OldGCCount_lock            = nullptr;
 76 Mutex*   G1RareEvent_lock             = nullptr;
 77 Mutex*   G1DetachedRefinementStats_lock = nullptr;
 78 Mutex*   MarkStackFreeList_lock       = nullptr;
 79 Mutex*   MarkStackChunkList_lock      = nullptr;
 80 Mutex*   MonitoringSupport_lock       = nullptr;
 81 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
 82 Mutex*   Compile_lock                 = nullptr;
 83 Monitor* MethodCompileQueue_lock      = nullptr;





 84 Monitor* CompileThread_lock           = nullptr;
 85 Monitor* Compilation_lock             = nullptr;
 86 Mutex*   CompileTaskAlloc_lock        = nullptr;
 87 Mutex*   CompileStatistics_lock       = nullptr;
 88 Mutex*   DirectivesStack_lock         = nullptr;
 89 Mutex*   MultiArray_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 #ifndef PRODUCT
 96 Mutex*   FullGCALot_lock              = nullptr;
 97 #endif
 98 
 99 Mutex*   tty_lock                     = nullptr;
100 
101 Mutex*   RawMonitor_lock              = nullptr;
102 Mutex*   PerfDataMemAlloc_lock        = nullptr;
103 Mutex*   PerfDataManager_lock         = nullptr;
104 Mutex*   OopMapCacheAlloc_lock        = nullptr;
105 
106 Mutex*   FreeList_lock                = nullptr;
107 Mutex*   OldSets_lock                 = nullptr;
108 Mutex*   Uncommit_lock                = nullptr;
109 Monitor* RootRegionScan_lock          = nullptr;
110 
111 Mutex*   Management_lock              = nullptr;
112 Monitor* MonitorDeflation_lock        = nullptr;
113 Monitor* Service_lock                 = nullptr;
114 Monitor* Notification_lock            = nullptr;

131 Monitor* MetaspaceCritical_lock       = nullptr;
132 Mutex*   ClassLoaderDataGraph_lock    = nullptr;
133 Monitor* ThreadsSMRDelete_lock        = nullptr;
134 Mutex*   ThreadIdTableCreate_lock     = nullptr;
135 Mutex*   SharedDecoder_lock           = nullptr;
136 Mutex*   DCmdFactory_lock             = nullptr;
137 Mutex*   NMTQuery_lock                = nullptr;
138 Mutex*   NMTCompilationCostHistory_lock = nullptr;
139 
140 #if INCLUDE_CDS
141 #if INCLUDE_JVMTI
142 Mutex*   CDSClassFileStream_lock      = nullptr;
143 #endif
144 Mutex*   DumpTimeTable_lock           = nullptr;
145 Mutex*   CDSLambda_lock               = nullptr;
146 Mutex*   DumpRegion_lock              = nullptr;
147 Mutex*   ClassListFile_lock           = nullptr;
148 Mutex*   UnregisteredClassesTable_lock= nullptr;
149 Mutex*   LambdaFormInvokers_lock      = nullptr;
150 Mutex*   ScratchObjects_lock          = nullptr;

151 #endif // INCLUDE_CDS
152 Mutex*   Bootclasspath_lock           = nullptr;
153 
154 #if INCLUDE_JVMCI
155 Monitor* JVMCI_lock                   = nullptr;
156 Monitor* JVMCIRuntime_lock            = nullptr;
157 #endif
158 
159 
160 #define MAX_NUM_MUTEX 128
161 static Mutex* _mutex_array[MAX_NUM_MUTEX];
162 static int _num_mutex;
163 
164 #ifdef ASSERT
165 void assert_locked_or_safepoint(const Mutex* lock) {
166   if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
167   // check if this thread owns the lock (common case)
168   assert(lock != nullptr, "Need non-null lock");
169   if (lock->owned_by_self()) return;
170   if (SafepointSynchronize::is_at_safepoint()) return;
171   if (!Universe::is_fully_initialized()) return;
172   fatal("must own lock %s", lock->name());
173 }
174 
175 // a stronger assertion than the above
176 void assert_lock_strong(const Mutex* lock) {
177   if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
178   assert(lock != nullptr, "Need non-null lock");
179   if (lock->owned_by_self()) return;
180   fatal("must own lock %s", lock->name());
181 }
182 #endif
183 
184 static void add_mutex(Mutex* var) {
185   assert(_num_mutex < MAX_NUM_MUTEX, "increase MAX_NUM_MUTEX");
186   _mutex_array[_num_mutex++] = var;


187 }
188 
189 #define MUTEX_STORAGE_NAME(name) name##_storage
190 #define MUTEX_STORAGE(name, type) alignas(type) static uint8_t MUTEX_STORAGE_NAME(name)[sizeof(type)]
191 #define MUTEX_DEF(name, type, pri, ...) {                                                       \
192   assert(name == nullptr, "Mutex/Monitor initialized twice");                                   \
193   MUTEX_STORAGE(name, type);                                                                    \
194   name = ::new(static_cast<void*>(MUTEX_STORAGE_NAME(name))) type((pri), #name, ##__VA_ARGS__); \
195   add_mutex(name);                                                                              \
196 }
197 #define MUTEX_DEFN(name, type, pri, ...) MUTEX_DEF(name, type, Mutex::pri, ##__VA_ARGS__)
198 
199 // Specify relative ranked lock
200 #ifdef ASSERT
201 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEF(name, type, (held_lock)->rank() - 1, ##__VA_ARGS__)
202 #else
203 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEFN(name, type, safepoint, ##__VA_ARGS__)
204 #endif
205 
206 // Using Padded subclasses to prevent false sharing of these global monitors and mutexes.

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















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

317 #endif // INCLUDE_CDS
318   MUTEX_DEFN(Bootclasspath_lock              , PaddedMutex  , nosafepoint);
319 
320 #if INCLUDE_JVMCI
321   // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock
322   MUTEX_DEFN(JVMCIRuntime_lock               , PaddedMonitor, safepoint, true);
323 #endif
324 
325   // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
326   MUTEX_DEFL(InlineCacheBuffer_lock         , PaddedMutex  , CompiledIC_lock);
327   MUTEX_DEFL(VtableStubs_lock               , PaddedMutex  , CompiledIC_lock);  // Also holds DumpTimeTable_lock
328   MUTEX_DEFL(CodeCache_lock                 , PaddedMonitor, VtableStubs_lock);
329   MUTEX_DEFL(CompiledMethod_lock            , PaddedMutex  , CodeCache_lock);
330 
331   MUTEX_DEFL(Threads_lock                   , PaddedMonitor, CompileThread_lock, true);
332   MUTEX_DEFL(Compile_lock                   , PaddedMutex  , MethodCompileQueue_lock);
333   MUTEX_DEFL(Heap_lock                      , PaddedMonitor, AdapterHandlerLibrary_lock);
334 
335   MUTEX_DEFL(PerfDataMemAlloc_lock          , PaddedMutex  , Heap_lock);
336   MUTEX_DEFL(PerfDataManager_lock           , PaddedMutex  , Heap_lock);
337   MUTEX_DEFL(ClassLoaderDataGraph_lock      , PaddedMutex  , MultiArray_lock);
338   MUTEX_DEFL(VMOperation_lock               , PaddedMonitor, Heap_lock, true);
339   MUTEX_DEFL(ClassInitError_lock            , PaddedMonitor, Threads_lock);
340 
341   if (UseG1GC) {
342     MUTEX_DEFL(G1OldGCCount_lock            , PaddedMonitor, Threads_lock, true);
343     MUTEX_DEFL(G1RareEvent_lock             , PaddedMutex  , Threads_lock, true);
344   }
345 
346   MUTEX_DEFL(CompileTaskAlloc_lock          , PaddedMutex  ,  MethodCompileQueue_lock);
347 #ifdef INCLUDE_PARALLELGC
348   if (UseParallelGC) {
349     MUTEX_DEFL(PSOldGenExpand_lock          , PaddedMutex  , Heap_lock, true);
350   }
351 #endif
352   MUTEX_DEFL(OopMapCacheAlloc_lock          , PaddedMutex  ,  Threads_lock, true);
353   MUTEX_DEFL(Module_lock                    , PaddedMutex  ,  ClassLoaderDataGraph_lock);
354   MUTEX_DEFL(SystemDictionary_lock          , PaddedMonitor, Module_lock);
355   MUTEX_DEFL(JNICritical_lock               , PaddedMonitor, AdapterHandlerLibrary_lock); // used for JNI critical regions
356 #if INCLUDE_JVMCI
357   // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
358   MUTEX_DEFL(JVMCI_lock                     , PaddedMonitor, JVMCIRuntime_lock);
359 #endif

360 }
361 
362 #undef MUTEX_DEFL
363 #undef MUTEX_DEFN
364 #undef MUTEX_DEF
365 #undef MUTEX_STORAGE
366 #undef MUTEX_STORAGE_NAME
367 


































































































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

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "compiler/compiler_globals.hpp"
 27 #include "gc/shared/gc_globals.hpp"
 28 #include "logging/log.hpp"
 29 #include "logging/logStream.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "memory/universe.hpp"
 32 #include "runtime/java.hpp"
 33 #include "runtime/javaThread.hpp"
 34 #include "runtime/mutexLocker.hpp"
 35 #include "runtime/safepoint.hpp"
 36 #include "runtime/vmThread.hpp"
 37 #include "services/management.hpp"
 38 #include "utilities/vmError.hpp"
 39 
 40 // Mutexes used in the VM (see comment in mutexLocker.hpp):
 41 
 42 Mutex*   Patching_lock                = nullptr;
 43 Mutex*   CompiledMethod_lock          = nullptr;
 44 Monitor* SystemDictionary_lock        = nullptr;
 45 Mutex*   InvokeMethodTypeTable_lock   = nullptr;
 46 Monitor* InvokeMethodIntrinsicTable_lock = nullptr;
 47 Mutex*   SharedDictionary_lock        = nullptr;
 48 Monitor* ClassInitError_lock          = nullptr;
 49 Mutex*   Module_lock                  = nullptr;
 50 Mutex*   CompiledIC_lock              = nullptr;
 51 Mutex*   InlineCacheBuffer_lock       = nullptr;
 52 Mutex*   VMStatistic_lock             = nullptr;
 53 Mutex*   JmethodIdCreation_lock       = nullptr;
 54 Mutex*   JfieldIdCreation_lock        = nullptr;
 55 Monitor* JNICritical_lock             = nullptr;
 56 Mutex*   JvmtiThreadState_lock        = nullptr;
 57 Monitor* EscapeBarrier_lock           = nullptr;

 67 Monitor* StringDedup_lock             = nullptr;
 68 Mutex*   StringDedupIntern_lock       = nullptr;
 69 Monitor* CodeCache_lock               = nullptr;
 70 Mutex*   TouchedMethodLog_lock        = nullptr;
 71 Mutex*   RetData_lock                 = nullptr;
 72 Monitor* VMOperation_lock             = nullptr;
 73 Monitor* Threads_lock                 = nullptr;
 74 Mutex*   NonJavaThreadsList_lock      = nullptr;
 75 Mutex*   NonJavaThreadsListSync_lock  = nullptr;
 76 Monitor* CGC_lock                     = nullptr;
 77 Monitor* STS_lock                     = nullptr;
 78 Monitor* G1OldGCCount_lock            = nullptr;
 79 Mutex*   G1RareEvent_lock             = nullptr;
 80 Mutex*   G1DetachedRefinementStats_lock = nullptr;
 81 Mutex*   MarkStackFreeList_lock       = nullptr;
 82 Mutex*   MarkStackChunkList_lock      = nullptr;
 83 Mutex*   MonitoringSupport_lock       = nullptr;
 84 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
 85 Mutex*   Compile_lock                 = nullptr;
 86 Monitor* MethodCompileQueue_lock      = nullptr;
 87 Monitor* MethodCompileQueueC1_lock    = nullptr;
 88 Monitor* MethodCompileQueueC2_lock    = nullptr;
 89 Monitor* MethodCompileQueueC3_lock    = nullptr;
 90 Monitor* MethodCompileQueueSC1_lock   = nullptr;
 91 Monitor* MethodCompileQueueSC2_lock   = nullptr;
 92 Monitor* CompileThread_lock           = nullptr;
 93 Monitor* Compilation_lock             = nullptr;
 94 Mutex*   CompileTaskAlloc_lock        = nullptr;
 95 Mutex*   CompileStatistics_lock       = nullptr;
 96 Mutex*   DirectivesStack_lock         = nullptr;
 97 Mutex*   MultiArray_lock              = nullptr;
 98 Monitor* Terminator_lock              = nullptr;
 99 Monitor* InitCompleted_lock           = nullptr;
100 Monitor* BeforeExit_lock              = nullptr;
101 Monitor* Notify_lock                  = nullptr;
102 Mutex*   ExceptionCache_lock          = nullptr;
103 Mutex*   TrainingData_lock            = nullptr;
104 Monitor* TrainingReplayQueue_lock     = nullptr;
105 #ifndef PRODUCT
106 Mutex*   FullGCALot_lock              = nullptr;
107 #endif
108 
109 Mutex*   tty_lock                     = nullptr;
110 
111 Mutex*   RawMonitor_lock              = nullptr;
112 Mutex*   PerfDataMemAlloc_lock        = nullptr;
113 Mutex*   PerfDataManager_lock         = nullptr;
114 Mutex*   OopMapCacheAlloc_lock        = nullptr;
115 
116 Mutex*   FreeList_lock                = nullptr;
117 Mutex*   OldSets_lock                 = nullptr;
118 Mutex*   Uncommit_lock                = nullptr;
119 Monitor* RootRegionScan_lock          = nullptr;
120 
121 Mutex*   Management_lock              = nullptr;
122 Monitor* MonitorDeflation_lock        = nullptr;
123 Monitor* Service_lock                 = nullptr;
124 Monitor* Notification_lock            = nullptr;

141 Monitor* MetaspaceCritical_lock       = nullptr;
142 Mutex*   ClassLoaderDataGraph_lock    = nullptr;
143 Monitor* ThreadsSMRDelete_lock        = nullptr;
144 Mutex*   ThreadIdTableCreate_lock     = nullptr;
145 Mutex*   SharedDecoder_lock           = nullptr;
146 Mutex*   DCmdFactory_lock             = nullptr;
147 Mutex*   NMTQuery_lock                = nullptr;
148 Mutex*   NMTCompilationCostHistory_lock = nullptr;
149 
150 #if INCLUDE_CDS
151 #if INCLUDE_JVMTI
152 Mutex*   CDSClassFileStream_lock      = nullptr;
153 #endif
154 Mutex*   DumpTimeTable_lock           = nullptr;
155 Mutex*   CDSLambda_lock               = nullptr;
156 Mutex*   DumpRegion_lock              = nullptr;
157 Mutex*   ClassListFile_lock           = nullptr;
158 Mutex*   UnregisteredClassesTable_lock= nullptr;
159 Mutex*   LambdaFormInvokers_lock      = nullptr;
160 Mutex*   ScratchObjects_lock          = nullptr;
161 Mutex*   ArchivedObjectTables_lock    = nullptr;
162 #endif // INCLUDE_CDS
163 Mutex*   Bootclasspath_lock           = nullptr;
164 
165 #if INCLUDE_JVMCI
166 Monitor* JVMCI_lock                   = nullptr;
167 Monitor* JVMCIRuntime_lock            = nullptr;
168 #endif
169 
170 
171 #define MAX_NUM_MUTEX 128
172 static Mutex* _mutex_array[MAX_NUM_MUTEX];
173 static int _num_mutex;
174 
175 #ifdef ASSERT
176 void assert_locked_or_safepoint(const Mutex* lock) {
177   if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
178   // check if this thread owns the lock (common case)
179   assert(lock != nullptr, "Need non-null lock");
180   if (lock->owned_by_self()) return;
181   if (SafepointSynchronize::is_at_safepoint()) return;
182   if (!Universe::is_fully_initialized()) return;
183   fatal("must own lock %s", lock->name());
184 }
185 
186 // a stronger assertion than the above
187 void assert_lock_strong(const Mutex* lock) {
188   if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
189   assert(lock != nullptr, "Need non-null lock");
190   if (lock->owned_by_self()) return;
191   fatal("must own lock %s", lock->name());
192 }
193 #endif
194 
195 static void add_mutex(Mutex* var) {
196   assert(_num_mutex < MAX_NUM_MUTEX, "increase MAX_NUM_MUTEX");
197   int id = _num_mutex++;
198   _mutex_array[id] = var;
199 //  var->set_id(id);
200 }
201 
202 #define MUTEX_STORAGE_NAME(name) name##_storage
203 #define MUTEX_STORAGE(name, type) alignas(type) static uint8_t MUTEX_STORAGE_NAME(name)[sizeof(type)]
204 #define MUTEX_DEF(name, type, pri, ...) {                                                       \
205   assert(name == nullptr, "Mutex/Monitor initialized twice");                                   \
206   MUTEX_STORAGE(name, type);                                                                    \
207   name = ::new(static_cast<void*>(MUTEX_STORAGE_NAME(name))) type((pri), #name, ##__VA_ARGS__); \
208   add_mutex(name);                                                                              \
209 }
210 #define MUTEX_DEFN(name, type, pri, ...) MUTEX_DEF(name, type, Mutex::pri, ##__VA_ARGS__)
211 
212 // Specify relative ranked lock
213 #ifdef ASSERT
214 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEF(name, type, (held_lock)->rank() - 1, ##__VA_ARGS__)
215 #else
216 #define MUTEX_DEFL(name, type, held_lock, ...) MUTEX_DEFN(name, type, safepoint, ##__VA_ARGS__)
217 #endif
218 
219 // Using Padded subclasses to prevent false sharing of these global monitors and mutexes.

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


316 
317 #if INCLUDE_JFR
318   MUTEX_DEFN(JfrBuffer_lock                  , PaddedMutex  , event);
319   MUTEX_DEFN(JfrMsg_lock                     , PaddedMonitor, event);
320   MUTEX_DEFN(JfrStacktrace_lock              , PaddedMutex  , event);
321   MUTEX_DEFN(JfrThreadSampler_lock           , PaddedMonitor, nosafepoint);
322 #endif
323 
324   MUTEX_DEFN(ContinuationRelativize_lock     , PaddedMonitor, nosafepoint-3);
325   MUTEX_DEFN(CodeHeapStateAnalytics_lock     , PaddedMutex  , safepoint);
326   MUTEX_DEFN(ThreadsSMRDelete_lock           , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock
327   MUTEX_DEFN(ThreadIdTableCreate_lock        , PaddedMutex  , safepoint);
328   MUTEX_DEFN(SharedDecoder_lock              , PaddedMutex  , tty-1);
329   MUTEX_DEFN(DCmdFactory_lock                , PaddedMutex  , nosafepoint);
330   MUTEX_DEFN(NMTQuery_lock                   , PaddedMutex  , safepoint);
331   MUTEX_DEFN(NMTCompilationCostHistory_lock  , PaddedMutex  , nosafepoint);
332 #if INCLUDE_CDS
333 #if INCLUDE_JVMTI
334   MUTEX_DEFN(CDSClassFileStream_lock         , PaddedMutex  , safepoint);
335 #endif
336   MUTEX_DEFN(DumpTimeTable_lock              , PaddedMutex  , nosafepoint);
337   MUTEX_DEFN(CDSLambda_lock                  , PaddedMutex  , nosafepoint);
338   MUTEX_DEFN(DumpRegion_lock                 , PaddedMutex  , nosafepoint);
339   MUTEX_DEFN(ClassListFile_lock              , PaddedMutex  , nosafepoint);
340   MUTEX_DEFN(UnregisteredClassesTable_lock   , PaddedMutex  , nosafepoint-1);
341   MUTEX_DEFN(LambdaFormInvokers_lock         , PaddedMutex  , safepoint);
342   MUTEX_DEFN(ScratchObjects_lock             , PaddedMutex  , nosafepoint-1); // Holds DumpTimeTable_lock
343   MUTEX_DEFN(ArchivedObjectTables_lock       , PaddedMutex  , nosafepoint);
344 #endif // INCLUDE_CDS
345   MUTEX_DEFN(Bootclasspath_lock              , PaddedMutex  , nosafepoint);
346 
347 #if INCLUDE_JVMCI
348   // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock
349   MUTEX_DEFN(JVMCIRuntime_lock               , PaddedMonitor, safepoint, true);
350 #endif
351 
352   // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
353   MUTEX_DEFL(InlineCacheBuffer_lock         , PaddedMutex  , CompiledIC_lock);
354   MUTEX_DEFL(VtableStubs_lock               , PaddedMutex  , CompiledIC_lock);  // Also holds DumpTimeTable_lock
355   MUTEX_DEFL(CodeCache_lock                 , PaddedMonitor, VtableStubs_lock);
356   MUTEX_DEFL(CompiledMethod_lock            , PaddedMutex  , CodeCache_lock);
357 
358   MUTEX_DEFL(Threads_lock                   , PaddedMonitor, CompileThread_lock, true);
359   MUTEX_DEFL(Compile_lock                   , PaddedMutex  , MethodCompileQueue_lock);
360   MUTEX_DEFL(Heap_lock                      , PaddedMonitor, TrainingData_lock  /*AdapterHandlerLibrary_lock*/);
361 
362   MUTEX_DEFL(PerfDataMemAlloc_lock          , PaddedMutex  , Heap_lock);
363   MUTEX_DEFL(PerfDataManager_lock           , PaddedMutex  , Heap_lock);
364   MUTEX_DEFL(ClassLoaderDataGraph_lock      , PaddedMutex  , MultiArray_lock);
365   MUTEX_DEFL(VMOperation_lock               , PaddedMonitor, Heap_lock, true);
366   MUTEX_DEFL(ClassInitError_lock            , PaddedMonitor, Threads_lock);
367 
368   if (UseG1GC) {
369     MUTEX_DEFL(G1OldGCCount_lock            , PaddedMonitor, Threads_lock, true);
370     MUTEX_DEFL(G1RareEvent_lock             , PaddedMutex  , Threads_lock, true);
371   }
372 
373   MUTEX_DEFL(CompileTaskAlloc_lock          , PaddedMutex  ,  MethodCompileQueue_lock);
374 #ifdef INCLUDE_PARALLELGC
375   if (UseParallelGC) {
376     MUTEX_DEFL(PSOldGenExpand_lock          , PaddedMutex  , Heap_lock, true);
377   }
378 #endif
379   MUTEX_DEFL(OopMapCacheAlloc_lock          , PaddedMutex  ,  Threads_lock, true);
380   MUTEX_DEFL(Module_lock                    , PaddedMutex  ,  ClassLoaderDataGraph_lock);
381   MUTEX_DEFL(SystemDictionary_lock          , PaddedMonitor, Module_lock);
382   MUTEX_DEFL(JNICritical_lock               , PaddedMonitor, AdapterHandlerLibrary_lock); // used for JNI critical regions
383 #if INCLUDE_JVMCI
384   // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
385   MUTEX_DEFL(JVMCI_lock                     , PaddedMonitor, JVMCIRuntime_lock);
386 #endif
387 
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 int _num_names = 0;
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 void MutexLockerImpl::init_counters() {
406   if (ProfileVMLocks && UsePerfData) {
407     ResourceMark rm;
408     EXCEPTION_MARK;
409     _perf_lock_count     = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
410     _perf_lock_wait_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
411     _perf_lock_hold_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
412 
413     NEWPERFEVENTCOUNTER(_perf_lock_count[0],     SUN_RT, PerfDataManager::counter_name("Other", "Count"));
414     NEWPERFEVENTCOUNTER(_perf_lock_wait_time[0], SUN_RT, PerfDataManager::counter_name("Other", "BeforeTime"));
415     NEWPERFEVENTCOUNTER(_perf_lock_hold_time[0], SUN_RT, PerfDataManager::counter_name("Other", "AfterTime"));
416     for (int i = 0; i < MAX_NAMES; i++) {
417       ResourceMark rm;
418       const char* counter_name = _names[i];
419       if (counter_name == nullptr) {
420         stringStream ss;
421         ss.print("UnnamedMutex#%d", i);
422         counter_name = ss.as_string();
423       }
424       NEWPERFEVENTCOUNTER(_perf_lock_count[i + 1],     SUN_RT, PerfDataManager::counter_name(counter_name, "Count"));
425       NEWPERFEVENTCOUNTER(_perf_lock_wait_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "BeforeTime"));
426       NEWPERFEVENTCOUNTER(_perf_lock_hold_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "AfterTime"));
427     }
428     if (HAS_PENDING_EXCEPTION) {
429       vm_exit_during_initialization("MutexLockerImpl::init_counters() failed unexpectedly");
430     }
431   }
432 }
433 
434 int MutexLockerImpl::name2id(const char* name) {
435   if (ProfileVMLocks && UsePerfData) {
436     for (int i = 0; i < _num_names; i++) {
437       if (strcmp(_names[i], name) == 0) {
438         _is_unique[i] = false;
439         return i;
440       }
441     }
442     if (_num_names < MAX_NAMES) {
443       int new_id = _num_names++;
444       _names[new_id] = os::strdup(name, mtInternal);
445       _is_unique[new_id] = true;
446       return new_id;
447     }
448     log_debug(init)("Unnamed: %s", name); // no slots left
449   }
450   return -1;
451 }
452 
453 void MutexLockerImpl::print_counter_on(outputStream* st, const char* name, bool is_unique, int idx) {
454   jlong count = _perf_lock_count[idx]->get_value();
455   if (count > 0) {
456     st->print_cr("  %3d: %s%40s = %5ldms (%5ldms) / %9ld events", idx, (is_unique ? " " : "M"), name,
457                  Management::ticks_to_ms(_perf_lock_hold_time[idx]->get_value()),
458                  Management::ticks_to_ms(_perf_lock_wait_time[idx]->get_value()),
459                  count);
460   }
461 }
462 
463 static jlong accumulate_lock_counters(PerfCounter** lock_counters) {
464   jlong acc = 0;
465   for (int i = 0; i < _num_names + 1; i++) { // 0 slot is reserved for unnamed locks
466     if (lock_counters[i] == nullptr) {
467       break;
468     }
469     acc += lock_counters[i]->get_value();
470   }
471   return acc;
472 }
473 
474 void MutexLockerImpl::print_counters_on(outputStream* st) {
475   if (ProfileVMLocks && UsePerfData) {
476     jlong total_count     = accumulate_lock_counters(_perf_lock_count);
477     jlong total_wait_time = accumulate_lock_counters(_perf_lock_wait_time);
478     jlong total_hold_time = accumulate_lock_counters(_perf_lock_hold_time);
479 
480     st->print_cr("MutexLocker: Total: %d named locks (%d unique names); hold = %ldms (wait = %ldms) / %ld events for thread \"main\"",
481                  _num_mutex, _num_names,
482                  Management::ticks_to_ms(total_hold_time),
483                  Management::ticks_to_ms(total_wait_time),
484                  total_count);
485     for (int i = 0; i < _num_names; i++) {
486       print_counter_on(st, _names[i], _is_unique[i], i+1);
487     }
488     print_counter_on(st, "Unnamed / Other", false /*is_unique*/, 0);
489   } else {
490     st->print_cr("MutexLocker: no info (%s is disabled)", (UsePerfData ? "ProfileVMLocks" : "UsePerfData"));
491   }
492 }
493 
494 void MutexLockerImpl::post_initialize() {
495   // Print mutex ranks if requested.
496   LogTarget(Info, vmmutex) lt;
497   if (lt.is_enabled()) {
498     ResourceMark rm;
499     LogStream ls(lt);
500     print_lock_ranks(&ls);
501   }
502 }
503 
504 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
505   if (SafepointSynchronize::is_at_safepoint()) {
506     _locked = false;
507   } else {
508     _mutex = mutex;
509     _locked = true;
510     _mutex->lock();
511   }
512 }
513 
< prev index next >