-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Conversation
👋 Welcome back mgronlun! A progress list of the required criteria for merging this PR into |
@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:
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
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 |
Webrevs
|
/remove hotspot |
@mgronlun Unknown command |
/help |
@mgronlun Available commands:
|
/label remove hotspot |
@mgronlun |
/list add hotspot-jfr |
@mgronlun Unknown command |
/label add hotspot-jfr |
/label add hotspot-runtime |
@mgronlun |
@mgronlun |
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.
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?
// 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); |
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.
Why change this? Overall I don't understand the "starting thread" changes.
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 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.
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 so nothing inherently related to the critical section removal, just an API cleanup.
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.
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.
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.
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) { |
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.
static bool assignment_precondition(const Thread* t, JfrThreadLocal* tl) { | |
static bool can_assign(const Thread* t, JfrThreadLocal* tl) { |
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.
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; |
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 supposed to reflect the java.lang.Thread
thread-id? If so this is changing to 3 with the object monitor virtual thread changes.
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 the primordial thread ID assignment is changed from 1 to 3, then that will have to be updated, of course.
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.
Hopefully the value will be available via the ObjectMonitor API so this code can use it symbolically.
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 happens to ID 1 and 2? Other threads that need to be tracked, and receive special treatment in a similar manner?
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.
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
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.
OK, they are reserved constants that do not denote a thread but particular states.
That's okay from our perspective.
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. |
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.
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; |
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.
Hopefully the value will be available via the ObjectMonitor API so this code can use it symbolically.
// 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); |
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.
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.
src/hotspot/share/jfr/jfr.cpp
Outdated
void Jfr::initialize_primordial_thread(JavaThread* jt) { | ||
JfrThreadLocal::initialize_primordial_thread(jt); |
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.
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
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.
Ok.
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.
Updated.
Thanks @dholmes-ora for your review. |
/integrate |
Going to push as commit 5995786.
Your commit was automatically rebased without conflicts. |
@mgronlun - two reviews required for hotspot changes. |
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
Issue
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