Skip to content
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

Reduce lock contention in Thread.getAndClearInterrupt #215

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

babsingh
Copy link
Member

Lock contention in Thread.getAndClearInterrupt is reduced by
acquiring the lock only when the value of the Thread.interrupted
field is true.

Related: eclipse-openj9/openj9#20414

if (oldValue) {
interrupted = false;
clearInterruptEvent();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the equivalent VirtualThread.java method, the synchronized block is just within the if-statement:

boolean getAndClearInterrupt() {
assert Thread.currentThread() == this;
boolean oldValue = interrupted;
if (oldValue) {
synchronized (interruptLock) {
interrupted = false;
carrierThread.clearInterrupt();
}
}
return oldValue;
}

Is there a reason as to why that approach wouldn't work here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionally, no difference will be seen. In the case, where there is high contention i.e. multiple threads attempt to enter the synchronized block and clear the interrupt, only one thread will invoke the native method (Thread.interruptedImpl) due to the extra if statement. Otherwise, all threads that enter the synchronized block will invoke the native method; and this will lower the throughput when resources are scarce. At the cost of an extra if statement, we lower the invocations of the native method in the worst case to improve perf.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is such a performance benefit, we should likely apply this to the VirtualThread.java method too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for perf data. Will apply the change to VirtualThread.java after reviewing perf data.

Copy link
Member Author

@babsingh babsingh Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Related lock contention is completely removed, and there is a 15% perf improvement.
  • Also, VirtualThread.java has been updated.

Lock contention in Thread.getAndClearInterrupt is reduced by
acquiring the lock only when the value of the Thread.interrupted
field is true.

Related: eclipse-openj9/openj9#20414

Signed-off-by: Babneet Singh <[email protected]>
@babsingh babsingh marked this pull request as ready for review November 6, 2024 08:08
@babsingh
Copy link
Member Author

babsingh commented Nov 6, 2024

jenkins test sanity alinux64 jdk21

@babsingh
Copy link
Member Author

babsingh commented Nov 6, 2024

jenkins test extended.system zlinux jdk21

@babsingh
Copy link
Member Author

babsingh commented Nov 6, 2024

jenkins test extended.functional plinux jdk21

@babsingh babsingh requested a review from tajila November 6, 2024 08:29
@tajila tajila merged commit 1b68aa6 into ibmruntimes:openj9 Nov 7, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants