On Fri, 6 Nov 2026 09:14:43 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/micro/org/openjdk/bench/vm/lang/MonitorBench.java`...

Functionally this looks really good. I admit I had my doubts about how complex 
it would be to do this, but it is relatively simple. Nice work. I have one 
change on a comment below.

A couple of meta-comments on terminology and the overall approach. Basically 
this is a reference counting scheme - similar to what we have for Symbols - so 
would it be better to actually express it that way: `increment_refount` etc. ? 
This is also basically a "smart pointer" mechanism, so could we express it in 
that form - similar to how JFR has RefCountHandle/RefCountPointer? (Though TBH 
I find these generalized "smart pointer" mechanisms harder to understand.)

Thanks.

src/hotspot/share/prims/jvmtiRawMonitor.cpp line 317:

> 315:     // into random stack space. So we have to ensure we extract (and
> 316:     // retain the usage of) the ParkEvent before we set the TS_RUN
> 317:     // state, and then don't access 'w', only the retained 'ev'.

This copy of the comment is not quite accurate. The `simple_wait` is not really 
affected by the change to TS_RUN. The waiting thread can proceed as soon as we 
drop the `RawMonitor_lock` below as that allows `dequeue_waiter` to proceed, 
which in turn allows the thread to  return from `simple_wait` leaving `w` as a 
dangling pointer. Suggestion:

// The RawMonitor_lock will prevent `w` from being removed, but as soon as we 
drop it, `w` could
// be pointing into random stack space. So we have to ensure we extract (and 
retain the usage of)
// the ParkEvent before doing that, and then don't access `w`, only the 
retained `ev`.

Though we could simply move the unlock to after the final unpark - after all 
for notifyAll we do all but the last unpark with the lock held.

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

Changes requested by dholmes (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/33770#pullrequestreview-6069153222
PR Review Comment: https://git.openjdk.org/jdk/pull/33770#discussion_r4704377416

Reply via email to