Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8340547: Starting many threads can delay safepoints #1365

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/hotspot/share/prims/jvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2955,9 +2955,10 @@ JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
// We must release the Threads_lock before we can post a jvmti event
// in Thread::start.
{
MutexLocker throttle_ml(UseThreadsLockThrottleLock ? ThreadsLockThrottle_lock : nullptr);
// Ensure that the C++ Thread and OSThread structures aren't freed before
// we operate.
MutexLocker mu(Threads_lock);
MutexLocker ml(Threads_lock);

// Since JDK 5 the java.lang.Thread threadStatus is used to prevent
// re-starting an already started thread, so we should usually find
Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/runtime/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,10 @@ const int ObjectAlignmentInBytes = 8;
"more eagerly at the cost of higher overhead. A value of 0 " \
"(default) disables native heap trimming.") \
range(0, UINT_MAX) \
\
product(bool, UseThreadsLockThrottleLock, true, DIAGNOSTIC, \
"Use an extra lock during Thread start and exit to alleviate" \
"contention on Threads_lock.") \

// end of RUNTIME_FLAGS

Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/runtime/mutexLocker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Monitor* CodeCache_lock = nullptr;
Mutex* TouchedMethodLog_lock = nullptr;
Mutex* RetData_lock = nullptr;
Monitor* VMOperation_lock = nullptr;
Monitor* ThreadsLockThrottle_lock = nullptr;
Monitor* Threads_lock = nullptr;
Mutex* NonJavaThreadsList_lock = nullptr;
Mutex* NonJavaThreadsListSync_lock = nullptr;
Expand Down Expand Up @@ -326,6 +327,8 @@ void mutex_init() {
MUTEX_DEFN(JVMCIRuntime_lock , PaddedMonitor, safepoint, true);
#endif

MUTEX_DEFN(ThreadsLockThrottle_lock , PaddedMonitor, safepoint);

// These locks have relative rankings, and inherit safepoint checking attributes from that rank.
MUTEX_DEFL(InlineCacheBuffer_lock , PaddedMutex , CompiledIC_lock);
MUTEX_DEFL(VtableStubs_lock , PaddedMutex , CompiledIC_lock); // Also holds DumpTimeTable_lock
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/runtime/mutexLocker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ extern Monitor* CodeCache_lock; // a lock on the CodeCache
extern Mutex* TouchedMethodLog_lock; // a lock on allocation of LogExecutedMethods info
extern Mutex* RetData_lock; // a lock on installation of RetData inside method data
extern Monitor* VMOperation_lock; // a lock on queue of vm_operations waiting to execute
extern Monitor* ThreadsLockThrottle_lock; // used by Thread start/exit to reduce competition for Threads_lock,
// so a VM thread calling a safepoint is prioritized
extern Monitor* Threads_lock; // a lock on the Threads table of active Java threads
// (also used by Safepoints too to block threads creation/destruction)
extern Mutex* NonJavaThreadsList_lock; // a lock on the NonJavaThreads list
Expand Down
6 changes: 4 additions & 2 deletions src/hotspot/share/runtime/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,9 @@ void Threads::add(JavaThread* p, bool force_daemon) {
void Threads::remove(JavaThread* p, bool is_daemon) {
// Extra scope needed for Thread_lock, so we can check
// that we do not remove thread without safepoint code notice
{ MonitorLocker ml(Threads_lock);
{
MutexLocker throttle_ml(UseThreadsLockThrottleLock ? ThreadsLockThrottle_lock : nullptr);
MonitorLocker ml(Threads_lock);

if (ThreadIdTable::is_initialized()) {
// This cleanup must be done before the current thread's GC barrier
Expand Down Expand Up @@ -1065,7 +1067,7 @@ void Threads::remove(JavaThread* p, bool is_daemon) {

// Notify threads waiting in EscapeBarriers
EscapeBarrier::thread_removed(p);
} // unlock Threads_lock
} // unlock Threads_lock and ThreadsLockThrottle_lock

// Reduce the ObjectMonitor ceiling for the exiting thread.
ObjectSynchronizer::dec_in_use_list_ceiling();
Expand Down