On Wed, 2 Dec 2026 16:53:54 GMT, Fredrik Bredberg <[email protected]> wrote:

>> This PR removes the type stable memory aspect of `ParkEvent`.
>> 
>> The concept of type stable memory means that once it's been allocated, it 
>> will forever be of a certain type (like a `ParkEvent`). Forever means that 
>> it will not be freed for as long as the VM lives.
>> 
>> Keeping the park events in type stable memory had the unfortunate 
>> consequence that we needed to hold on to memory after a thread population 
>> spike.
>> 
>> Since the JVMTI raw monitor supports non-JavaThread users, we can not use 
>> the Thread-SMR (Safe Memory Reclamation) mechanism to make sure the park 
>> event is kept live while operating on it. Because the Thread-SMR mechanism 
>> can only protect JavaThreads in its captured list.
>> 
>> I have instead placed an atomic usage counter (`_in_use_cnt`) in each park 
>> event that tracks the current number of users of the park event. The counter 
>> is set to one in the park event's constructor. Anyone that wants to operate 
>> on the park event can call the park event's `retain_usage()`. This should be 
>> done at a time when it can be guaranteed that the thread associated with the 
>> park event is still alive. Then call `drop_usage()` after the operation is 
>> fulfilled. Calling `retain_usage()` will increase the value of `_in_use_cnt` 
>> by one, and `drop_usage()` usage will decrease the value by one. After 
>> `drop_usage()` has decreased the value it will also check if the value is 
>> down to zero, and if so it will delete the park event.
>> 
>> This way we can make sure that a "retained" reference to the park event is 
>> still valid, even though the park event has outlived the associated thread.
>> 
>> A nice side effect of this PR is that it eliminates a common reason for 
>> spurious wakeups for threads, caused by the fact that we used to recycle 
>> `ParkEvent`s (e.g. allocate from a free-list).
>> 
>> I've used the submitted 
>> [ThreadSpike.java](https://bugs.openjdk.org/secure/attachment/122155/ThreadSpike.java)
>>  test to measure the reduced memory consumption.
>> 
>> `linux-x64/jdk/bin/java -XX:NativeMemoryTracking=summary 
>> -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -Dthreads=20000 
>> ThreadSpike.java  | egrep '(Internal|Synchronization|Thread).*committed='`
>> 
>> This shows that committed `Synchronization` memory goes from ~6.7 MB to ~0.3 
>> MB, which is a ~95% reduction in memory consumption. `Thread` memory only 
>> increases by 1.4 KB and `Internal` memory stays the same.
>> 
>> Running a thread-churn test for one hour, did not show any leakage of 
>> `Synchronization` memory.
>> 
>> Running `test/micr...
>
> Fredrik Bredberg has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Hopefully, the last one for Daniel

Looks good. I agree with David, it is simpler than expected.
Some part of `JvmtiRawMonitor::simple_notify()` update is safe but it feels 
like can be redundant (not necessary). But I'm okay with that in favor of 
safety.

-------------

Marked as reviewed by sspitsyn (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/33770#pullrequestreview-6156764053

Reply via email to