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* NMethodState_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;
54 Monitor* Heap_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* CGC_lock = nullptr;
73 Monitor* STS_lock = nullptr;
74 Monitor* G1OldGCCount_lock = nullptr;
75 Mutex* G1RareEvent_lock = nullptr;
76 Mutex* G1DetachedRefinementStats_lock = nullptr;
77 Mutex* MarkStackFreeList_lock = nullptr;
78 Mutex* MarkStackChunkList_lock = nullptr;
79 Mutex* MonitoringSupport_lock = nullptr;
80 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
81 Mutex* Compile_lock = nullptr;
82 Monitor* MethodCompileQueue_lock = nullptr;
83 Monitor* CompileThread_lock = nullptr;
84 Monitor* Compilation_lock = nullptr;
85 Mutex* CompileTaskAlloc_lock = nullptr;
86 Mutex* CompileStatistics_lock = nullptr;
87 Mutex* DirectivesStack_lock = nullptr;
88 Monitor* Terminator_lock = nullptr;
89 Monitor* InitCompleted_lock = nullptr;
90 Monitor* BeforeExit_lock = nullptr;
91 Monitor* Notify_lock = nullptr;
92 Mutex* ExceptionCache_lock = nullptr;
93 #ifndef PRODUCT
94 Mutex* FullGCALot_lock = nullptr;
95 #endif
96
97 Mutex* tty_lock = nullptr;
98
99 Mutex* RawMonitor_lock = nullptr;
100 Mutex* PerfDataMemAlloc_lock = nullptr;
101 Mutex* PerfDataManager_lock = nullptr;
102
103 Mutex* FreeList_lock = nullptr;
104 Mutex* OldSets_lock = nullptr;
105 Mutex* Uncommit_lock = nullptr;
106 Monitor* RootRegionScan_lock = nullptr;
107
108 Mutex* Management_lock = nullptr;
109 Monitor* MonitorDeflation_lock = nullptr;
110 Monitor* Service_lock = nullptr;
111 Monitor* Notification_lock = nullptr;
112 Monitor* PeriodicTask_lock = nullptr;
130 Monitor* MetaspaceCritical_lock = nullptr;
131 Mutex* ClassLoaderDataGraph_lock = nullptr;
132 Monitor* ThreadsSMRDelete_lock = nullptr;
133 Mutex* ThreadIdTableCreate_lock = nullptr;
134 Mutex* SharedDecoder_lock = nullptr;
135 Mutex* DCmdFactory_lock = nullptr;
136 Mutex* NMTQuery_lock = nullptr;
137 Mutex* NMTCompilationCostHistory_lock = nullptr;
138
139 #if INCLUDE_CDS
140 #if INCLUDE_JVMTI
141 Mutex* CDSClassFileStream_lock = nullptr;
142 #endif
143 Mutex* DumpTimeTable_lock = nullptr;
144 Mutex* CDSLambda_lock = nullptr;
145 Mutex* DumpRegion_lock = nullptr;
146 Mutex* ClassListFile_lock = nullptr;
147 Mutex* UnregisteredClassesTable_lock= nullptr;
148 Mutex* LambdaFormInvokers_lock = nullptr;
149 Mutex* ScratchObjects_lock = nullptr;
150 #endif // INCLUDE_CDS
151 Mutex* Bootclasspath_lock = nullptr;
152
153 #if INCLUDE_JVMCI
154 Monitor* JVMCI_lock = nullptr;
155 Monitor* JVMCIRuntime_lock = nullptr;
156 #endif
157
158 // Only one RecursiveMutex
159 RecursiveMutex* MultiArray_lock = nullptr;
160
161 #ifdef ASSERT
162 void assert_locked_or_safepoint(const Mutex* lock) {
163 if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
164 // check if this thread owns the lock (common case)
165 assert(lock != nullptr, "Need non-null lock");
166 if (lock->owned_by_self()) return;
167 if (SafepointSynchronize::is_at_safepoint()) return;
168 if (!Universe::is_fully_initialized()) return;
169 fatal("must own lock %s", lock->name());
236 MUTEX_DEFN(SignatureHandlerLibrary_lock , PaddedMutex , safepoint);
237 MUTEX_DEFN(SymbolArena_lock , PaddedMutex , nosafepoint);
238 MUTEX_DEFN(ExceptionCache_lock , PaddedMutex , safepoint);
239 #ifndef PRODUCT
240 MUTEX_DEFN(FullGCALot_lock , PaddedMutex , safepoint); // a lock to make FullGCALot MT safe
241 #endif
242 MUTEX_DEFN(BeforeExit_lock , PaddedMonitor, safepoint);
243
244 MUTEX_DEFN(NonJavaThreadsList_lock , PaddedMutex , nosafepoint-1);
245 MUTEX_DEFN(NonJavaThreadsListSync_lock , PaddedMutex , nosafepoint);
246
247 MUTEX_DEFN(RetData_lock , PaddedMutex , safepoint);
248 MUTEX_DEFN(Terminator_lock , PaddedMonitor, safepoint, true);
249 MUTEX_DEFN(InitCompleted_lock , PaddedMonitor, nosafepoint);
250 MUTEX_DEFN(Notify_lock , PaddedMonitor, safepoint, true);
251
252 MUTEX_DEFN(JfieldIdCreation_lock , PaddedMutex , safepoint);
253
254 MUTEX_DEFN(CompiledIC_lock , PaddedMutex , nosafepoint); // locks VtableStubs_lock
255 MUTEX_DEFN(MethodCompileQueue_lock , PaddedMonitor, safepoint);
256 MUTEX_DEFN(CompileStatistics_lock , PaddedMutex , safepoint);
257 MUTEX_DEFN(DirectivesStack_lock , PaddedMutex , nosafepoint);
258
259 MUTEX_DEFN(JvmtiVTMSTransition_lock , PaddedMonitor, safepoint); // used for Virtual Thread Mount State transition management
260 MUTEX_DEFN(EscapeBarrier_lock , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI
261 MUTEX_DEFN(Management_lock , PaddedMutex , safepoint); // used for JVM management
262
263 MUTEX_DEFN(ConcurrentGCBreakpoints_lock , PaddedMonitor, safepoint, true);
264 MUTEX_DEFN(TouchedMethodLog_lock , PaddedMutex , safepoint);
265
266 MUTEX_DEFN(CompileThread_lock , PaddedMonitor, safepoint);
267 MUTEX_DEFN(PeriodicTask_lock , PaddedMonitor, safepoint, true);
268 MUTEX_DEFN(RedefineClasses_lock , PaddedMonitor, safepoint);
269 MUTEX_DEFN(Verify_lock , PaddedMutex , safepoint);
270 MUTEX_DEFN(ClassLoaderDataGraph_lock , PaddedMutex , safepoint);
271
272 if (WhiteBoxAPI) {
273 MUTEX_DEFN(Compilation_lock , PaddedMonitor, nosafepoint);
274 }
275
276 #if INCLUDE_JFR
277 MUTEX_DEFN(JfrBuffer_lock , PaddedMutex , event);
278 MUTEX_DEFN(JfrMsg_lock , PaddedMonitor, event);
279 MUTEX_DEFN(JfrStacktrace_lock , PaddedMutex , event);
280 MUTEX_DEFN(JfrThreadSampler_lock , PaddedMonitor, nosafepoint);
281 #endif
282
283 MUTEX_DEFN(ContinuationRelativize_lock , PaddedMonitor, nosafepoint-3);
284 MUTEX_DEFN(CodeHeapStateAnalytics_lock , PaddedMutex , safepoint);
285 MUTEX_DEFN(ThreadsSMRDelete_lock , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock
286 MUTEX_DEFN(ThreadIdTableCreate_lock , PaddedMutex , safepoint);
287 MUTEX_DEFN(SharedDecoder_lock , PaddedMutex , tty-1);
288 MUTEX_DEFN(DCmdFactory_lock , PaddedMutex , nosafepoint);
289 MUTEX_DEFN(NMTQuery_lock , PaddedMutex , safepoint);
290 MUTEX_DEFN(NMTCompilationCostHistory_lock , PaddedMutex , nosafepoint);
291 #if INCLUDE_CDS
292 #if INCLUDE_JVMTI
293 MUTEX_DEFN(CDSClassFileStream_lock , PaddedMutex , safepoint);
294 #endif
295 MUTEX_DEFN(DumpTimeTable_lock , PaddedMutex , nosafepoint);
296 MUTEX_DEFN(CDSLambda_lock , PaddedMutex , nosafepoint);
297 MUTEX_DEFN(DumpRegion_lock , PaddedMutex , nosafepoint);
298 MUTEX_DEFN(ClassListFile_lock , PaddedMutex , nosafepoint);
299 MUTEX_DEFN(UnregisteredClassesTable_lock , PaddedMutex , nosafepoint-1);
300 MUTEX_DEFN(LambdaFormInvokers_lock , PaddedMutex , safepoint);
301 MUTEX_DEFN(ScratchObjects_lock , PaddedMutex , nosafepoint-1); // Holds DumpTimeTable_lock
302 #endif // INCLUDE_CDS
303 MUTEX_DEFN(Bootclasspath_lock , PaddedMutex , nosafepoint);
304
305 #if INCLUDE_JVMCI
306 // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock
307 MUTEX_DEFN(JVMCIRuntime_lock , PaddedMonitor, safepoint, true);
308 #endif
309
310 MUTEX_DEFN(ThreadsLockThrottle_lock , PaddedMonitor, safepoint);
311
312 // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
313 MUTEX_DEFL(VtableStubs_lock , PaddedMutex , CompiledIC_lock); // Also holds DumpTimeTable_lock
314 MUTEX_DEFL(CodeCache_lock , PaddedMonitor, VtableStubs_lock);
315 MUTEX_DEFL(NMethodState_lock , PaddedMutex , CodeCache_lock);
316
317 // tty_lock is held when printing nmethod and its relocations which use this lock.
318 MUTEX_DEFL(ExternalsRecorder_lock , PaddedMutex , tty_lock);
319
320 MUTEX_DEFL(Threads_lock , PaddedMonitor, CompileThread_lock, true);
321 MUTEX_DEFL(Compile_lock , PaddedMutex , MethodCompileQueue_lock);
322 MUTEX_DEFL(Heap_lock , PaddedMonitor, AdapterHandlerLibrary_lock);
323
324 MUTEX_DEFL(PerfDataMemAlloc_lock , PaddedMutex , Heap_lock);
325 MUTEX_DEFL(PerfDataManager_lock , PaddedMutex , Heap_lock);
326 MUTEX_DEFL(VMOperation_lock , PaddedMonitor, Heap_lock, true);
327 MUTEX_DEFL(ClassInitError_lock , PaddedMonitor, Threads_lock);
328
329 if (UseG1GC) {
330 MUTEX_DEFL(G1OldGCCount_lock , PaddedMonitor, Threads_lock, true);
331 MUTEX_DEFL(G1RareEvent_lock , PaddedMutex , Threads_lock, true);
332 }
333
334 MUTEX_DEFL(CompileTaskAlloc_lock , PaddedMutex , MethodCompileQueue_lock);
335 #ifdef INCLUDE_PARALLELGC
336 if (UseParallelGC) {
337 MUTEX_DEFL(PSOldGenExpand_lock , PaddedMutex , Heap_lock, true);
338 }
339 #endif
340 MUTEX_DEFL(Module_lock , PaddedMutex , ClassLoaderDataGraph_lock);
341 MUTEX_DEFL(SystemDictionary_lock , PaddedMonitor, Module_lock);
342 MUTEX_DEFL(JNICritical_lock , PaddedMonitor, AdapterHandlerLibrary_lock); // used for JNI critical regions
343 #if INCLUDE_JVMCI
344 // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
345 MUTEX_DEFL(JVMCI_lock , PaddedMonitor, JVMCIRuntime_lock);
346 #endif
347 MUTEX_DEFL(JvmtiThreadState_lock , PaddedMutex , JvmtiVTMSTransition_lock); // Used by JvmtiThreadState/JvmtiEventController
348
349 // Allocate RecursiveMutex
350 MultiArray_lock = new RecursiveMutex();
351 }
352
353 #undef MUTEX_DEFL
354 #undef MUTEX_DEFN
355 #undef MUTEX_DEF
356 #undef MUTEX_STORAGE
357 #undef MUTEX_STORAGE_NAME
358
359 void MutexLockerImpl::post_initialize() {
360 // Print mutex ranks if requested.
361 LogTarget(Info, vmmutex) lt;
362 if (lt.is_enabled()) {
363 ResourceMark rm;
364 LogStream ls(lt);
365 Mutex::print_lock_ranks(&ls);
366 }
367 }
368
369 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
370 if (SafepointSynchronize::is_at_safepoint()) {
371 _locked = false;
372 } else {
373 _mutex = mutex;
374 _locked = true;
375 _mutex->lock();
376 }
377 }
378
|
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* 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 Monitor* Heap_lock = nullptr;
66 Mutex* StringDedupIntern_lock = nullptr;
67 Monitor* CodeCache_lock = nullptr;
68 Mutex* TouchedMethodLog_lock = nullptr;
69 Mutex* RetData_lock = nullptr;
70 Monitor* VMOperation_lock = nullptr;
71 Monitor* ThreadsLockThrottle_lock = nullptr;
72 Monitor* Threads_lock = nullptr;
73 Mutex* NonJavaThreadsList_lock = nullptr;
74 Mutex* NonJavaThreadsListSync_lock = nullptr;
75 Monitor* CGC_lock = nullptr;
76 Monitor* STS_lock = nullptr;
77 Monitor* G1OldGCCount_lock = nullptr;
78 Mutex* G1RareEvent_lock = nullptr;
79 Mutex* G1DetachedRefinementStats_lock = nullptr;
80 Mutex* MarkStackFreeList_lock = nullptr;
81 Mutex* MarkStackChunkList_lock = nullptr;
82 Mutex* MonitoringSupport_lock = nullptr;
83 Monitor* ConcurrentGCBreakpoints_lock = nullptr;
84 Mutex* Compile_lock = nullptr;
85 Monitor* MethodCompileQueue_lock = nullptr;
86 Monitor* MethodCompileQueueC1_lock = nullptr;
87 Monitor* MethodCompileQueueC2_lock = nullptr;
88 Monitor* MethodCompileQueueC3_lock = nullptr;
89 Monitor* MethodCompileQueueSC1_lock = nullptr;
90 Monitor* MethodCompileQueueSC2_lock = nullptr;
91 Monitor* CompileThread_lock = nullptr;
92 Monitor* Compilation_lock = nullptr;
93 Mutex* CompileTaskAlloc_lock = nullptr;
94 Mutex* CompileStatistics_lock = nullptr;
95 Mutex* DirectivesStack_lock = nullptr;
96 Monitor* Terminator_lock = nullptr;
97 Monitor* InitCompleted_lock = nullptr;
98 Monitor* BeforeExit_lock = nullptr;
99 Monitor* Notify_lock = nullptr;
100 Mutex* ExceptionCache_lock = nullptr;
101 Mutex* TrainingData_lock = nullptr;
102 Monitor* TrainingReplayQueue_lock = nullptr;
103 #ifndef PRODUCT
104 Mutex* FullGCALot_lock = nullptr;
105 #endif
106
107 Mutex* tty_lock = nullptr;
108
109 Mutex* RawMonitor_lock = nullptr;
110 Mutex* PerfDataMemAlloc_lock = nullptr;
111 Mutex* PerfDataManager_lock = nullptr;
112
113 Mutex* FreeList_lock = nullptr;
114 Mutex* OldSets_lock = nullptr;
115 Mutex* Uncommit_lock = nullptr;
116 Monitor* RootRegionScan_lock = nullptr;
117
118 Mutex* Management_lock = nullptr;
119 Monitor* MonitorDeflation_lock = nullptr;
120 Monitor* Service_lock = nullptr;
121 Monitor* Notification_lock = nullptr;
122 Monitor* PeriodicTask_lock = nullptr;
140 Monitor* MetaspaceCritical_lock = nullptr;
141 Mutex* ClassLoaderDataGraph_lock = nullptr;
142 Monitor* ThreadsSMRDelete_lock = nullptr;
143 Mutex* ThreadIdTableCreate_lock = nullptr;
144 Mutex* SharedDecoder_lock = nullptr;
145 Mutex* DCmdFactory_lock = nullptr;
146 Mutex* NMTQuery_lock = nullptr;
147 Mutex* NMTCompilationCostHistory_lock = nullptr;
148
149 #if INCLUDE_CDS
150 #if INCLUDE_JVMTI
151 Mutex* CDSClassFileStream_lock = nullptr;
152 #endif
153 Mutex* DumpTimeTable_lock = nullptr;
154 Mutex* CDSLambda_lock = nullptr;
155 Mutex* DumpRegion_lock = nullptr;
156 Mutex* ClassListFile_lock = nullptr;
157 Mutex* UnregisteredClassesTable_lock= nullptr;
158 Mutex* LambdaFormInvokers_lock = nullptr;
159 Mutex* ScratchObjects_lock = nullptr;
160 Mutex* ArchivedObjectTables_lock = nullptr;
161 #endif // INCLUDE_CDS
162 Mutex* Bootclasspath_lock = nullptr;
163
164 #if INCLUDE_JVMCI
165 Monitor* JVMCI_lock = nullptr;
166 Monitor* JVMCIRuntime_lock = nullptr;
167 #endif
168
169 // Only one RecursiveMutex
170 RecursiveMutex* MultiArray_lock = nullptr;
171
172 #ifdef ASSERT
173 void assert_locked_or_safepoint(const Mutex* lock) {
174 if (DebuggingContext::is_enabled() || VMError::is_error_reported()) return;
175 // check if this thread owns the lock (common case)
176 assert(lock != nullptr, "Need non-null lock");
177 if (lock->owned_by_self()) return;
178 if (SafepointSynchronize::is_at_safepoint()) return;
179 if (!Universe::is_fully_initialized()) return;
180 fatal("must own lock %s", lock->name());
247 MUTEX_DEFN(SignatureHandlerLibrary_lock , PaddedMutex , safepoint);
248 MUTEX_DEFN(SymbolArena_lock , PaddedMutex , nosafepoint);
249 MUTEX_DEFN(ExceptionCache_lock , PaddedMutex , safepoint);
250 #ifndef PRODUCT
251 MUTEX_DEFN(FullGCALot_lock , PaddedMutex , safepoint); // a lock to make FullGCALot MT safe
252 #endif
253 MUTEX_DEFN(BeforeExit_lock , PaddedMonitor, safepoint);
254
255 MUTEX_DEFN(NonJavaThreadsList_lock , PaddedMutex , nosafepoint-1);
256 MUTEX_DEFN(NonJavaThreadsListSync_lock , PaddedMutex , nosafepoint);
257
258 MUTEX_DEFN(RetData_lock , PaddedMutex , safepoint);
259 MUTEX_DEFN(Terminator_lock , PaddedMonitor, safepoint, true);
260 MUTEX_DEFN(InitCompleted_lock , PaddedMonitor, nosafepoint);
261 MUTEX_DEFN(Notify_lock , PaddedMonitor, safepoint, true);
262
263 MUTEX_DEFN(JfieldIdCreation_lock , PaddedMutex , safepoint);
264
265 MUTEX_DEFN(CompiledIC_lock , PaddedMutex , nosafepoint); // locks VtableStubs_lock
266 MUTEX_DEFN(MethodCompileQueue_lock , PaddedMonitor, safepoint);
267 if (UseGlobalCompileQueueLock) {
268 MethodCompileQueueC1_lock = MethodCompileQueue_lock;
269 MethodCompileQueueC2_lock = MethodCompileQueue_lock;
270 MethodCompileQueueC3_lock = MethodCompileQueue_lock;
271 MethodCompileQueueSC1_lock = MethodCompileQueue_lock;
272 MethodCompileQueueSC2_lock = MethodCompileQueue_lock;
273 } else {
274 MUTEX_DEFN(MethodCompileQueueC1_lock , PaddedMonitor, safepoint);
275 MUTEX_DEFN(MethodCompileQueueC2_lock , PaddedMonitor, safepoint);
276 MUTEX_DEFN(MethodCompileQueueC3_lock , PaddedMonitor, safepoint);
277 MUTEX_DEFN(MethodCompileQueueSC1_lock , PaddedMonitor, safepoint);
278 MUTEX_DEFN(MethodCompileQueueSC2_lock , PaddedMonitor, safepoint);
279 }
280 MUTEX_DEFL(TrainingData_lock , PaddedMutex , MethodCompileQueue_lock);
281 MUTEX_DEFN(TrainingReplayQueue_lock , PaddedMonitor, safepoint);
282 MUTEX_DEFN(CompileStatistics_lock , PaddedMutex , safepoint);
283 MUTEX_DEFN(DirectivesStack_lock , PaddedMutex , nosafepoint);
284
285 MUTEX_DEFN(JvmtiVTMSTransition_lock , PaddedMonitor, safepoint); // used for Virtual Thread Mount State transition management
286 MUTEX_DEFN(EscapeBarrier_lock , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI
287 MUTEX_DEFN(Management_lock , PaddedMutex , safepoint); // used for JVM management
288
289 MUTEX_DEFN(ConcurrentGCBreakpoints_lock , PaddedMonitor, safepoint, true);
290 MUTEX_DEFN(TouchedMethodLog_lock , PaddedMutex , safepoint);
291
292 MUTEX_DEFN(CompileThread_lock , PaddedMonitor, safepoint);
293 MUTEX_DEFN(PeriodicTask_lock , PaddedMonitor, safepoint, true);
294 MUTEX_DEFN(RedefineClasses_lock , PaddedMonitor, safepoint);
295 MUTEX_DEFN(Verify_lock , PaddedMutex , safepoint);
296 MUTEX_DEFN(ClassLoaderDataGraph_lock , PaddedMutex , safepoint);
297
298 MUTEX_DEFN(Compilation_lock , PaddedMonitor, nosafepoint);
299
300 #if INCLUDE_JFR
301 MUTEX_DEFN(JfrBuffer_lock , PaddedMutex , event);
302 MUTEX_DEFN(JfrMsg_lock , PaddedMonitor, event);
303 MUTEX_DEFN(JfrStacktrace_lock , PaddedMutex , event);
304 MUTEX_DEFN(JfrThreadSampler_lock , PaddedMonitor, nosafepoint);
305 #endif
306
307 MUTEX_DEFN(ContinuationRelativize_lock , PaddedMonitor, nosafepoint-3);
308 MUTEX_DEFN(CodeHeapStateAnalytics_lock , PaddedMutex , safepoint);
309 MUTEX_DEFN(ThreadsSMRDelete_lock , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock
310 MUTEX_DEFN(ThreadIdTableCreate_lock , PaddedMutex , safepoint);
311 MUTEX_DEFN(SharedDecoder_lock , PaddedMutex , tty-1);
312 MUTEX_DEFN(DCmdFactory_lock , PaddedMutex , nosafepoint);
313 MUTEX_DEFN(NMTQuery_lock , PaddedMutex , safepoint);
314 MUTEX_DEFN(NMTCompilationCostHistory_lock , PaddedMutex , nosafepoint);
315 #if INCLUDE_CDS
316 #if INCLUDE_JVMTI
317 MUTEX_DEFN(CDSClassFileStream_lock , PaddedMutex , safepoint);
318 #endif
319 MUTEX_DEFN(DumpTimeTable_lock , PaddedMutex , nosafepoint);
320 MUTEX_DEFN(CDSLambda_lock , PaddedMutex , nosafepoint);
321 MUTEX_DEFN(DumpRegion_lock , PaddedMutex , nosafepoint);
322 MUTEX_DEFN(ClassListFile_lock , PaddedMutex , nosafepoint);
323 MUTEX_DEFN(UnregisteredClassesTable_lock , PaddedMutex , nosafepoint-1);
324 MUTEX_DEFN(LambdaFormInvokers_lock , PaddedMutex , safepoint);
325 MUTEX_DEFN(ScratchObjects_lock , PaddedMutex , nosafepoint-1); // Holds DumpTimeTable_lock
326 MUTEX_DEFN(ArchivedObjectTables_lock , PaddedMutex , nosafepoint);
327 #endif // INCLUDE_CDS
328 MUTEX_DEFN(Bootclasspath_lock , PaddedMutex , nosafepoint);
329
330 #if INCLUDE_JVMCI
331 // JVMCIRuntime::_lock must be acquired before JVMCI_lock to avoid deadlock
332 MUTEX_DEFN(JVMCIRuntime_lock , PaddedMonitor, safepoint, true);
333 #endif
334
335 MUTEX_DEFN(ThreadsLockThrottle_lock , PaddedMonitor, safepoint);
336
337 // These locks have relative rankings, and inherit safepoint checking attributes from that rank.
338 MUTEX_DEFL(VtableStubs_lock , PaddedMutex , CompiledIC_lock); // Also holds DumpTimeTable_lock
339 MUTEX_DEFL(CodeCache_lock , PaddedMonitor, VtableStubs_lock);
340 MUTEX_DEFL(NMethodState_lock , PaddedMutex , CodeCache_lock);
341
342 // tty_lock is held when printing nmethod and its relocations which use this lock.
343 MUTEX_DEFL(ExternalsRecorder_lock , PaddedMutex , tty_lock);
344
345 MUTEX_DEFL(Threads_lock , PaddedMonitor, CompileThread_lock, true);
346 MUTEX_DEFL(Compile_lock , PaddedMutex , MethodCompileQueue_lock);
347 MUTEX_DEFL(Heap_lock , PaddedMonitor, TrainingData_lock /*AdapterHandlerLibrary_lock*/);
348
349 MUTEX_DEFL(PerfDataMemAlloc_lock , PaddedMutex , Heap_lock);
350 MUTEX_DEFL(PerfDataManager_lock , PaddedMutex , Heap_lock);
351 MUTEX_DEFL(VMOperation_lock , PaddedMonitor, Heap_lock, true);
352 MUTEX_DEFL(ClassInitError_lock , PaddedMonitor, Threads_lock);
353
354 if (UseG1GC) {
355 MUTEX_DEFL(G1OldGCCount_lock , PaddedMonitor, Threads_lock, true);
356 MUTEX_DEFL(G1RareEvent_lock , PaddedMutex , Threads_lock, true);
357 }
358
359 MUTEX_DEFL(CompileTaskAlloc_lock , PaddedMutex , MethodCompileQueue_lock);
360 #ifdef INCLUDE_PARALLELGC
361 if (UseParallelGC) {
362 MUTEX_DEFL(PSOldGenExpand_lock , PaddedMutex , Heap_lock, true);
363 }
364 #endif
365 MUTEX_DEFL(Module_lock , PaddedMutex , ClassLoaderDataGraph_lock);
366 MUTEX_DEFL(SystemDictionary_lock , PaddedMonitor, Module_lock);
367 MUTEX_DEFL(JNICritical_lock , PaddedMonitor, AdapterHandlerLibrary_lock); // used for JNI critical regions
368 #if INCLUDE_JVMCI
369 // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
370 MUTEX_DEFL(JVMCI_lock , PaddedMonitor, JVMCIRuntime_lock);
371 #endif
372 MUTEX_DEFL(JvmtiThreadState_lock , PaddedMutex , JvmtiVTMSTransition_lock); // Used by JvmtiThreadState/JvmtiEventController
373
374 // Allocate RecursiveMutex
375 MultiArray_lock = new RecursiveMutex();
376 }
377
378 #undef MUTEX_DEFL
379 #undef MUTEX_DEFN
380 #undef MUTEX_DEF
381 #undef MUTEX_STORAGE
382 #undef MUTEX_STORAGE_NAME
383
384 static const int MAX_NAMES = 200;
385 static const char* _names[MAX_NAMES] = { nullptr };
386 static bool _is_unique[MAX_NAMES] = { false };
387 static int _num_names = 0;
388
389 PerfCounter** MutexLockerImpl::_perf_lock_count = nullptr;
390 PerfCounter** MutexLockerImpl::_perf_lock_wait_time = nullptr;
391 PerfCounter** MutexLockerImpl::_perf_lock_hold_time = nullptr;
392
393 void MutexLockerImpl::init_counters() {
394 if (ProfileVMLocks && UsePerfData) {
395 ResourceMark rm;
396 EXCEPTION_MARK;
397 _perf_lock_count = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
398 _perf_lock_wait_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
399 _perf_lock_hold_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
400
401 NEWPERFEVENTCOUNTER(_perf_lock_count[0], SUN_RT, PerfDataManager::counter_name("Other", "Count"));
402 NEWPERFEVENTCOUNTER(_perf_lock_wait_time[0], SUN_RT, PerfDataManager::counter_name("Other", "BeforeTime"));
403 NEWPERFEVENTCOUNTER(_perf_lock_hold_time[0], SUN_RT, PerfDataManager::counter_name("Other", "AfterTime"));
404 for (int i = 0; i < MAX_NAMES; i++) {
405 ResourceMark rm;
406 const char* counter_name = _names[i];
407 if (counter_name == nullptr) {
408 stringStream ss;
409 ss.print("UnnamedMutex#%d", i);
410 counter_name = ss.as_string();
411 }
412 NEWPERFEVENTCOUNTER(_perf_lock_count[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "Count"));
413 NEWPERFEVENTCOUNTER(_perf_lock_wait_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "BeforeTime"));
414 NEWPERFEVENTCOUNTER(_perf_lock_hold_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "AfterTime"));
415 }
416 if (HAS_PENDING_EXCEPTION) {
417 vm_exit_during_initialization("MutexLockerImpl::init_counters() failed unexpectedly");
418 }
419 }
420 }
421
422 int MutexLockerImpl::name2id(const char* name) {
423 if (ProfileVMLocks && UsePerfData) {
424 for (int i = 0; i < _num_names; i++) {
425 if (strcmp(_names[i], name) == 0) {
426 _is_unique[i] = false;
427 return i;
428 }
429 }
430 if (_num_names < MAX_NAMES) {
431 int new_id = _num_names++;
432 _names[new_id] = os::strdup(name, mtInternal);
433 _is_unique[new_id] = true;
434 return new_id;
435 }
436 log_debug(init)("Unnamed: %s", name); // no slots left
437 }
438 return -1;
439 }
440
441 void MutexLockerImpl::print_counter_on(outputStream* st, const char* name, bool is_unique, int idx) {
442 jlong count = _perf_lock_count[idx]->get_value();
443 if (count > 0) {
444 st->print_cr(" %3d: %s%40s = " JLONG_FORMAT_W(5) "us (" JLONG_FORMAT_W(5) "us) / " JLONG_FORMAT_W(9) " events",
445 idx, (is_unique ? " " : "M"), name,
446 Management::ticks_to_us(_perf_lock_hold_time[idx]->get_value()),
447 Management::ticks_to_us(_perf_lock_wait_time[idx]->get_value()),
448 count);
449 }
450 }
451
452 static jlong accumulate_lock_counters(PerfCounter** lock_counters) {
453 jlong acc = 0;
454 for (int i = 0; i < _num_names + 1; i++) { // 0 slot is reserved for unnamed locks
455 if (lock_counters[i] == nullptr) {
456 break;
457 }
458 acc += lock_counters[i]->get_value();
459 }
460 return acc;
461 }
462
463 void MutexLockerImpl::print_counters_on(outputStream* st) {
464 if (ProfileVMLocks && UsePerfData) {
465 jlong total_count = accumulate_lock_counters(_perf_lock_count);
466 jlong total_wait_time = accumulate_lock_counters(_perf_lock_wait_time);
467 jlong total_hold_time = accumulate_lock_counters(_perf_lock_hold_time);
468
469 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\"",
470 Mutex::num_mutex(), _num_names,
471 Management::ticks_to_us(total_hold_time),
472 Management::ticks_to_us(total_wait_time),
473 total_count);
474 for (int i = 0; i < _num_names; i++) {
475 print_counter_on(st, _names[i], _is_unique[i], i+1);
476 }
477 print_counter_on(st, "Unnamed / Other", false /*is_unique*/, 0);
478 } else {
479 st->print_cr("MutexLocker: no info (%s is disabled)", (UsePerfData ? "ProfileVMLocks" : "UsePerfData"));
480 }
481 }
482
483 void MutexLockerImpl::post_initialize() {
484 // Print mutex ranks if requested.
485 LogTarget(Info, vmmutex) lt;
486 if (lt.is_enabled()) {
487 ResourceMark rm;
488 LogStream ls(lt);
489 Mutex::print_lock_ranks(&ls);
490 }
491 }
492
493 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
494 if (SafepointSynchronize::is_at_safepoint()) {
495 _locked = false;
496 } else {
497 _mutex = mutex;
498 _locked = true;
499 _mutex->lock();
500 }
501 }
502
|