RFR: 8340547: Starting many threads can delay safepoints [v5]
Aleksey Shipilev
shade at openjdk.org
Mon Sep 30 08:18:37 UTC 2024
On Wed, 25 Sep 2024 09:00:09 GMT, Oli Gillespie <ogillespie at openjdk.org> wrote:
>> Mitigate the impact of JVM_StartThread on safepoint synchronization, by adding a new ThreadStart_lock which limits the number of JVM_StartThread invocations competing for Threads_lock at any given time to 1.
>> This gives a VM thread trying to call a safepoint a much better chance of acquiring Threads_lock when there are many JVM_StartThread invocations in flight, at the cost of one extra lock/unlock for every new thread.
>>
>> Can be disabled with new diagnostic flag `-XX:-UseExtraThreadStartLock`.
>>
>> Before (ThreadStartTtsp.java is shared in JDK-8340547):
>>
>> java -Xlog:safepoint ThreadStartTtsp.java | grep -o 'Reaching safepoint: [0-9]* ns'
>> Reaching safepoint: 1291591 ns
>> Reaching safepoint: 59962 ns
>> Reaching safepoint: 1958065 ns
>> Reaching safepoint: 14456666258 ns <-- 14 seconds!
>> ...
>>
>>
>> After:
>>
>> java -Xlog:safepoint ThreadStartTtsp.java | grep -o 'Reaching safepoint: [0-9]* ns'
>> Reaching safepoint: 214269 ns
>> Reaching safepoint: 60253 ns
>> Reaching safepoint: 2040680 ns
>> Reaching safepoint: 3089284 ns
>> Reaching safepoint: 2998303 ns
>> Reaching safepoint: 4433713 ns <-- 4.4ms
>> Reaching safepoint: 3368436 ns
>> Reaching safepoint: 2986519 ns
>> Reaching safepoint: 3269102 ns
>> ...
>>
>>
>>
>> **Alternatives**
>>
>> I considered some other options for mitigating this. For example, could we reduce the time spent holding the lock in StartThread? Most of the time is spent managing the threads list for ThreadSMR support, and each time we add a thread to that list we need to copy the whole list and free every entry in the original, which is slow. But I didn't see an easy way to avoid this.
>> I also looked at some kind of signal from the VM thread that it is ready to start synchronizing that StartThread could check before trying to grab Threads_lock, but I didn't find anything better than this extra lock.
>
> Oli Gillespie has updated the pull request incrementally with two additional commits since the last revision:
>
> - Improve doc
>
> Co-authored-by: David Holmes <62092539+dholmes-ora at users.noreply.github.com>
> - Improve comment
>
> Co-authored-by: David Holmes <62092539+dholmes-ora at users.noreply.github.com>
Looks fine, only cosmetics:
src/hotspot/share/prims/jvm.cpp line 2918:
> 2916: }
> 2917:
> 2918:
I think this double-new-line is deliberate style in this file.
src/hotspot/share/runtime/threads.cpp line 1032:
> 1030: {
> 1031: ConditionalMutexLocker ml1(ThreadsLockThrottle_lock, UseThreadsLockThrottleLock);
> 1032: MonitorLocker ml2(Threads_lock);
I thinking about the names here again. I think this is cleaner, as it does not require checking and fixing the uses of `ml`.
ConditionalMutexLocker throttle_ml(ThreadsLockThrottle_lock, UseThreadsLockThrottleLock);
MonitorLocker ml(Threads_lock);
-------------
Marked as reviewed by shade (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/21111#pullrequestreview-2336714026
PR Review Comment: https://git.openjdk.org/jdk/pull/21111#discussion_r1780632542
PR Review Comment: https://git.openjdk.org/jdk/pull/21111#discussion_r1780635083
More information about the hotspot-dev
mailing list