-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8369019: Improve timed-park mechanism in ObjectMonitor for virtual thread support #27597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
👋 Welcome back pchilanomate! A progress list of the required criteria for merging this PR into |
@pchilano This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 167 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
/label remove core-libs |
@pchilano |
Webrevs
|
// all other carriers have a vthread pinned to it waiting for said class | ||
// to be loaded/initialized. | ||
// If there are unmounted virtual threads ahead in the _entry_list we want | ||
// to do a timed-park instead to alleviate some deadlocks cases where one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// to do a timed-park instead to alleviate some deadlocks cases where one | |
// to do a timed-park instead to alleviate some deadlock cases where one |
assert_mark_word_consistency(); | ||
|
||
// If there are unmounted virtual threads ahead in the _entry_list we want | ||
// to do a timed-park instead to alleviate some deadlocks cases where one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// to do a timed-park instead to alleviate some deadlocks cases where one | |
// to do a timed-park instead to alleviate some deadlock cases where one |
// _entry_list uses Atomic::cmpxchg() which already provides a fence that | ||
// prevents this load from floating up previous store. | ||
// Note that we can have false positives where timed-park is not necessary. | ||
bool do_timed_parked = has_unmounted_vthreads(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we still only need the timed-park if the current thread is a pinned vthread?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, except if the monitor is also used in the context of a carrier thread. Currently there are only very few such cases and we disable preemption for them (e.g. interruptLock
), so it’s likely not needed. With the upcoming changes to preempt on klass initialization, we could also have this situation if a class can be initialized both in the context of a carrier and a vthread. Since code executed in the context of the carriers is limited to library code there will also be very few cases of this, but I’ve seen at least one such case with LockSupport
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you are saying the current code is insufficient and could still deadlock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there’s a monitor that is used both in the context of a virtual thread and the carriers then potentially yes. But this should almost never happen, and for the very few special cases we identified we currently disable preemption. So this is mostly to cover for the upcoming changes in case there is a class that can be initialized in the context of both a virtual thread and the carriers. Again this should also be a rare case but I’ve seen at least one case.
// the notifier in notify_internal. | ||
// Note that we can have false positives where timed-park is not necessary. | ||
bool do_timed_parked = has_unmounted_vthreads(); | ||
static int MAX_RECHECK_INTERVAL = 1000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a constant? This is the same as the enter case, should there be only one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I moved it to a global static.
* paths and notification is done using notifyAll. | ||
*/ | ||
@Test | ||
void testMixedPinnedUnmounted() throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you think of testing timed-wait too? Some of the other tests are paramerized with a value source and some time values to test both untimed and timed waits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
/** | ||
* Test no deadlock happens when Object.wait is called from a mix of pinned and non-pinned | ||
* paths and notification is done using notifyAll. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point then maybe we should combine this with RetryMonitorEnterWhenPinned. I'm not suggesting we do this now but some of the expanded description might be useful to include here as a passing reader might not immediately know what this test is doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn’t sure where to put the extra test and missed RetryMonitorEnterWhenPinned.java
. I agree it makes more sense to have it there. Moved now, let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for moving to RetryMonitorEnterWhenPinned as it is similar to the test we had for enter. The update, and conversion to JUnit look good.
@pchilano |
// _entry_list uses Atomic::cmpxchg() which already provides a fence that | ||
// prevents this load from floating up previous store. | ||
// Note that we can have false positives where timed-park is not necessary. | ||
bool do_timed_parked = has_unmounted_vthreads(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you are saying the current code is insufficient and could still deadlock?
} | ||
|
||
inline void ObjectMonitor::inc_unmounted_vthreads() { | ||
assert(_unmounted_vthreads >= 0, ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert(_unmounted_vthreads >= 0, ""); | |
assert(_unmounted_vthreads >= 0, "invariant"); |
Here and below - thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay - thanks for clarifying.
Thanks for the reviews and comments @dholmes-ora, @AlanBateman and @coleenp! |
Going to push as commit 9feb8f2.
Your commit was automatically rebased without conflicts. |
Please review the following fix. When blocking in
ObjectMonitor::enter_internal
we currently use timed-park for pinned virtual threads. This is done to alleviate some potential deadlocks cases where the successor is an unmounted virtual thread that cannot run. In particular this could happen during class loading/initialization if all other carriers are blocked waiting for the same class to be loaded/initialized.This mechanism should be extended to cover
ObjectMonitor::reenter_internal
used inObject.wait
(notification case). Also, the criteria to decide whether to do a timed-park should be based on whether there are unmounted vthreads already in the_entry_list
, and not just if this is a pinned virtual thread. This covers mixed usages of the same ObjectMonitor between virtual threads and platform threads. This will become more relevant once we bring the changes currently in the fibers branch to preempt virtual threads during klass initialization.These changes have been running in the loom pipeline for a couple of months already. I also added a new test case to test/jdk/java/lang/Thread/virtual/MonitorWaitNotify.java which deadlocks without these changes.
Thanks,
Patricio
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27597/head:pull/27597
$ git checkout pull/27597
Update a local copy of the PR:
$ git checkout pull/27597
$ git pull https://git.openjdk.org/jdk.git pull/27597/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 27597
View PR using the GUI difftool:
$ git pr show -t 27597
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27597.diff
Using Webrev
Link to Webrev Comment