On Thu, 26 Nov 2026 05:49:20 GMT, David Holmes <[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... > > 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. The problem is that `dequeue_waiter() `can exit without grabbing the `RawMonitor_lock`, if the state of `w` is already `TS_RUN` when `dequeue_waiter()` is called. Which can happen if `simple_wait()` calls a timed `park()`, that times out, and `simple_notify() `is called by some thread and sets the state of `w` to `TS_RUN`. Hence, why I believe the `simple_wait()` is affected by change to `TS_RUN`. ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/33770#discussion_r4710876259
