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`... src/hotspot/os/posix/os_posix.cpp line 1862: > 1860: assert_status(status == 0, status, "mutex_unlock"); > 1861: > 1862: // Note that we signal() *after* dropping the lock for "immortal" > Events. Q: Just wanted to double-ckeck if this comment saying about `"immortal" Events` need to be updated. It looks like your changes impacted this part. src/hotspot/share/runtime/objectMonitor.cpp line 1528: > 1526: // 4. ST _owner = nullptr > 1527: // 5. unpark(wakee) > 1528: // 6. drop usage of wakee's park event Q: Does the `Exit protocol` described above have differences for platform and virtual threads? The virtual thread case uses the shared immortal unparker events. src/hotspot/share/runtime/objectMonitor.cpp line 1543: > 1541: vthread = Wakee->vthread(); > 1542: assert(vthread != nullptr, ""); > 1543: // The vthread park event is immortal, so no need to retain its > usage. Q: Should the comment also say "is shared immortal"? src/hotspot/share/runtime/park.hpp line 115: > 113: // unpark operation. Using a ThreadsListHandle (TLH) is another. But, > since a > 114: // TLH can not protect a non-Java thread waiting on a raw monitor, we > don't > 115: // use TLH, only usage counting. Q: Does the comment above needs a correction related to the interrupt path as `JavaThread::interrupt()` still depends on a `ThreadsListHandle`? There is a comment at the beginning of `JavaThread::interrupt()`: void JavaThread::interrupt() { // All callers should have 'this' thread protected by a // ThreadsListHandle so that it cannot terminate and deallocate // itself. ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/33770#discussion_r4763984806 PR Review Comment: https://git.openjdk.org/jdk/pull/33770#discussion_r4764058027 PR Review Comment: https://git.openjdk.org/jdk/pull/33770#discussion_r4764070619 PR Review Comment: https://git.openjdk.org/jdk/pull/33770#discussion_r4764022663
