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* VThreadTransition_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;
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 , nosafepoint); // 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(VThreadTransition_lock , PaddedMonitor, safepoint);
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);
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 , VThreadTransition_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* VThreadTransition_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;
251 MUTEX_DEFN(SignatureHandlerLibrary_lock , PaddedMutex , safepoint);
252 MUTEX_DEFN(SymbolArena_lock , PaddedMutex , nosafepoint);
253 MUTEX_DEFN(ExceptionCache_lock , PaddedMutex , safepoint);
254 #ifndef PRODUCT
255 MUTEX_DEFN(FullGCALot_lock , PaddedMutex , nosafepoint); // a lock to make FullGCALot MT safe
256 #endif
257 MUTEX_DEFN(BeforeExit_lock , PaddedMonitor, safepoint);
258
259 MUTEX_DEFN(NonJavaThreadsList_lock , PaddedMutex , nosafepoint-1);
260 MUTEX_DEFN(NonJavaThreadsListSync_lock , PaddedMutex , nosafepoint);
261
262 MUTEX_DEFN(RetData_lock , PaddedMutex , safepoint);
263 MUTEX_DEFN(Terminator_lock , PaddedMonitor, safepoint, true);
264 MUTEX_DEFN(InitCompleted_lock , PaddedMonitor, nosafepoint);
265 MUTEX_DEFN(Notify_lock , PaddedMonitor, safepoint, true);
266
267 MUTEX_DEFN(JfieldIdCreation_lock , PaddedMutex , safepoint);
268
269 MUTEX_DEFN(CompiledIC_lock , PaddedMutex , nosafepoint); // locks VtableStubs_lock
270 MUTEX_DEFN(MethodCompileQueue_lock , PaddedMonitor, safepoint);
271 if (UseGlobalCompileQueueLock) {
272 MethodCompileQueueC1_lock = MethodCompileQueue_lock;
273 MethodCompileQueueC2_lock = MethodCompileQueue_lock;
274 MethodCompileQueueSC1_lock = MethodCompileQueue_lock;
275 MethodCompileQueueSC2_lock = MethodCompileQueue_lock;
276 } else {
277 MUTEX_DEFN(MethodCompileQueueC1_lock , PaddedMonitor, safepoint);
278 MUTEX_DEFN(MethodCompileQueueC2_lock , PaddedMonitor, safepoint);
279 MUTEX_DEFN(MethodCompileQueueSC1_lock , PaddedMonitor, safepoint);
280 MUTEX_DEFN(MethodCompileQueueSC2_lock , PaddedMonitor, safepoint);
281 }
282 MUTEX_DEFN(TrainingData_lock , PaddedMutex , nosafepoint);
283 MUTEX_DEFN(TrainingReplayQueue_lock , PaddedMonitor, safepoint);
284 MUTEX_DEFN(CompileStatistics_lock , PaddedMutex , safepoint);
285 MUTEX_DEFN(DirectivesStack_lock , PaddedMutex , nosafepoint);
286
287 MUTEX_DEFN(VThreadTransition_lock , PaddedMonitor, safepoint);
288 MUTEX_DEFN(JvmtiVThreadSuspend_lock , PaddedMutex, nosafepoint-1);
289 MUTEX_DEFN(EscapeBarrier_lock , PaddedMonitor, nosafepoint); // Used to synchronize object reallocation/relocking triggered by JVMTI
290 MUTEX_DEFN(Management_lock , PaddedMutex , safepoint); // used for JVM management
291
292 MUTEX_DEFN(ConcurrentGCBreakpoints_lock , PaddedMonitor, safepoint, true);
293 MUTEX_DEFN(TouchedMethodLog_lock , PaddedMutex , safepoint);
294
295 MUTEX_DEFN(CompileThread_lock , PaddedMonitor, safepoint);
296 MUTEX_DEFN(PeriodicTask_lock , PaddedMonitor, safepoint, true);
297 MUTEX_DEFN(RedefineClasses_lock , PaddedMonitor, safepoint);
298 MUTEX_DEFN(Verify_lock , PaddedMutex , safepoint);
299 MUTEX_DEFN(ClassLoaderDataGraph_lock , PaddedMutex , safepoint);
300
301 MUTEX_DEFN(Compilation_lock , PaddedMonitor, nosafepoint);
302
303 #if INCLUDE_JFR
304 MUTEX_DEFN(JfrBuffer_lock , PaddedMutex , event);
305 MUTEX_DEFN(JfrMsg_lock , PaddedMonitor, event);
306 MUTEX_DEFN(JfrStacktrace_lock , PaddedMutex , event);
307 #endif
308
309 MUTEX_DEFN(ContinuationRelativize_lock , PaddedMonitor, nosafepoint-3);
310 MUTEX_DEFN(CodeHeapStateAnalytics_lock , PaddedMutex , safepoint);
311 MUTEX_DEFN(ThreadsSMRDelete_lock , PaddedMonitor, service-2); // Holds ConcurrentHashTableResize_lock
312 MUTEX_DEFN(ThreadIdTableCreate_lock , PaddedMutex , safepoint);
313 MUTEX_DEFN(DCmdFactory_lock , PaddedMutex , nosafepoint);
314 MUTEX_DEFN(NMTQuery_lock , PaddedMutex , safepoint);
315 MUTEX_DEFN(NMTCompilationCostHistory_lock , PaddedMutex , nosafepoint);
316 MUTEX_DEFN(NmtVirtualMemory_lock , PaddedMutex , service-4); // Must be lower than G1Mapper_lock used from G1RegionsSmallerThanCommitSizeMapper::commit_regions
317 #if INCLUDE_CDS
318 #if INCLUDE_JVMTI
319 MUTEX_DEFN(CDSClassFileStream_lock , PaddedMutex , safepoint);
320 #endif
321 MUTEX_DEFN(DumpTimeTable_lock , PaddedMutex , nosafepoint);
374 }
375 #endif
376 MUTEX_DEFL(SystemDictionary_lock , PaddedMonitor, Module_lock);
377 #if INCLUDE_JVMCI
378 // JVMCIRuntime_lock must be acquired before JVMCI_lock to avoid deadlock
379 MUTEX_DEFL(JVMCI_lock , PaddedMonitor, JVMCIRuntime_lock);
380 #endif
381 MUTEX_DEFL(JvmtiThreadState_lock , PaddedMutex , VThreadTransition_lock); // Used by JvmtiThreadState/JvmtiEventController
382 MUTEX_DEFL(SharedDecoder_lock , PaddedMutex , NmtVirtualMemory_lock); // Must be lower than NmtVirtualMemory_lock due to MemTracker::print_containing_region
383
384 // Allocate RecursiveMutex
385 MultiArray_lock = new RecursiveMutex();
386 }
387
388 #undef MUTEX_DEFL
389 #undef MUTEX_DEFN
390 #undef MUTEX_DEF
391 #undef MUTEX_STORAGE
392 #undef MUTEX_STORAGE_NAME
393
394 static const int MAX_NAMES = 200;
395 static const char* _names[MAX_NAMES] = { nullptr };
396 static bool _is_unique[MAX_NAMES] = { false };
397 static volatile int _num_names = 0;
398
399 static bool _mutex_init_done = false;
400
401 PerfCounter** MutexLockerImpl::_perf_lock_count = nullptr;
402 PerfCounter** MutexLockerImpl::_perf_lock_wait_time = nullptr;
403 PerfCounter** MutexLockerImpl::_perf_lock_hold_time = nullptr;
404
405 MutexLockerImpl::MutexLockerImpl(Mutex* mutex, Mutex::SafepointCheckFlag flag) :
406 _mutex(mutex),
407 _prof(ProfileVMLocks && Thread::current_or_null() != nullptr && Thread::current()->profile_vm_locks()) {
408
409 bool no_safepoint_check = flag == Mutex::_no_safepoint_check_flag;
410 if (_mutex != nullptr) {
411 if (_prof) { _before.start(); } // before
412
413 if (no_safepoint_check) {
414 _mutex->lock_without_safepoint_check();
415 } else {
416 _mutex->lock();
417 }
418
419 if (_prof) { _before.stop(); _after.start(); } // after
420 }
421 }
422
423 MutexLockerImpl::MutexLockerImpl(Thread* thread, Mutex* mutex, Mutex::SafepointCheckFlag flag) :
424 _mutex(mutex), _prof(thread->profile_vm_locks()) {
425
426 if (_prof) { _before.start(); } // before
427
428 bool no_safepoint_check = flag == Mutex::_no_safepoint_check_flag;
429 if (_mutex != nullptr) {
430 if (no_safepoint_check) {
431 _mutex->lock_without_safepoint_check(thread);
432 } else {
433 _mutex->lock(thread);
434 }
435 }
436
437 if (_prof) { _before.stop(); _after.start(); } // after
438 }
439
440 void MutexLockerImpl::init_counters() {
441 if (ProfileVMLocks && UsePerfData) {
442 ResourceMark rm;
443 EXCEPTION_MARK;
444 _perf_lock_count = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
445 _perf_lock_wait_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
446 _perf_lock_hold_time = NEW_C_HEAP_ARRAY(PerfCounter*, MAX_NAMES + 1, mtInternal);
447
448 NEWPERFEVENTCOUNTER(_perf_lock_count[0], SUN_RT, PerfDataManager::counter_name("Other", "Count"));
449 NEWPERFEVENTCOUNTER(_perf_lock_wait_time[0], SUN_RT, PerfDataManager::counter_name("Other", "BeforeTime"));
450 NEWPERFEVENTCOUNTER(_perf_lock_hold_time[0], SUN_RT, PerfDataManager::counter_name("Other", "AfterTime"));
451 for (int i = 0; i < MAX_NAMES; i++) {
452 ResourceMark rm;
453 const char* counter_name = _names[i];
454 if (counter_name == nullptr) {
455 stringStream ss;
456 ss.print("UnnamedMutex#%d", i);
457 counter_name = ss.as_string();
458 _names[i] = os::strdup(counter_name, mtInternal); // replace default nullptr
459 }
460 NEWPERFEVENTCOUNTER(_perf_lock_count[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "Count"));
461 NEWPERFEVENTCOUNTER(_perf_lock_wait_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "BeforeTime"));
462 NEWPERFEVENTCOUNTER(_perf_lock_hold_time[i + 1], SUN_RT, PerfDataManager::counter_name(counter_name, "AfterTime"));
463 }
464 if (HAS_PENDING_EXCEPTION) {
465 vm_exit_during_initialization("MutexLockerImpl::init_counters() failed unexpectedly");
466 }
467 }
468 _mutex_init_done = true;
469 }
470
471 int MutexLockerImpl::name2id(const char* name) {
472 if (ProfileVMLocks && UsePerfData) {
473 // There is not concurency or duplication in mutex_init().
474 if (!_mutex_init_done) {
475 int new_id = AtomicAccess::load(&_num_names);
476 precond(new_id < MAX_NAMES);
477 AtomicAccess::inc(&_num_names);
478 _names[new_id] = os::strdup(name, mtInternal);
479 _is_unique[new_id] = true;
480 return new_id;
481 }
482 int limit = AtomicAccess::load(&_num_names); // Cache static value which can be updated concurently
483 for (int i = Mutex::num_mutex(); i < limit; i++) {
484 if (strcmp(_names[i], name) == 0) {
485 _is_unique[i] = false;
486 return i;
487 }
488 }
489 if (limit < MAX_NAMES) {
490 int old_limit = limit;
491 const char* name_dup = os::strdup(name, mtInternal);
492 int new_id; // Get new id for this name
493 do {
494 new_id = limit++;
495 if (new_id == MAX_NAMES) break;
496 } while (AtomicAccess::cmpxchg(&_num_names, new_id, limit) != new_id);
497 for (int i = old_limit; i < new_id; i++) {
498 if (strcmp(_names[i], name) == 0) { // Other thread put it there
499 _is_unique[i] = false;
500 return i; // Wasted new_id slot to simplify code: _num_names is only incremented
501 }
502 }
503 if (new_id < MAX_NAMES) {
504 _names[new_id] = name_dup;
505 _is_unique[new_id] = true;
506 return new_id;
507 }
508 }
509 log_debug(init)("Unnamed: %s", name); // no slots left
510 }
511 return -1;
512 }
513
514 void MutexLockerImpl::print_counter_on(outputStream* st, const char* name, bool is_unique, int idx) {
515 jlong count = _perf_lock_count[idx]->get_value();
516 if (count > 0) {
517 st->print_cr(" %3d: %s%40s = " JLONG_FORMAT_W(5) "us (" JLONG_FORMAT_W(5) "us) / " JLONG_FORMAT_W(9) " events",
518 idx, (is_unique ? " " : "M"), name,
519 Management::ticks_to_us(_perf_lock_hold_time[idx]->get_value()),
520 Management::ticks_to_us(_perf_lock_wait_time[idx]->get_value()),
521 count);
522 }
523 }
524
525 static jlong accumulate_lock_counters(PerfCounter** lock_counters) {
526 jlong acc = 0;
527 for (int i = 0; i < _num_names + 1; i++) { // 0 slot is reserved for unnamed locks
528 if (lock_counters[i] == nullptr) {
529 break;
530 }
531 acc += lock_counters[i]->get_value();
532 }
533 return acc;
534 }
535
536 void MutexLockerImpl::print_counters_on(outputStream* st) {
537 if (ProfileVMLocks && UsePerfData) {
538 jlong total_count = accumulate_lock_counters(_perf_lock_count);
539 jlong total_wait_time = accumulate_lock_counters(_perf_lock_wait_time);
540 jlong total_hold_time = accumulate_lock_counters(_perf_lock_hold_time);
541
542 st->print_cr("MutexLocker: Total: %d named locks (%d unique names); hold = " JLONG_FORMAT "us (wait = " JLONG_FORMAT "us) / " JLONG_FORMAT " events for thread \"main\"",
543 Mutex::num_mutex(), _num_names,
544 Management::ticks_to_us(total_hold_time),
545 Management::ticks_to_us(total_wait_time),
546 total_count);
547 for (int i = 0; i < _num_names; i++) {
548 print_counter_on(st, _names[i], _is_unique[i], i+1);
549 }
550 print_counter_on(st, "Unnamed / Other", false /*is_unique*/, 0);
551 } else {
552 st->print_cr("MutexLocker: no info (%s is disabled)", (UsePerfData ? "ProfileVMLocks" : "UsePerfData"));
553 }
554 }
555
556 void MutexLockerImpl::post_initialize() {
557 // Print mutex ranks if requested.
558 LogTarget(Info, vmmutex) lt;
559 if (lt.is_enabled()) {
560 ResourceMark rm;
561 LogStream ls(lt);
562 Mutex::print_lock_ranks(&ls);
563 }
564 }
565
566 GCMutexLocker::GCMutexLocker(Mutex* mutex) {
567 if (SafepointSynchronize::is_at_safepoint()) {
568 _locked = false;
569 } else {
570 _mutex = mutex;
571 _locked = true;
572 _mutex->lock();
573 }
574 }
|