Skip to content

8343177: JFR: Remove critical section for thread id assignment #21756

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

Closed
wants to merge 3 commits into from

Conversation

mgronlun
Copy link

@mgronlun mgronlun commented Oct 29, 2024

Greetings,

With Loom, JFR had to change the assignment of unique thread IDs to threads. For JavaThreads, a dependency exists on the threadObj before the thread ID can be determined and assigned. We currently have a lazy assignment scheme that assigns on first use. Several threads, such as thread iterators and sampling threads, can issue the first use. Hence, a critical section protects assignments.

However, a critical section at this location makes it challenging to build robust signal handlers, for example, because we cannot read the thread ID.

We can remove this critical section with careful rearrangements, ensuring threads are only assigned a thread ID in thread state _thread_new (a thread state that at least the JFR sampler and the JFR iterators exclude).

The problem child is JNI attaching threads, created directly into state _thread_in_vm and added to the threads list before the creation and assignment of the threadObj. We can also manage this case by being careful when we sample such a thread, alternatively allowing for a thread ID of 0 or taking account of the 'attaching via jni' property.

Testing: jdk_jfr Tier 1- 3, Stress testing

Thanks
Markus


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-8343177: JFR: Remove critical section for thread id assignment (Enhancement - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 21756

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 29, 2024

👋 Welcome back mgronlun! 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 Oct 29, 2024

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

8343177: JFR: Remove critical section for thread id assignment

Reviewed-by: dholmes

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

  • f77a514: 8343194: Avoid redundant Hashtable.containsKey call in CodeSource.readObject
  • 4a70c83: 8343378: Exceptions in javax/management DeadLockTest.java do not cause test failure
  • 8d4d589: 8343297: Vector unsigned min/max test are failing with -Xcomp
  • 2a4d9d9: 8343379: [BACKOUT] 8343190 GHA: Try building JTReg several times
  • 6f6cfe6: 8343380: C2: assert(iff->in(1)->is_OpaqueNotNull()) failed: must be OpaqueNotNull
  • cbda758: 8343122: RISC-V: C2: Small improvement for real runtime callouts
  • 803612e: 8343121: RISC-V: More accurate max size for C2SafepointPollStub and C2EntryBarrierStub
  • b627a41: 8343345: Use -jvmArgsPrepend when running microbenchmarks in RunTests.gmk
  • 2932144: 8343305: Remove Indify-dependent microbenchmarks
  • 34655c6: 8342544: [macos] jpackage test helper should check for both "--app-image" and "--mac-sign" for signing predefined app image case
  • ... and 60 more: https://git.openjdk.org/jdk/compare/d5fb6b4a3cf4926acb333e7ee55f96fc76225631...master

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 Oct 29, 2024
@openjdk
Copy link

openjdk bot commented Oct 29, 2024

@mgronlun 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 Oct 29, 2024
@mlbridge
Copy link

mlbridge bot commented Oct 29, 2024

Webrevs

@mgronlun
Copy link
Author

/remove hotspot

@openjdk
Copy link

openjdk bot commented Oct 29, 2024

@mgronlun Unknown command remove - for a list of valid commands use /help.

@mgronlun
Copy link
Author

/help

@openjdk
Copy link

openjdk bot commented Oct 29, 2024

@mgronlun Available commands:

  • approval - request for maintainer's approval
  • approve - null
  • author - sets an overriding author to be used in the commit when the PR is integrated
  • backport - create a backport
  • cc - add or remove an additional classification label
  • clean - Mark the backport pull request as a clean backport
  • contributor - adds or removes additional contributors for a PR
  • covered - used when employer has signed the OCA
  • csr - require a compatibility and specification request (CSR) for this pull request
  • help - shows this text
  • integrate - performs integration of the changes in the PR
  • issue - edit the list of issues that this PR solves
  • jep - require a JDK Enhancement Proposal (JEP) for this pull request
  • label - add or remove an additional classification label
  • open - Set the pull request state to "open"
  • reviewer - manage additional reviewers for a PR
  • reviewers - set the number of additional required reviewers for this PR
  • signed - used after signing the OCA
  • solves - edit the list of issues that this PR solves
  • sponsor - performs integration of a PR that is authored by a non-committer
  • summary - updates the summary in the commit message
  • test - used to run tests

@mgronlun
Copy link
Author

/label remove hotspot

@openjdk openjdk bot removed the hotspot hotspot-dev@openjdk.org label Oct 29, 2024
@openjdk
Copy link

openjdk bot commented Oct 29, 2024

@mgronlun
The hotspot label was successfully removed.

@mgronlun
Copy link
Author

/list add hotspot-jfr

@openjdk
Copy link

openjdk bot commented Oct 29, 2024

@mgronlun Unknown command list - for a list of valid commands use /help.

@mgronlun
Copy link
Author

/label add hotspot-jfr

@mgronlun
Copy link
Author

/label add hotspot-runtime

@openjdk openjdk bot added the hotspot-jfr hotspot-jfr-dev@openjdk.org label Oct 29, 2024
@openjdk
Copy link

openjdk bot commented Oct 29, 2024

@mgronlun
The hotspot-jfr label was successfully added.

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Oct 29, 2024
@openjdk
Copy link

openjdk bot commented Oct 29, 2024

@mgronlun
The hotspot-runtime label was successfully added.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Sorry I struggled to map the code changes to the description of the changes. I think I'm missing some background context here - what is the "traceid" of a thread?

Comment on lines +503 to +505
// Sets the argument thread as starting thread. Returns failure if thread
// creation fails due to lack of memory, too many threads etc.
bool set_as_starting_thread();
static bool set_as_starting_thread(JavaThread* jt);
Copy link
Member

Choose a reason for hiding this comment

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

Why change this? Overall I don't understand the "starting thread" changes.

Copy link
Author

@mgronlun mgronlun Oct 30, 2024

Choose a reason for hiding this comment

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

I changed this with the argument that it is unbalanced to have a non-static instance function available on all Threads for calling t->set_as_starting_thread(), which should only be called on a JavaThread, to set a static field, a static field that is only set once, by the main (primordial) JavaThread during startup in Threads::create_VM().

The assert function is_starting_thread(Thread*t) is for asserting the special assignment of 1 to the primordial thread.

Copy link
Member

Choose a reason for hiding this comment

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

Okay so nothing inherently related to the critical section removal, just an API cleanup.

Copy link
Member

Choose a reason for hiding this comment

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

Actually the more I look at this the more I dislike the fact set_as_starting_thread also calls os::create_main_thread. Seems to me that should be done directly by the caller, and if successful we call set_as_starting_thread. But that is a different cleanup.

Copy link
Member

Choose a reason for hiding this comment

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

Just FTR set_as_starting_thread originally only called os::create_main_thread so why it even existed is a bit of a mystery. The actual _starting_thread variable was something I added under JDK-8214097.

assert(t != nullptr, "invariant");
assert(t->is_Java_thread(), "invariant");
oop threadObj = JavaThread::cast(t)->threadObj();
return threadObj != nullptr ? AccessThreadTraceId::id(threadObj) : 0;
}

#ifdef ASSERT
static bool assignment_precondition(const Thread* t, JfrThreadLocal* tl) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
static bool assignment_precondition(const Thread* t, JfrThreadLocal* tl) {
static bool can_assign(const Thread* t, JfrThreadLocal* tl) {

Copy link
Author

Choose a reason for hiding this comment

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

Good suggestion.

assert(jt != nullptr, "invariant");
assert(Thread::is_starting_thread(jt), "invariant");
assert(jt->threadObj() == nullptr, "invariant");
jt->jfr_thread_local()->_jvm_thread_id = 1;
Copy link
Member

Choose a reason for hiding this comment

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

Is this supposed to reflect the java.lang.Thread thread-id? If so this is changing to 3 with the object monitor virtual thread changes.

Copy link
Author

Choose a reason for hiding this comment

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

If the primordial thread ID assignment is changed from 1 to 3, then that will have to be updated, of course.

Copy link
Member

Choose a reason for hiding this comment

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

Hopefully the value will be available via the ObjectMonitor API so this code can use it symbolically.

Copy link
Author

@mgronlun mgronlun Oct 31, 2024

Choose a reason for hiding this comment

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

What happens to ID 1 and 2? Other threads that need to be tracked, and receive special treatment in a similar manner?

Copy link
Contributor

@coleenp coleenp Oct 31, 2024

Choose a reason for hiding this comment

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

There will be no Java thread-id 1 and 2. Those values are used as values for deflated-marker and anonymous-owner for ObjectMonitor see #21565

Copy link
Author

Choose a reason for hiding this comment

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

OK, they are reserved constants that do not denote a thread but particular states.

That's okay from our perspective.

@mgronlun
Copy link
Author

mgronlun commented Oct 30, 2024

Sorry I struggled to map the code changes to the description of the changes. I think I'm missing some background context here - what is the "traceid" of a thread?

A traceid is a unique, monotonic, type specific ID that JFR assigns to artifacts (instances) (Threads, Klasses, Methods, CLDs, Symbols etc).

Invariant; no reuse of IDs.

With Loom, the unique monotonic ID for threads has been made intrinsic to the tid field of the threadObj.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

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

Overall looks good. Thanks for the clarifications.

I have one change request with regards to naming below.

Thanks

assert(jt != nullptr, "invariant");
assert(Thread::is_starting_thread(jt), "invariant");
assert(jt->threadObj() == nullptr, "invariant");
jt->jfr_thread_local()->_jvm_thread_id = 1;
Copy link
Member

Choose a reason for hiding this comment

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

Hopefully the value will be available via the ObjectMonitor API so this code can use it symbolically.

Comment on lines +503 to +505
// Sets the argument thread as starting thread. Returns failure if thread
// creation fails due to lack of memory, too many threads etc.
bool set_as_starting_thread();
static bool set_as_starting_thread(JavaThread* jt);
Copy link
Member

Choose a reason for hiding this comment

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

Actually the more I look at this the more I dislike the fact set_as_starting_thread also calls os::create_main_thread. Seems to me that should be done directly by the caller, and if successful we call set_as_starting_thread. But that is a different cleanup.

Comment on lines 103 to 104
void Jfr::initialize_primordial_thread(JavaThread* jt) {
JfrThreadLocal::initialize_primordial_thread(jt);
Copy link
Member

Choose a reason for hiding this comment

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

The main thread is generally not the process primordial thread. Can we use main_thread or starting_thread here to keep semi-consistent with the Thread/Threads code terminology? Thanks

Copy link
Author

Choose a reason for hiding this comment

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

Ok.

Copy link
Author

Choose a reason for hiding this comment

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

Updated.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 1, 2024
@mgronlun
Copy link
Author

mgronlun commented Nov 1, 2024

Thanks @dholmes-ora for your review.

@mgronlun
Copy link
Author

mgronlun commented Nov 1, 2024

/integrate

@openjdk
Copy link

openjdk bot commented Nov 1, 2024

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

  • 751a914: 8340733: Add scope for relaxing constraint on JavaCalls from CompilerThread
  • 7e87c07: 8340116: test/jdk/sun/security/tools/jarsigner/PreserveRawManifestEntryAndDigest.java can fail due to regex
  • da0e9e3: 8343333: Parallel: Cleanup comment referring Solaris in MutableNUMASpace
  • f77a514: 8343194: Avoid redundant Hashtable.containsKey call in CodeSource.readObject
  • 4a70c83: 8343378: Exceptions in javax/management DeadLockTest.java do not cause test failure
  • 8d4d589: 8343297: Vector unsigned min/max test are failing with -Xcomp
  • 2a4d9d9: 8343379: [BACKOUT] 8343190 GHA: Try building JTReg several times
  • 6f6cfe6: 8343380: C2: assert(iff->in(1)->is_OpaqueNotNull()) failed: must be OpaqueNotNull
  • cbda758: 8343122: RISC-V: C2: Small improvement for real runtime callouts
  • 803612e: 8343121: RISC-V: More accurate max size for C2SafepointPollStub and C2EntryBarrierStub
  • ... and 63 more: https://git.openjdk.org/jdk/compare/d5fb6b4a3cf4926acb333e7ee55f96fc76225631...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Nov 1, 2024

@mgronlun Pushed as commit 5995786.

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

@dholmes-ora
Copy link
Member

@mgronlun - two reviews required for hotspot changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-jfr hotspot-jfr-dev@openjdk.org hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

3 participants