Skip to content

8355627: Don't use ThreadCritical for EventLog list#24954

Closed
coleenp wants to merge 5 commits intoopenjdk:masterfrom
coleenp:tc
Closed

8355627: Don't use ThreadCritical for EventLog list#24954
coleenp wants to merge 5 commits intoopenjdk:masterfrom
coleenp:tc

Conversation

@coleenp
Copy link
Contributor

@coleenp coleenp commented Apr 29, 2025

Use LockFreeStack to link events on the eventLog queue. They are never popped so this requires no further synchronization.
Tested by tier1-4.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8355627: Don't use ThreadCritical for EventLog list (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24954/head:pull/24954
$ git checkout pull/24954

Update a local copy of the PR:
$ git checkout pull/24954
$ git pull https://git.openjdk.org/jdk.git pull/24954/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24954

View PR using the GUI difftool:
$ git pr show -t 24954

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24954.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 29, 2025

👋 Welcome back coleenp! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Apr 29, 2025

@coleenp 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:

8355627: Don't use ThreadCritical for EventLog list

Reviewed-by: shade, lmesnik, zgu

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 168 new commits pushed to the master branch:

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 master branch, type /integrate in a new comment.

@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 29, 2025
@openjdk
Copy link

openjdk bot commented Apr 29, 2025

@coleenp The following label will be automatically applied to this pull request:

  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot hotspot-dev@openjdk.org label Apr 29, 2025
@mlbridge
Copy link

mlbridge bot commented Apr 29, 2025

Webrevs

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Not having extra locks during initialization is nice. But I am somewhat confused why we need LockFreeStack here. Looks like we "only" need to build the linked list of EventLog-s in thread-safe manner? So why don't just do our usual Atomic dance, e.g. (untested!):

EventLog::EventLog() {
  EventLog* old_head;
  do {
    old_head = Atomic::load(&Events::_logs);
    _next = old_head;
  } while (Atomic::cmpxchg(&Events::_logs, old_head, this, memory_order_relaxed) != old_head);
}

@coleenp
Copy link
Contributor Author

coleenp commented Apr 30, 2025

So I was playing around with LockFreeStack because I thought it might be a good way to avoid writing yet another CAS loop in the code. But you're right, I don't really use LockFreeStack which isn't so lock free since it requires synchronization to pop. I couldn't decide whether I wanted to use it partially or write the CAS loop. I take it this is a vote for the CAS loop.

do {
old_head = Atomic::load(&Events::_logs);
_next = old_head;
} while (Atomic::cmpxchg(&Events::_logs, old_head, this, memory_order_relaxed) != old_head);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it is safe to use memory_order_relaxed order if events are added after JVM enters multi-thread mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this. I didn't know why Aleksey suggested it. I'll remove it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe @shipilev meant memory_order_release? Anyway, I guess we don't need to optimize it.

Copy link
Member

@shipilev shipilev May 1, 2025

Choose a reason for hiding this comment

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

I saw no point in enforcing memory ordering mode here, as it looks like we only did ThreadCritical for mutual exclusion. Note that we do not have a matching acquire on list traversals, so seqcst/release on list additions would be incomplete. That only reinforces my original thinking: we are riding on memory ordering given by something else, I'd guess the initialization sequence itself.

But I won't quibble, it is a very minor optimization.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we rely on memory ordering by address dependency on the reader's side.

Copy link
Contributor

Choose a reason for hiding this comment

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

First of all, nothing on the writer's side can fix the reader's side.

We're basically relying on volatile load + dependency chain on the reader's side. This is very likely to work, but C++ memory model specialists may find this questionable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are we on the same page? which load you are talking about?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm talking about Atomic::load() which is essentially a volatile load without ordering.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe we need reader side barrier, given the conservative order on writer side. @shipilev any opinion?

Copy link
Member

@shipilev shipilev May 5, 2025

Choose a reason for hiding this comment

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

Well, if we are going fully pedantic here, we only need to make sure that the EventLog::_next is properly visible to the code that traverses the EventLog list. This realistically only matters if there are no synchronization points in between those, which I suspect are plenty. E.g. this is the same thing that make all other init code robust in practice: things that happen at init are good to go.

If you want to cover the path to be fully 100% bullet-proof and squeeze some performance at the same time, then I think cmpxchg to Events::_logs should be release and loads from there should be acquire-s.

Copy link
Member

@lmesnik lmesnik left a comment

Choose a reason for hiding this comment

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

The changes looks good.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Apr 30, 2025
Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Looks fine, thanks.

Copy link
Contributor

@zhengyu123 zhengyu123 left a comment

Choose a reason for hiding this comment

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

LGTM

@coleenp
Copy link
Contributor Author

coleenp commented May 2, 2025

Thank you for reviewing and commenting, Aleksey, Zhengyu, Leonid and Martin.
/integrate

@openjdk
Copy link

openjdk bot commented May 2, 2025

Going to push as commit afb9134.
Since your change was applied there have been 184 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 2, 2025
@openjdk openjdk bot closed this May 2, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 2, 2025
@openjdk
Copy link

openjdk bot commented May 2, 2025

@coleenp Pushed as commit afb9134.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@coleenp coleenp deleted the tc branch May 2, 2025 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

5 participants